CircuitBreaker コード例の単体テストを記述しようとすると、次のエラーが発生します。
org.springframework.beans.factory.UnsatisfiedDependencyException: 'com.foosball.team.Service.TeamServiceUnitTest' という名前の Bean の作成中にエラーが発生しました: フィールド 'registry' で表現された依存関係が満たされていません。ネストされた例外は org.springframework.beans.factory.NoSuchBeanDefinitionException: タイプ 'io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry' の適格な Bean がありません: オートワイヤー候補として適格な少なくとも 1 つの Bean が必要です。依存関係アノテーション: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
チームサービス:
@Service
public class TeamService {
@Autowired
private ITeamRepository teamRepository;
@Autowired
private PlayerClient playerClient;
Logger logger = LoggerFactory.getLogger(TeamService.class);
.
.
.
TeamServiceUnitTest:
@SpringBootTest(classes = {
TeamService.class
})
@RunWith(SpringRunner.class)
@DirtiesContext
public class TeamServiceUnitTest extends AbstractCircuitBreakerTest {
@MockBean
private ITeamRepository teamRepository;
@MockBean
private PlayerClient playerClient;
@Autowired
private TeamService service;
private TeamEntity teamEntity;
private Logger logger = LoggerFactory.getLogger(TeamServiceUnitTest.class);
@Before
public void setUp(){
teamEntity = new TeamEntity();
teamEntity.setId(1L);
teamEntity.setPlayerOne("One");
teamEntity.setPlayerTwo("Two");
teamEntity.setPlayerThree("Three");
teamEntity.setPlayerFour("Four");
}
@Test
@DisplayName("when Player Client Fails 11 Times Then CircuitBreaker Should Be In OPEN State")
public void whenPlayerClientFailsElevenTimesThenCircuitBreakerShouldBeInOPENState(){
//Given
when(teamRepository.findAll()).thenReturn(new ArrayList<>());
when(playerClient.get(Mockito.anyString())).thenThrow(new RuntimeException());
//When
for(int i=0; i<11; i++){
try {
service.addTeam(teamEntity);
} catch (Exception ignore) {
logger.info(ignore.getClass().getName());
}
}
//Then
checkHealthStatus(BACKEND_B, CircuitBreaker.State.OPEN);
}
}
参照クラス:
public class AbstractCircuitBreakerTest {
protected static final String BACKEND_A = "backendA";
protected static final String BACKEND_B = "playerClientCircuitBreaker";
@Autowired
protected CircuitBreakerRegistry registry;
@Before
public void setup(){
transitionToClosedState(BACKEND_A);
transitionToClosedState(BACKEND_B);
}
protected void checkHealthStatus(String circuitBreakerName, CircuitBreaker.State state) {
CircuitBreaker circuitBreaker = registry.circuitBreaker(circuitBreakerName);
assertThat(circuitBreaker.getState()).isEqualTo(state);
}
protected void transitionToOpenState(String circuitBreakerName) {
CircuitBreaker circuitBreaker = registry.circuitBreaker(circuitBreakerName);
if(!circuitBreaker.getState().equals(CircuitBreaker.State.OPEN)){
circuitBreaker.transitionToOpenState();
}
}
protected void transitionToClosedState(String circuitBreakerName) {
CircuitBreaker circuitBreaker = registry.circuitBreaker(circuitBreakerName);
if(!circuitBreaker.getState().equals(CircuitBreaker.State.CLOSED)){
circuitBreaker.transitionToClosedState();
}
}
}
参照プロジェクト: https://github.com/resilience4j/resilience4j-spring-boot2-demo
提案された Soln ディスカッション: https://github.com/resilience4j/resilience4j-spring-boot2-demo/issues/33
提案された Soln プロジェクト: https://github.com/shah-smit/resilience4j-spring-boot2-demo-maven