在春季进行集成测试时如何模拟Eureka?
我正在运行一个简单的Junit在Spring Boot中测试控制器.测试代码如下:
I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this:
@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 {
在application-integrationTest.properties中,我具有以下Eureka设置:
In the 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
开始进行junit测试时,我看到以下内容:
when a junit test started 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]
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]
测试通过了,那不是问题,但是我看到很多与Eureka有关的异常堆栈跟踪.问题是进行测试时是否有模拟eureka的方法或跳过它的其他方法?
The test passes, that is not the problem, but I see a lot of exception stack traces that has to do with Eureka. The questions is if there is a way to mock eureka or another way to skip brining it up when doing tests?
如果测试失败并且tst运行得更快,则好处将是更容易查看相关的堆栈跟踪
The benefit would be easier to see relevant stack traces if test would fail and tst would run much faster
另一种解决方案是在test/resources下的application.properties或application.yml文件中禁用Eureka Client
Another solution is to disable the Eureka Client in your application.properties or application.yml file under test/resources
applications.properties:
applications.properties:
eureka.client.enabled=false
application.yml:
application.yml:
eureka:
client:
enabled: false
eureka:
client:
enabled: false
这样做的好处是,无需为每个需要禁用Eureka Client的JUnit测试都包含系统属性.
This has the benefit of not needing to remeber to include the system property for every JUnit test that requires disabling the Eureka Client.