Spring boot 断路器弹簧试验
我在我的一个Spring Boot应用程序中使用Spring Retry断路器,如下所示:Spring boot 断路器弹簧试验,spring-boot,junit5,spring-retry,circuit-breaker,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 = studentReposito
@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种互动吗?还是会是一个?任何解释都会大有帮助。任何关于测试断路器的其他提示都是值得赞赏的(我几乎找不到任何关于测试此断路器的示例)。谢谢