简体   繁体   English

Microprofile 容错单元测试

[英]Microprofile Fault Tolerance Unit Test

I am using Qurkus with Microprofile Fault Tolerance to implement foult tollerance on a JDBC connection ( Dremio ).我正在使用 Qurkus 和Microprofile Fault Tolerance在 JDBC 连接 ( Dremio ) 上实现Microprofile Fault Tolerance

I have implemented something like that:我已经实现了类似的东西:

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

}

Now I am trying to test the retry with Unit Test.现在我正在尝试使用单元测试来测试重试。 I didn't find any documentation related to Microprofile Fault Tolerance .我没有找到任何与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);

    }
}

My expectation is that the data return after one retry (the first call to getData return an error).我的期望是重试后数据返回(第一次调用getData返回错误)。 Instead return only the error.而是只返回错误。

Which is the corrrect way in order to testing the retries of my method?为了测试我的方法的重试,哪种方法是正确的?

Mocks unfortunately can't be used with Fault Tolerance annotations because Mockito effectively overrides everything about the original method, including the @Retry annotation.不幸的是,Mocks 不能与 Fault Tolerance 注释一起使用,因为 Mockito 有效地覆盖了原始方法的所有内容,包括@Retry注释。 You can fix this by moving the @Retry annotation onto something that you don't mock (the Service.callService() method), so that callService method will be retried instead, and on the second attempt, the call to the repository will go through.您可以通过将@Retry注释移到您不模拟的对象( Service.callService()方法)上来解决此问题,这样将重试callService方法,并且在第二次尝试时,将调用存储库通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM