1

So, I have a few dependencies which I'm trying to mock for integration tests.

More specifically, I have a spring-cloud on my classpath which loads a Eureka client as part of its bootstrap configuration process, and tries to connect to discovery service.

Currently, I'm using a real server, but I'd like to mock it, before the client attempts to connect to it.

Unfortunately, as client is included in the library, I have no control over it, i.e. I can't add a @DependsOn annotation to it.

I was playing with various Spring lifecycle listeners and I think I should be able to get it to work, but was wondering if there is a cleaner approach to solve the problem.

jFrenetic
  • 5,384
  • 5
  • 42
  • 67
  • Maybe [this](http://stackoverflow.com/questions/34307529/strategy-for-unit-testing-a-spring-cloud-service) can help you. – ksokol Sep 26 '16 at 18:48

1 Answers1

0

You can disable eureka for you integration-test with the following property "eureka.client.enabled:false"


Another way is to produces a mock eureka bean with the @Primary annotation on it. This strategy is the opposite of setting @DependsOn.

Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.

CompilaMente
  • 359
  • 3
  • 5