gpt4 book ai didi

java - mockito UnfinishedStubbingException 而不是想要的异常

转载 作者:行者123 更新时间:2023-12-01 23:24:07 31 4
gpt4 key购买 nike

我正在尝试模拟一个客户端响应不佳的服务进行测试。我仍然在 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");
}

}

检查UserServiceImpl:

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

提前致谢。

最佳答案

您使用 Mockito.doThrow 是错误的。

您的代码:

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

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

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

关于java - mockito UnfinishedStubbingException 而不是想要的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20260479/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com