apolloconfig / apollo

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
https://www.apolloconfig.com
Apache License 2.0
29.17k stars 10.2k forks source link

使用apollo-mockserver构建单元测试出错 #2513

Closed ankelen closed 5 years ago

ankelen commented 5 years ago

22:17:15.663 [main] DEBUG com.ctrip.framework.apollo.internals.RemoteConfigRepository - Loading config from http://localhost:63125/configs/ApolloNoAppIdPlaceHolder/default/application?ip=192.168.0.112 Exception in thread "MockWebServer" java.lang.NoSuchFieldError: H2_PRIOR_KNOWLEDGE at okhttp3.mockwebserver.MockWebServer$3.processConnection(MockWebServer.java:488) at okhttp3.mockwebserver.MockWebServer$3.execute(MockWebServer.java:443) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

nobodyiam commented 5 years ago

提供下重现步骤

ankelen commented 5 years ago
  1. 引入apollo-mockserver依赖
  2. 在test下新建resources文件夹,并在其下添加mockdata-application.properties文件(只有两行,即key1=value1和key2=value2)
  3. 使用ApolloMockServerApiTest.java中的测试代码

    public class Demo {
    @ClassRule
    public static EmbeddedApollo embeddedApollo = new EmbeddedApollo();
    
    @Test
    public void testGetProperty() throws Exception {
        Config applicationConfig = ConfigService.getAppConfig();
        assertEquals("value1", applicationConfig.getProperty("key1", null));
        assertEquals("value2", applicationConfig.getProperty("key2", null));
    }
    }
  4. 运行测试,发生上述错误
nobodyiam commented 5 years ago

看上去是你运行时的okhttp3的版本比较老所致,默认传递依赖的版本是3.11.0。

image

ankelen commented 5 years ago

是的,我看到apollo-mockserver中依赖的okhttp和mockwebserver都是3.11.0,但是我引用到项目后,okhttp版变成了3.8.1 image

nobodyiam commented 5 years ago

应该是被上层的dependencyManagement覆盖了,自己设置一下吧

ankelen commented 5 years ago

嗯,自己设置是没问题。主要是被SpringCloud中的dependencyManagement覆盖掉了