@DataJpaTest does not work when using Eureka / Feign

I have 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());
    }
}

When starting the test, I get 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 any way to solve this problem? I saw that this is caused by @EnableFeignClients and @EnableDiscoveryClient annotations.

+4
source share
3 answers

Finally, I managed to solve my problem as follows:

  • Added bootstrap.yml file with the following contents:

    eureka:
      client:
        enabled: false
     spring:
       cloud:
         discovery:
           enabled: false
       config:
           enabled: false
    
  • I wrote a test configuration and indicated it in the test:

    @ContextConfiguration(classes = EventServiceApplicationTest.class)
    

    where is EventServiceApplicationTest:

    @SpringBootApplication
    @EnableTransactionManagement(proxyTargetClass = true)
    public class EventServiceApplicationTest {}
    

, , .

+3

@EnableDiscoveryClient.

, :

eureka:
  client:
    enabled: false

src/test/resources/application.[properties|yml]. , src/main/resources/application.[properties|yml].

+1

, , beans ApplicationContext, Feign ..

excludeFilters @DataJpaTest.

Spring.

Another option is to disable auto-configuration for Feign, etc. - in this case, you may find this answer useful: Disable protection for unit tests with spring boot

0
source

Source: https://habr.com/ru/post/1652468/


All Articles