gpt4 book ai didi

java - UnitTesting Spring Boot 中 Unresolved 依赖关系

转载 作者:行者123 更新时间:2023-12-02 08:53:55 29 4
gpt4 key购买 nike

尝试为 CircuitBreaker 代码示例编写单元测试时出现以下错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.foosball.team.Service.TeamServiceUnitTest': Unsatisfied dependency expressed through field 'registry'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@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);
.
.
.

团队服务单元测试:

@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

最佳答案

看起来您在测试中尝试 Autowiring 的 CircuitBreakerRegistry 是一个工厂类,而不是一个 bean。代替@Autowired
protected CircuitBreakerRegistry注册表;

尝试: protected CircuitBreakerRegistry 注册表 = CircuitBreakerRegistry.ofDefaults()

关于java - UnitTesting Spring Boot 中 Unresolved 依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60602707/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com