我試圖嘲笑服務與客戶端測試的壞客戶端。 我仍然在min eclass中得到一個UnfinishedStubbingException而不是ClientResponseFailure,我看不到我做錯了什麼。 我也試過:mockito UnfinishedStubbingException而不是想要異常
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。
現在我總是得到EsbOfflineException,所以在HrService中沒有拋出異常。 – chillworld
然後修復你的代碼;) – Brice