0

I'm trying to use the new @JdbcTest annotation in Spring boot 1.5.0.RC1.

My app uses Eureka discovery ie I have

compile('org.springframework.cloud:spring-cloud-starter-eureka')

in my build.gradle and

@EnableDiscoveryClient

on my main Spring Boot class

When I try to use @JdbcTest to test a JdbcTemplate based DAO I get this error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method eurekaHealthIndicator in org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration$EurekaHealthIndicatorConfiguration required a bean of type 'com.netflix.discovery.EurekaClient' that could not be found.


Action:

Consider defining a bean of type 'com.netflix.discovery.EurekaClient' in your configuration.

It looks like the auto configuration is loading part of the Eureka configuration when it should only load JDBC related beans.

If I add

@TestPropertySource(properties={"eureka.client.enabled=false"})

to the test the problem goes away, but I think @JdbcTest should be making sure already that only relevant beans are loaded.

Am I missing something or is this a problem with the new @JdbcTest, or maybe Spring Cloud Eureka?

David Geary
  • 1,756
  • 2
  • 14
  • 23
  • Try to add these options to your test props: `eureka.client.register-with-eureka=false` and `eureka.client.fetch-registry=false` and `endpoints.health.enabled=false` – Strelok Jan 11 '17 at 04:10
  • As I said it works when I add eureka.client.enabled=false. But the idea of @JdbcTest is that it is only concerned with the JDBC layer of the application. I shouldn't have to start working out which extra dependencies I have in my overall application are causing problems and how to turn them off. – David Geary Jan 11 '17 at 10:12

2 Answers2

1

Your @SpringBootApplication has @ EnableDiscoveryClient on it. When you use a slice annotation (such as @JdbcTest), Spring Boot finds the context to use by looking at the package of your test for a @SpringBootConfiguration. If it does not find one, it looks in the parent package, etc. With a sensible package structure and no further customization, your tests use your @SpringBootApplication as the root context.

So, that configuration class (and only that one) is processed an @EnableDiscoveryClient is also obviously processed. In your case, this has an additional side effect: every single test now requires the Eureka Client.

TL;DR never put such annotation on your application. And only put it if you actually need it. You could define a @Configuration class next to your Spring Boot app for those.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • "never put such annotation on your application" - This is new to me, most examples seem to do this including https://spring.io/guides/gs/service-registration-and-discovery and the 'Quick Start' at http://cloud.spring.io/spring-cloud-netflix/, so I'm not sure why you should not do this (and I would not have the annotation unless I needed it!). I suppose I could move the annotation, but really seems to be moving your core config around in order to suit the implementation of the test slice functionality? – David Geary Jan 16 '17 at 12:42
  • I told you that in the context of slicing. If you don't want your tests to use your app as the source then create a dedicated `@SpringBootConfiguration`. Try to take a step back, you can't have it both ways. – Stephane Nicoll Jan 16 '17 at 18:40
1

@David I had similar issue and fixed by adding the following to my application.yml file

eureka:
  client:
    enabled: false
Arthur Kazemi
  • 960
  • 1
  • 10
  • 11