gpt4 book ai didi

java - Spring:如何将@RestClientTest 与带有@Qualifier 的RestTemplate 结合起来?

转载 作者:行者123 更新时间:2023-12-01 21:57:40 24 4
gpt4 key购买 nike

我已将使用 RestTemplate 的 Spring Boot (2.1.4) 服务更改为使用 @Qualifier。现在我的测试(使用 @RestClientTest@AutoConfigureWebClient)失败了,因为它无法解析 bean。

我该如何解决这个问题?

配置:

  @Bean
@Qualifier("eureka")
@LoadBalanced
RestTemplate eurekaRestTemplate() {

服务:

  public ClarkClient(
@Qualifier("eureka") RestTemplate restTemplate, ClarkConfiguration configuration)
throws URISyntaxException {

测试:

@ExtendWith({SpringExtension.class, MockitoExtension.class})
@RestClientTest({CastorClient.class, CastorConfiguration.class})
@AutoConfigureWebClient(registerRestTemplate = true)
class CastorClientWebTest {

@Autowired
private CastorClient cut;

@Autowired
private MockRestServiceServer server;

错误:

[2019-04-16T14:02:22,614] [WARN ] [            ....AnnotationConfigApplicationContext] [refresh 557] : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'castorClient' defined in file [/home/martinsc/java/routing/route-testing-batch-manager/out/production/classes/com/tyntec/routetesting/batchmanager/core/clients/CastorClient.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.web.client.RestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=eureka)}

最佳答案

您不应该使用 (registerRestTemplate = true),因为它会为您创建一个不是您使用的 RestTemplate bean。

如果在 CastorConfiguration 中声明了合格的 RestTemplate bean,请尝试使用 @Import(CastorConfiguration.class)

关于java - Spring:如何将@RestClientTest 与带有@Qualifier 的RestTemplate 结合起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55708446/

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