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

Java Spring:如何将@RestClientTest与带有@Qualifier的RestTemplate结合起来?,java,spring,spring-boot,spring-boot-test,qualifiers,Java,Spring,Spring Boot,Spring Boot Test,Qualifiers,我已将使用restmplate的Spring Boot(2.1.4)服务更改为使用@限定符。现在,我的测试(使用@RestClientTest和@AutoConfigureWebClient)失败,因为它无法解析bean 我该如何解决这个问题 配置: @Bean @Qualifier("eureka") @LoadBalanced RestTemplate eurekaRestTemplate() { 服务: public ClarkClient( @Quali

我已将使用
restmplate
的Spring Boot(2.1.4)服务更改为使用
@限定符。现在,我的测试(使用
@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)
,因为它将为您创建一个RESTTemplatebean,而不是您使用的


如果在
CastorConfiguration
中声明了合格的RESTTemplatebean,请尝试使用
@Import(CastorConfiguration.class)
对我有效的解决方案:
@AutoConfigureWebClient
(不含
(registerRestTemplate=true)
)。 在
@TestConfiguration
类中,使用右
@Qualifier

@Bean
@Qualifier("eureka")
public RestTemplate eurekaRestTemplate() {
  return new RestTemplate();
}
将其注入测试类

@Autowired
@Qualifier("eureka")
private RestTemplate restTemplate;
现在我们需要将其连接到
MockRestServiceServer
。我们在每次之前通过
@beforeach

private MockRestServiceServer server;
@BeforeEach
  void setUp () {
    server = MockRestServiceServer.bindTo(restTemplate).build();
  }

@SpringBootTest
@Deadpool-Nope注释
CastorClientWebTest
,它会非常响亮地爆炸。config类是
restemplateconfig
,只是简单地导入没有帮助的内容。如何声明我自己的
RestTemplate
,以便与
@RestClientTest
一起使用?我在测试类中已经有了
@TestConfiguration
类。