Question

I'm trying to get to grips with EasyMock in order to run some server side integration tests on a spring-ws web service. I have a DAO which I want to mock for my integration testing, I've managed to autowire it successfully, but I can't figure out how to set the expectations post autowire.

I have the following in my spring context xml:

<bean id="accountServiceDao" class="org.easymock.EasyMock" factory-method="createMock">
    <constructor-arg value="com.xxx.account.dao.AccountServiceDao" />
</bean> 

<bean id="notMockedDao" class="com.xxx.account.dao.AccountServiceDaoImpl"/>

<context:component-scan base-package="com.xxx.account" />

<context:property-placeholder location="classpath:accountDetailService_test.properties" />

<sws:annotation-driven />

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:sql/db_schema.sql" />
    <jdbc:script location="classpath:sql/test_data.sql" />
</jdbc:embedded-database>   

My dummy test is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext_test.xml" })
public class AccountDetailServiceMockIntergrationTest {

    @Autowired
    private ApplicationContext applicationContext;

    private MockWebServiceClient mockClient;

    @Before
    public void createClient() {

        mockClient = MockWebServiceClient.createClient(applicationContext);

        /* Set the expectations for the autowired mock dao here */

    }

    @Test
    public void customerEndpoint() throws Exception {
        Source requestPayload = new StringSource(TestData.requestXML);

        Source responsePayload = new StringSource(TestData.responseXML);

        mockClient.sendRequest(withPayload(requestPayload)).andExpect(
                payload(responsePayload));
    }
}

The endpoint which is hit is below:

@Autowired
private AccountService accountService;

@PayloadRoot(localPart = "AccountSearchRequest", namespace = TARGET_NAMESPACE)
public @ResponsePayload
AccountSearchResponse getAccountDetails(
        @RequestPayload AccountSearchRequest request) {
    logger.info("Received request | debtornum - " + request.getDebtornum());

    AccountSearchResponse accountSearchResponse = objectFactory.createAccountSearchResponse();
    AccountDetailsType accountDetails = accountService.getAccountDetails(request.getDebtornum());

    accountSearchResponse.setAccountDetails(accountDetails);

    logger.info("Returned response | status - " + accountSearchResponse.getAccountDetails().getDebtorStatus().value());

    return accountSearchResponse;
}

And here's the service class which contains the DAO which is being mocked

@Service
public class AccountServiceImpl implements AccountService {

    //Autowired on a setter
    private AccountServiceDao accountServiceDao;

    Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class);

    @Override
    public AccountDetailsType getAccountDetails(BigInteger accountNumber) {

........................

Via debug I can see that the mock DAO is getting injected correctly, but I don't know how to set the behavior on the mock object.

For my unit tests I was able to do the following:

    accountDao = EasyMock.createMock(AccountServiceDao.class);

    EasyMock.expect(accountDao.checkAccountExists(new BigInteger("12345678"))).andReturn(new Account(new BigInteger("12345678"),"Y",1,0,0,0,"ROI","ROI","DO","10012054082","POST","DD","John Doe","a@a.com","123456"));

    EasyMock.replay(accountDao);

    testSvc.setAccountServiceDao(accountDao);

I'm not sure how to do the same configuration when the mock is autowired via spring xml config. I'm probably missing something obvious or misunderstanding EasyMock, but any help or guidance would be appreciated.

Thanks

Was it helpful?

Solution

As per Dan's comment above, I've set my expectations as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext_test.xml" })
public class AccountDetailServiceMockIntergrationTest {

    @Autowired
    private ApplicationContext applicationContext;

    private MockWebServiceClient mockClient;

    @Before
    public void createClient() {

        mockClient = MockWebServiceClient.createClient(applicationContext);

        //get the mocked bean from the applicationContext
        AccountServiceDao svcDao = (AccountServiceDao)applicationContext.getBean("accountServiceDao");

        //reset just in case
        EasyMock.reset();

        //set expectations on the mock
        EasyMock.expect(svcDao.checkAccountExists(new BigInteger("12345678"))).andReturn(new Account(new BigInteger("12345678"),"Y",1,0,0,0,"ROI","ROI","DO","10012054082","POST","DD","John Doe","a@a.com","123456"));

        EasyMock.replay(svcDao);
    }

    @Test
    public void customerEndpoint() throws Exception {
        Source requestPayload = new StringSource(TestData.requestXML);

        Source responsePayload = new StringSource(TestData.responseXML);

        mockClient.sendRequest(withPayload(requestPayload)).andExpect(
                payload(responsePayload));
    }
}

Works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top