Restablecer Monica - M. Schröder:
He cambiado un inicio de primavera (2.1.4) que consume un servicio RestTemplate
para utilizar una @Qualifier
. Ahora mi prueba (con @RestClientTest
y @AutoConfigureWebClient
) produce un error porque no puede resolver el grano.
¿Cómo puedo solucionar esto?
El config:
@Bean
@Qualifier("eureka")
@LoadBalanced
RestTemplate eurekaRestTemplate() {
El servicio:
public ClarkClient(
@Qualifier("eureka") RestTemplate restTemplate, ClarkConfiguration configuration)
throws URISyntaxException {
La prueba:
@ExtendWith({SpringExtension.class, MockitoExtension.class})
@RestClientTest({CastorClient.class, CastorConfiguration.class})
@AutoConfigureWebClient(registerRestTemplate = true)
class CastorClientWebTest {
@Autowired
private CastorClient cut;
@Autowired
private MockRestServiceServer server;
El error:
[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)}
Restablecer Monica - M. Schröder:
Solución que funcionó para mí: @AutoConfigureWebClient
(sin (registerRestTemplate = true)
). En la @TestConfiguration
clase de crear un grano de RestTemplate
la derecha@Qualifier
@Bean
@Qualifier("eureka")
public RestTemplate eurekaRestTemplate() {
return new RestTemplate();
}
Inyectar que en la clase de prueba
@Autowired
@Qualifier("eureka")
private RestTemplate restTemplate;
Ahora necesitamos cable que en el MockRestServiceServer
. Lo hacemos a través de@BeforeEach
private MockRestServiceServer server;
@BeforeEach
void setUp () {
server = MockRestServiceServer.bindTo(restTemplate).build();
}