我在将Spring-cloud-contract与我的服务集成在消费者方面存在问题。在我的服务中,我使用已经假装(调用其他服务)和zuul(用于路由)来自spring-cloud。当我尝试运行使用@AutoConfigureStubRunner注释的测试时,会出现问题。这是我的简单课程:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureStubRunner(ids = {"group:artifact:+:stubs:8080"}, workOffline = true)
public class WithContractTests {
@Test
public void test() {}
}
在输出中,我可以看到存根已正确启动:
o.s.c.c.stubrunner.StubRunnerExecutor : All stubs are now running RunningStubs [namesAndPorts={group:artifact:1.0.0-SNAPSHOT:stubs=8080}]
然后在创建spring上下文时出现此错误:
Caused by: java.lang.UnsupportedOperationException: null
at java.util.AbstractList.add(AbstractList.java:148) ~[na:1.8.0_91]
at java.util.AbstractList.add(AbstractList.java:108) ~[na:1.8.0_91]
at java.util.AbstractCollection.addAll(AbstractCollection.java:344) ~[na:1.8.0_91]
at org.springframework.cloud.contract.stubrunner.spring.cloud.StubRunnerDiscoveryClient.getServices(StubRunnerDiscoveryClient.java:139) ~[spring-cloud-contract-stub-runner-1.0.0.RC1.jar:1.0.0.RC1]
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.locateRoutes(DiscoveryClientRouteLocator.java:105) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.locateRoutes(DiscoveryClientRouteLocator.java:43) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator.doRefresh(SimpleRouteLocator.java:152) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.refresh(DiscoveryClientRouteLocator.java:155) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping.setDirty(ZuulHandlerMapping.java:61) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.zuul.ZuulConfiguration$ZuulRefreshListener.onApplicationEvent(ZuulConfiguration.java:180) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:388) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:110) ~[spring-cloud-context-1.1.2.RELEASE.jar:1.1.2.RELEASE]
at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:79) ~[spring-cloud-context-1.1.2.RELEASE.jar:1.1.2.RELEASE]
at org.springframework.cloud.context.named.NamedContextFactory.getInstance(NamedContextFactory.java:115) ~[spring-cloud-context-1.1.2.RELEASE.jar:1.1.2.RELEASE]
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getOptional(FeignClientFactoryBean.java:139) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.feign(FeignClientFactoryBean.java:84) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:157) ~[spring-cloud-netflix-core-1.2.0.RC1.jar:1.2.0.RC1]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
有趣的是,spring-cloud-contract示例中出现了同样的错误: 将Zuul添加到项目后https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples/standalone/dsl/http-client。通过添加Zuul我的意思是添加 spring-cloud-starter-zuul 作为依赖项和 @EnableZuulProxy 注释。之后,我们可以在执行测试时看到以下错误:
java.lang.UnsupportedOperationException: null
at java.util.AbstractList.add(AbstractList.java:148) ~[na:1.8.0_91]
at java.util.AbstractList.add(AbstractList.java:108) ~[na:1.8.0_91]
at java.util.AbstractCollection.addAll(AbstractCollection.java:344) ~[na:1.8.0_91]
at org.springframework.cloud.contract.stubrunner.spring.cloud.StubRunnerDiscoveryClient.getServices(StubRunnerDiscoveryClient.java:139) ~[spring-cloud-contract-stub-runner-1.0.0.BUILD-20160922.090756-647.jar:1.0.0.BUILD-SNAPSHOT]
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.locateRoutes(DiscoveryClientRouteLocator.java:105) ~[spring-cloud-netflix-core-1.2.0.BUILD-20160922.110240-890.jar:1.2.0.BUILD-SNAPSHOT]
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.locateRoutes(DiscoveryClientRouteLocator.java:43) ~[spring-cloud-netflix-core-1.2.0.BUILD-20160922.110240-890.jar:1.2.0.BUILD-SNAPSHOT]
at org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator.doRefresh(SimpleRouteLocator.java:152) ~[spring-cloud-netflix-core-1.2.0.BUILD-20160922.110240-890.jar:1.2.0.BUILD-SNAPSHOT]
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.refresh(DiscoveryClientRouteLocator.java:155) ~[spring-cloud-netflix-core-1.2.0.BUILD-20160922.110240-890.jar:1.2.0.BUILD-SNAPSHOT]
at org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping.setDirty(ZuulHandlerMapping.java:61) ~[spring-cloud-netflix-core-1.2.0.BUILD-20160922.110240-890.jar:1.2.0.BUILD-SNAPSHOT]
at org.springframework.cloud.netflix.zuul.ZuulConfiguration$ZuulRefreshListener.onApplicationEvent(ZuulConfiguration.java:180) ~[spring-cloud-netflix-core-1.2.0.BUILD-20160922.110240-890.jar:1.2.0.BUILD-SNAPSHOT]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-1.4.0.BUILD-20160728.175440-591.jar:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) ~[spring-boot-1.4.0.BUILD-20160728.175440-591.jar:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) ~[spring-boot-1.4.0.BUILD-20160728.175440-591.jar:1.4.0.BUILD-SNAPSHOT]
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:111) [spring-boot-test-1.4.0.BUILD-20160728.175440-528.jar:1.4.0.BUILD-SNAPSHOT]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.2.RELEASE.jar:4.3.2.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.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.2.RELEASE.jar:4.3.2.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.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.2.RELEASE.jar:4.3.2.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.2.RELEASE.jar:4.3.2.RELEASE]
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) [junit-4.12.jar:4.12]
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) [junit-rt.jar:na]
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:253) [junit-rt.jar:na]
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84) [junit-rt.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
将Zuul与Stub runner一起使用时,是否需要一些额外的配置?
答案 0 :(得分:0)
哦,小伙子,这是我的错。你能在合同中提出问题吗?到目前为止,您必须要覆盖现有的类,它不会将服务器添加到从委托返回的列表中,而是创建添加服务器的新列表。我没有对Zuul进行测试:/
这里已经修复了(希望如此) - https://github.com/spring-cloud/spring-cloud-contract/issues/82