mockito UnfinishedStubbingException而不是所需的Exception



我正试图模拟一个客户端响应不好的服务进行测试。我仍然会在min-eclass中得到一个UnfinishedStubbingException,而不是ClientResponseFailure,我看不出我做错了什么。我也试过:

 Mockito.when(hrServiceBad.verifyIdentity("412", "aaa", "aaa")).thenThrow(clientResponseFailure);

但是给出了相同的结果。

矿山代码:

@Autowired
private CheckUserServiceImpl checkUserService;
private HrService hrServiceBad;
private ClientResponseFailure clientResponseFailure;
private ClientResponseImpl response = new ClientResponseImpl();
@Before
public void init() {
    hrServiceBad = Mockito.mock(HrService.class);
    checkUserService.setHrService(hrServiceBad);
    clientResponseFailure = new ClientResponseFailure(response);
}
@Test(expected = EsbVerificationError.class)
public void testUserValidInEsbWith412Fault() throws EsbOffLineException, EsbVerificationError {
    // precondition failed
    response.setStatus(412);
    Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));
    checkUserService.verifyUserInEsb("412", "aaa", "aaa");
}
@Test(expected = EsbOffLineException.class)
public void testUserValidInEsbWith503Fault() throws EsbOffLineException, EsbVerificationError {
    // service unavailable
    response.setStatus(503);
    Mockito.when(hrServiceBad.verifyIdentity("503", "aaa", "aaa"))
            .thenThrow(clientResponseFailure);
    checkUserService.verifyUserInEsb("503", "aaa", "aaa");
}
@Test(expected = EsbOffLineException.class)
public void testUserValidInEsbWith404Fault() throws EsbOffLineException, EsbVerificationError {
    // page not found
    response.setStatus(404);
    Mockito.when(hrServiceBad.verifyIdentity("404", "aaa", "aaa"))
            .thenThrow(clientResponseFailure);
    checkUserService.verifyUserInEsb("404", "aaa", "aaa");
}
@Test(expected = EsbOffLineException.class)
public void testUserValidInEsbWith403Fault() throws EsbOffLineException, EsbVerificationError {
    // page forbidden
    response.setStatus(403);
    Mockito.when(hrServiceBad.verifyIdentity("403", "aaa", "aaa"))
            .thenThrow(clientResponseFailure);
    checkUserService.verifyUserInEsb("403", "aaa", "aaa");
}
@Test(expected = EsbOffLineException.class)
public void testUserValidInEsbWith522Fault() throws EsbOffLineException, EsbVerificationError {
    // connection timeout
    response.setStatus(522);
    Mockito.when(hrServiceBad.verifyIdentity("522", "aaa", "aaa"))
            .thenThrow(clientResponseFailure);
    checkUserService.verifyUserInEsb("522", "aaa", "aaa");
}

}

checkUserServiceImpl:

 @Override
public void verifyUserInEsb(final String nationalNumber, final String serviceNumber,
        final String bafuser) throws EsbOffLineException, EsbVerificationError {
    String cleanedNationalNumber = BulletinUserManager.keepDigitsOnly(nationalNumber);
    try {
        Identity identity = this.hrService.verifyIdentity(cleanedNationalNumber, serviceNumber, bafuser);
        if (identity != null) {
            //more code here but not relevant.
            return;
        }
    } catch (ClientResponseFailure e) {
        logger.info(e.getResponse().getStatus());
        if (PRECONDITION_FAILED == e.getResponse().getStatus()) {
            throw new EsbVerificationError("Hr check failed");
        }
    }        
    throw new EsbOffLineException();
}

提前Thx。

您使用的是Mockito.doThrow错误。

您的代码:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));

但是when方法只需要您的mock作为单个参数:

Mockito.doThrow(clientResponseFailure).when(hrServiceBad).verifyIdentity("412", "aaa", "aaa");

存在多个故障。第一个确实像Stefan Birkner所说的那样。

第二:

@Autowired
private CheckUserServiceImpl checkUserService;
private HrService hrServiceBad;
private ClientResponseFailure clientResponseFailure;
private ClientResponseImpl response = new ClientResponseImpl();
@Before
public void init() {
    hrServiceBad = Mockito.mock(HrService.class);
    checkUserService.setHrService(hrServiceBad);
}
@Test(expected = EsbVerificationError.class)
public void testUserValidInEsbWith412Fault() throws EsbOffLineException, EsbVerificationError {
    // precondition failed
    response.setStatus(412);
    clientResponseFailure = new ClientResponseFailure(response); // add this.
    Mockito.doThrow(clientResponseFailure).when(hrServiceBad).verifyIdentity("412", "aaa", "aaa");
    checkUserService.setHrService(hrServiceBad);
    checkUserService.verifyUserInEsb("412", "aaa", "aaa");
}

怎么了?经过一点搜索,我发现response.getStatus给出的代码为0。在每次测试中直接使用init,并在将响应添加到ClientResponseFailure之前先在响应中设置错误代码,从而修复了整个问题。

当你这样做时:

clientResponseFailure = new ClientResponseFailure(response);
response.setStatus(412);

测试失败。

相关内容

  • 没有找到相关文章

最新更新