2

我在我的一个 Spring Boot 应用程序中使用 Spring Retry Circuit Breaker:

   @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次互动吗?还是会是一个?任何解释都会有很大帮助。任何其他有关测试断路器的技巧都值得赞赏(我几乎找不到任何测试此断路器的示例)。谢谢

4

0 回答 0