我正在尝试使用错误的客户端响应来模拟服务以进行测试。 我仍然在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();
}
提前谢谢。
答案 0 :(得分:3)
您使用的是Mockito.doThrow错误。
您的代码:
Mockito.doThrow(clientResponseFailure).when(hrServiceBad.verifyIdentity("412", "aaa", "aaa"));
但是when
方法只需要将mock作为单个参数:
Mockito.doThrow(clientResponseFailure).when(hrServiceBad).verifyIdentity("412", "aaa", "aaa");
答案 1 :(得分:0)
哪里有多个故障。 第一个确实像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);
测试失败。