5

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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Alessandro Dionisi
  • 2,494
  • 4
  • 33
  • 37

4 Answers4

3

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.

Alessandro Dionisi
  • 2,494
  • 4
  • 33
  • 37
1

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

Ziemowit Stolarczyk
  • 1,014
  • 2
  • 11
  • 26
0

Since I assume you're only intending to test your repository layer, you could filter out the unneeded beans from your ApplicationContext related to Feign, etc.

You can configure an exclude filter via the excludeFilters attribute of @DataJpaTest.

Details on filters can be found in the Spring Reference Manual.

Another option would be to disable auto-configuration for Feign, etc. -- in which case you might find this answer useful: Disable security for unit tests with spring boot

Community
  • 1
  • 1
Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • Do you have an example? – Alessandro Dionisi Aug 26 '16 at 15:27
  • I've updated my answer with additional links and a comment regarding disabling auto-configuration. – Sam Brannen Aug 26 '16 at 15:34
  • In fact @DataJpaTest automatically disables autoconfiguration, as stated [here](http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.html) so disabling Feign, Eureka is not needed. I updated the post with the full stack trace. Thanks for your help in any case. – Alessandro Dionisi Aug 29 '16 at 08:10
  • Anyway I managed to disable Eureka setting property `eureka.client.enabled=false` but I'm not able to disable Feign, event using `@DataJpaTest(showSql = true, excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = FeignAutoConfiguration.class))` – Alessandro Dionisi Aug 29 '16 at 08:26
0

The easiest way is to add the following configuration in your test class:

@RunWith(SpringRunner.class)
@TestPropertySource(properties={"eureka.client.enabled=false"})
@DataJpaTest(showSql = true)

public class BankRepositoryTest {

}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135