I have the the following Spring Boot application (using Eureka and Feign):
@SpringBootApplication
@EnableFeignClients
@EnableRabbit
@EnableDiscoveryClient
@EnableTransactionManagement(proxyTargetClass = true)
public class EventServiceApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(EventServiceApplication.class, args);
}
}
and the following test, annotated with @SpringJpaTest:
@RunWith(SpringRunner.class)
@DataJpaTest(showSql = true)
public class EventRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private EventRepository repository;
@Test
public void testPersist() {
this.entityManager.persist(new PhoneCall());
List<Event> list = this.repository.findAll();
assertEquals(1, list.size());
}
}
While running the test I receive the following error:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.netflix.discovery.EurekaClient] found for dependency [com.netflix.discovery.EurekaClient]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Full stacktrace here
Is there a way to solve this issue? I've seen that it is caused by @EnableFeignClients and @EnableDiscoveryClient annotations.
I had similar problem with @EnableDiscoveryClient.
I think that in such cases the easiest approach is put disabling information:
eureka:
client:
enabled: false
in src/test/resources/application.[properties|yml]
. Then during tests running this configuration has higher priority than src/main/resources/application.[properties|yml]
.
Finally I managed to solve my issue in the following way:
Added bootstrap.yml with the following contents:
eureka:
client:
enabled: false
spring:
cloud:
discovery:
enabled: false
config:
enabled: false
I written a test configuration and referenced it in the test:
@ContextConfiguration(classes = EventServiceApplicationTest.class)
where EventServiceApplicationTest is:
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass = true)
public class EventServiceApplicationTest {}
I don't know if there is an easiest way but this works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With