Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@DataJpaTest fails when using Eureka / Feign

Tags:

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.

like image 634
Alessandro Dionisi Avatar asked Aug 25 '16 12:08

Alessandro Dionisi


2 Answers

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].

like image 39
Ziemowit Stolarczyk Avatar answered Sep 25 '22 16:09

Ziemowit Stolarczyk


Finally I managed to solve my issue in the following way:

  1. Added bootstrap.yml with the following contents:

    eureka:
      client:
        enabled: false
     spring:
       cloud:
         discovery:
           enabled: false
       config:
           enabled: false
    
  2. 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.

like image 90
Alessandro Dionisi Avatar answered Sep 22 '22 16:09

Alessandro Dionisi