测试弹簧重试断路器

时间:2018-07-03 01:37:16

标签: spring-boot junit5 spring-retry circuit-breaker

我在以下其中一个Spring Boot应用程序中使用Spring Retry断路器:

   @CircuitBreaker(include = CustomException.class, maxAttempts = 3, openTimeout = 2000L, resetTimeout = 4000L)
StudentResponse getStudentInfo(String studentId) {
    StudentResponse res = studentRepository.getInfoByStudentId(studentId);
    return res;
}

@Recover
StudentResponse recoverStudentInfo(CustomException ex, String studentId) {
    throw new StudentApiException("In recovery...");
}

现在我正尝试使用Junit 5对其进行测试:

    @ExtendWith(SpringExtension.class)
    @SpringBootTest
    @EnableRetry
    public class StudentServiceTest {

    @Autowired
    StudentService studentService;

    @MockBean
    StudentRepository studentRepository;

    @Test
    public void testCircuitBreakGetStudentInfo(){
        String id = "id";
        doThrow(CustomException.class).when(studentRepository).getInfoByStudentId(id);
        StudentResponse res = this.studentService.getStudentInfo(id);
        verify(studentRepository, times(3)).getInfoByStudentId(id); // this is telling that there has been 1 interaction with the mock

    }
}

然后我尝试:

 @Test
    public void testCircuitBreakGetStudentInfo(){
        String id = "id";
        doThrow(CustomException.class).when(studentRepository).getInfoByStudentId(id);
        assertThrows(StudentApiException.class,
            ()->{ this.studentService.getStudentInfo(id); }); // this is also telling that there has been 1 interaction with the mock instead of 3
    }

应该没有3个互动?还是一个?任何解释都会有很大帮助。任何有关测试断路器的技巧都值得赞赏(我很难找到有关测试此断路器的任何示例)。谢谢

0 个答案:

没有答案