如何解决Microprofile容错单元测试
我正在将Qurkus与Microprofile Fault Tolerance
结合使用,以对JDBC连接(Dremio)实施全额收费。
我已经实现了类似的东西:
class Repository {
private final DataSource dataSource; //initialized on constructor
Collection<String> getData() exception SQLException {
try (var conn = dataSource.getConnection();
var stmt = conn.createStatement();
var rs = stmt.executeQuery(sql)) {
var result = new ArrayList<String>();
while (rs.next()) {
result.add(rs.getString("data"));
}
return result;
} catch(SQLException e) {
//log and throw custom exception
}
}
}
class Service {
@Inject
Repository repo;
public Collection<String> callService() {
//other code that dosen't require retries
try {
var res = getData();
} catch (Exception e) {
//log exception
throw new CustomException(e);
}
return res;
}
@Retry()
private Collection<String> getData() throw Exception {
return repo.getData();
}
}
现在,我正在尝试使用单元测试对重试进行测试。我没有找到任何与Microprofile Fault Tolerance
相关的文档。
@QuarkusTest
class ServiceTest {
@Inject
Service service;
@InjectMock
Repository repository;
@Test
void shouldHandleRetryWhenErrorOccursDuringQueryData() throws Exception {
ArrayList<String> expectedResult = Lists.newArrayList("1","2");
when(repository.getData())
.thenThrow(new RuntimeException("Runtime Exception"))
.thenReturn(expectedResult);
Collection<String> executionResult = service.callService();
assertIterableEquals(expectedResult,executionResult);
}
}
我的期望是,数据在一次重试后返回(对getData
的第一次调用返回错误)。而是仅返回错误。
测试我的方法重试的正确方法是什么?
解决方法
不幸的是,不能将Mocks与Fault Tolerance注释一起使用,因为Mockito有效地覆盖了有关原始方法的所有内容,包括@Retry
注释。您可以通过将@Retry
注释移到您不嘲笑的东西(Service.callService()
方法)上来解决此问题,以便改用callService
方法,然后再次尝试,对存储库的调用将通过。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。