![](/img/trans.png)
[英]WLPs Microprofile Fault Tolerance bulkhead implementation not kicking in
[英]Microprofile Fault Tolerance Unit Test
我正在使用 Qurkus 和Microprofile Fault Tolerance
在 JDBC 连接 ( Dremio ) 上实现Microprofile Fault Tolerance
。
我已经实现了类似的东西:
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
方法,并且在第二次尝试时,将调用存储库通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.