com.mockrunner.example.connector.PersonSearchDAOTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Show all versions of mockrunner-jdk1.3-j2ee1.3 Show documentation
Mockrunner is a lightweight framework for unit testing applications
in the J2EE environment. It supports servlets, filters, tag classes
and Struts actions. It includes a JDBC a JMS and a JCA test
framework and can be used to test EJB based applications.
The newest version!
package com.mockrunner.example.connector;
import java.io.FileInputStream;
import com.mockrunner.connector.ConnectorTestCaseAdapter;
import com.mockrunner.connector.StreamableRecordByteArrayInteraction;
import com.mockrunner.ejb.EJBTestModule;
/**
* Example test for {@link PersonSearchDAO}. The two files
* personin.bin and personout.bin are snapshots
* from a real mainframe communication. Once created, the snapshot
* files can be used to simulate mainframe access in tests.
* The personin.bin file represents an empty person with an id of
* 1, which is the request. The personout.bin contains the
* user data for the person with id 1. If we search for a user with id
* 1, the framework recognizes that the actual request matches the expected
* request and returns the actual response (the personout.bin data).
* If we pass 2 as the id, no response is found and findPersonById
* returns an empty person. This example uses the
* {@link com.mockrunner.connector.StreamableRecordByteArrayInteraction} which
* works with byte data. {@link com.mockrunner.connector.StreamableRecordByteArrayInteraction}
* can always be used when the involved Record
classes implement
* Streamable
.
*/
public class PersonSearchDAOTest extends ConnectorTestCaseAdapter
{
private EJBTestModule ejbModule;
private PersonSearchDAO dao;
protected void setUp() throws Exception
{
super.setUp();
ejbModule = createEJBTestModule();
ejbModule.bindToContext("java:ra/cics/ConnectionFactory", getConnectorMockObjectFactory().getMockConnectionFactory());
dao = new PersonSearchDAO();
}
private void prepareInteraction() throws Exception
{
StreamableRecordByteArrayInteraction interaction = new StreamableRecordByteArrayInteraction();
FileInputStream request = new FileInputStream("src/com/mockrunner/example/connector/personin.bin");
FileInputStream response = new FileInputStream("src/com/mockrunner/example/connector/personout.bin");
interaction.setExpectedRequest(request);
interaction.setResponse(response);
getInteractionHandler().addImplementor(interaction);
request.close();
response.close();
}
public void testFindPersonByIdFound() throws Exception
{
prepareInteraction();
Person response = dao.findPersonById("1");
assertEquals("1", response.getId());
assertEquals("Jane", response.getFirstName());
assertEquals("Doe", response.getLastName());
assertEquals(30, response.getAge());
verifyConnectionClosed();
verifyAllInteractionsClosed();
}
public void testFindPersonByIdNotFound() throws Exception
{
prepareInteraction();
Person response = dao.findPersonById("2");
assertNull(response.getId());
verifyConnectionClosed();
verifyAllInteractionsClosed();
}
}