wake-up-neo.com

Wie deaktiviere ich Eureka und Spring Cloud Config in einem WebMvcTest?

Ich spiele mit einer einfachen Spring Boot-Anwendung, die sich in Eureka registriert und Spring Cloud-Konfiguration zum Lesen einiger Eigenschaften verwendet.

Hier kommt's:

@SpringBootApplication
@EnableEurekaClient
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

In bootstrap.yml habe ich:

eureka:
    client:
        serviceUrl:
            defaultZone: http://registry-service:5051/eureka/

spring:
    application:
        name: product-service
    cloud:
        config:
            discovery:
                serviceId: config-service
                enabled: true
            fail-fast: true
            retry:
                initial-interval: 2000

Ich habe einen Rest-Controller, für den ich einen Test schreiben möchte. Der Test verwendet mockMVC. Ein vereinfachtes Beispiel folgt:

Der Controller:

@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(method = RequestMethod.POST, 
            value = "/api/products", 
            consumes = MediaType.APPLICATION_JSON_VALUE, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Product> createProduct(@RequestBody Product product) {

        ...
        return ...;
    }
}

Der Test:

@RunWith(SpringRunner.class)
@WebMvcTest(ProductController.class)
public class ProductControllerTest {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private ProductService mockProductService;


    @Test
    public void testProductFound() {
        ...
    }
}

Wenn ich diesen Test starte, schlägt der Test fehl, weil er versucht, die Mock-MVC automatisch zu konfigurieren. Vorher versucht er jedoch, die App in Eureka zu registrieren. Es wird auch versucht, Konfigurationen von einem Konfigurationsdienst abzurufen. Keiner dieser Dienste läuft natürlich während der Testausführung. Ich möchte sie deaktivieren.

Ich habe viele Dinge ausprobiert. Zum Beispiel habe ich versucht, den Testfall mit folgenden Anmerkungen zu versehen:

@TestPropertySource(locations="classpath:application-controller-tests.properties")

Innerhalb der Eigenschaften gibt es:

spring.cloud.discovery.enabled = false
spring.cloud.config.enabled = false

Theoretisch scheint dies zu funktionieren, aber der Test spuckt immer noch Ausnahmen wie diese aus:

017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2017-06-11 19:10:56.549  INFO 11508 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2017-06-11 19:10:57.683 ERROR 11508 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

