How to mock Eureka when running integration tests in Spring?

I am running a simple Junit Testing Controller in Spring Boot. The verification code is as follows:

@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {FrontControllerApplication.class}) @WebAppConfiguration @ComponentScan @IntegrationTest({"server.port:0", "eureka.client.registerWithEureka:false", "eureka.client.fetchRegistry:false"}) @ActiveProfiles("integrationTest") public class MyControllerIT { 

In application -integrationTest.properties, I have the following Eureka settings:

 ####### Eureka eureka.serviceUrl.default=http://localhost:8767/eureka/ eureka.printDeltaFullDiff=false eureka.client.refresh.interval=1 eureka.appinfo.replicate.interval=1 eureka.serviceUrlPollIntervalMs=1000 eureka.name=${spring.application.name} ####### Netflix Eureka ####### eureka.client.serviceUrl.defaultZone=http://localhost:8767/eureka/ eureka.client.instanceInfoReplicationIntervalSeconds=1 eureka.client.initialInstanceInfoReplicationIntervalSeconds=0 eureka.instance.virtualHostName=${spring.application.name} eureka.instance.preferIpAddress=true eureka.instance.initialStatus=DOWN eureka.instance.leaseRenewalIntervalInSeconds=3 eureka.instance.leaseExpirationDurationInSeconds=10 eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}} eureka.eurekaserver.connectionIdleTimeoutInSeconds=5 eureka.responseCacheAutoExpirationInSeconds=5 

when running the junit test, I see the following:

2015-09-16 16:46:03,905 ERROR localhost-startStop-1 com.netflix.discovery.DiscoveryClient Can't get a response from http://localhost:8767/eureka/apps/ Can't contact any eureka nodes - possibly a security group issue? com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:184) ~[jersey-apache-client4-1.11.jar:1.11]

The test passes, this is not a problem , but I see many traces of the exception stack that are related to Eureka. The question is, is there a way to mock eureka or another way to skip it when conducting tests?

The advantage will be easier to see the corresponding stack traces if the test fails and tst will run much faster

+6
source share
2 answers

Another solution is to disable the Eureka client in the application.properties or application.yml file in the test / resources

applications.properties:

eureka.client.enabled=false

application.yml:

eureka: client: enabled: false

It makes sense to not need to remember to enable the system property for every JUnit test requiring the Eureka client to be disabled.

+11
source

You can set the system property for eureka.client.enabled=false for tests.

If you use tests with gradle, you can do this:

 tasks.withType(Test) { systemProperty 'eureka.client.enabled', 'false' } 

If you run tests in the IDE, you also need to set the system property.

+9
source

All Articles