如何在@Autowired注射后设置EasyMock的期望?

时间:2014-04-24 18:16:55

标签: spring testing jaxb spring-ws easymock

我试图掌握EasyMock,以便在spring-ws Web服务上运行一些服务器端集成测试。我有一个DAO,我想为我的集成测试进行模拟,我已经设法成功自动装配它,但我无法弄清楚如何设置自动装配后的期望。

我的弹簧上下文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>   

我的虚拟测试如下:

@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));
    }
}

被击中的端点如下:

@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;
}

这里是包含被模拟的DAO的服务类

@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) {

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

通过调试我可以看到模拟DAO正在被正确注入,但我不知道如何在模拟对象上设置行为。

对于我的单元测试,我能够做到以下几点:

    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);

当通过spring xml config自动装配模拟时,我不确定如何进行相同的配置。我可能会遗漏一些明显或误解的EasyMock,但任何帮助或指导都会受到赞赏。

由于

1 个答案:

答案 0 :(得分:0)

根据丹的上述评论,我的期望如下:

@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));
    }
}

工作正常。