com.Sun.jersey.api.client.ClientHandlerException: Java.net.ConnectException: Connection refused: connect
    at com.Sun.jersey.client.Apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.Java:187) ~[jersey-Apache-client4-1.19.1.jar:1.19.1]
    at com.Sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.Java:123) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.Java:27) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.Sun.jersey.api.client.Client.handle(Client.Java:652) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.Sun.jersey.api.client.WebResource.handle(WebResource.Java:682) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.Sun.jersey.api.client.WebResource.access$200(WebResource.Java:74) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.Sun.jersey.api.client.WebResource$Builder.get(WebResource.Java:509) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplicationsInternal(AbstractJerseyEurekaHttpClient.Java:194) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplications(AbstractJerseyEurekaHttpClient.Java:165) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.Java:137) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpClient.execute(MetricsCollectingEurekaHttpClient.Java:73) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.Java:134) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.Java:137) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.executeOnNewServer(RedirectingEurekaHttpClient.Java:118) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.Java:79) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.Java:134) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.Java:137) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.Java:119) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.Java:134) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.Java:137) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.Java:77) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.Java:134) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.Java:1030) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.Java:944) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.Java:444) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.Java:304) [eureka-client-1.4.12.jar:1.4.12]
    at org.springframework.cloud.netflix.eureka.CloudEurekaClient.<init>(CloudEurekaClient.Java:51) [spring-cloud-netflix-eureka-client-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.Java:162) [spring-cloud-netflix-eureka-client-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration$$EnhancerBySpringCGLIB$$bec65081.CGLIB$eurekaClient$0(<generated>) [spring-cloud-netflix-eureka-client-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration$$EnhancerBySpringCGLIB$$bec65081$$FastClassBySpringCGLIB$$97f6772b.invoke(<generated>) [spring-cloud-netflix-eureka-client-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.Java:228) [spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.Java:358) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$EurekaClientConfiguration$$EnhancerBySpringCGLIB$$bec65081.eurekaClient(<generated>) [spring-cloud-netflix-eureka-client-1.2.5.RELEASE.jar:1.2.5.RELEASE]
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
    at Sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_65]
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_65]
    at Java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_65]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.Java:162) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.Java:588) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.Java:1173) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.Java:1067) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:513) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:483) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:202) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.Java:208) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:1138) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:1066) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.Java:835) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.Java:741) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.Java:467) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.Java:1173) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.Java:1067) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:513) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:483) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:202) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.Java:208) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.Java:1138) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.Java:1066) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.Java:585) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:88) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.Java:366) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1264) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:553) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:483) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:306) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:230) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:302) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:197) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:761) [spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:866) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:542) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.Java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.Java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.Java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.Java:134) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.Java:138) [spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.Java:84) [spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
    at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.Java:62) [spring-cloud-context-1.1.7.RELEASE.jar:1.1.7.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.Java:167) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.Java:139) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.Java:122) [spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.Java:73) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.Java:54) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.Java:336) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.Java:307) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.Java:120) [spring-boot-test-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.Java:98) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.Java:116) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.Java:83) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.Java:117) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.Java:83) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.Java:44) [spring-boot-test-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.Java:230) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.Java:228) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.Java:287) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.Java:289) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:247) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.Java:94) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.Java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.Java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.Java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.Java:61) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.Java:70) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.Java:191) [spring-test-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.Eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.Java:86) [.cp/:na]
    at org.Eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.Java:38) [.cp/:na]
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:459) [.cp/:na]
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:678) [.cp/:na]
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:382) [.cp/:na]
    at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:192) [.cp/:na]
Caused by: Java.net.ConnectException: Connection refused: connect
    at Java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_65]
    at Java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_65]
    at Java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_65]
    at Java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_65]
    at Java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_65]
    at Java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_65]
    at Java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_65]
    at Java.net.Socket.connect(Unknown Source) ~[na:1.8.0_65]
    at org.Apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.Java:117) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.Java:177) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.Java:144) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.Java:131) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.Java:611) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:446) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.Java:882) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.Java:117) ~[httpclient-4.5.jar:4.5]
    at org.Apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.Java:55) ~[httpclient-4.5.jar:4.5]
    at com.Sun.jersey.client.Apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.Java:173) ~[jersey-Apache-client4-1.19.1.jar:1.19.1]
    ... 119 common frames omitted

Was ist der richtige Weg, um Eureka und die Cloud-Konfiguration beim Testen der Controller zu deaktivieren?

7
Lachezar Balev

Ich habe einen Weg gefunden, diese Tests mit einer relativ einfachen Testkonfiguration auszuführen. 

Die Angabe dieser Eigenschaften scheint in Ordnung zu sein:

#Disable discovery
spring.cloud.discovery.enabled = false

#Disable cloud config and config discovery
spring.cloud.config.discovery.enabled = false
spring.cloud.config.enabled = false

Eine Basistestklasse wird wie folgt kommentiert:

@TestPropertySource(locations = "classpath:application-controller-tests.properties")

Der Test selbst enthält die folgenden Anmerkungen:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = ProductController.class, secure = false)

Dann ist der Mock-MVC wie folgt verdrahtet und konfiguriert:

@Autowired
protected MockMvc mockMvc;

Ich bin mir immer noch nicht sicher, ob dies der "elegante" Weg ist oder ich sollte Profile verwenden oder ..

8
Lachezar Balev

Verschieben Sie Ihre Eureka-Konfiguration (@EnableEurekaClient) in eine separate Konfigurationsklasse und aktivieren Sie sie mit @ConditionalOnProperty.
Strategie für Unit-Tests eines Spring Cloud-Dienstes

0
Ilya Zinkovich