2014-09-30 61 views
0

尝试在我的robotium android测试中启动WireMockServer时收到以下堆栈跟踪。 可能是Apache Http客户端的两个版本之间的冲突,但我还没有设法解决它。 有什么想法?在Android集成测试中使用WireMock时NoSuchMethodError

java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLSocketFactory.<init> 
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createSslSocketFactory(HttpClientFactory.java:110) 
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClientConnectionManagerWithSSLSettings(HttpClientFactory.java:88) 
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:54) 
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:70) 
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:58) 
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:96) 
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:140) 
at com.me.expertsystem.AcceptanceTest.setUp(AcceptanceTest.java:63) 

回答

0

恐怕截至2015年第一场比赛WireMock不支持Android。在github issue中正在跟踪支持的进展情况。

它在Roboelectric测试中工作得非常好,因为它们运行在标准的JVM中,因此您至少可以在测试方面使用它。

0

WireMock现在可在Android应用程序中工作,截至2016年1月。这是几个星期前从2.0-beta branch WireMock 2.0.8-beta修复。我已更新WireMock GitHub issue并创建了sample project showing it working

这里有你需要使用它的build.gradle依赖关系:

androidTestCompile("com.github.tomakehurst:wiremock:2.0.8-beta") { 
    //Allows us to use the Android version of Apache httpclient 
    exclude group: 'org.apache.httpcomponents', module: 'httpclient' 

    //Resolves the Duplicate Class Exception 
    //Error:Execution failed for task ':app:transformClassesWithJarMergingForDebugAndroidTest'. 
    //  > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/objectweb/asm/AnnotationVisitor.class 
    exclude group: 'asm', module: 'asm' 

    //Fixes conflict with Android's version 
    //Warning:Dependency org.json:json:20090211 is ignored for debugAndroidTest as it may be conflicting with the internal version provided by Android. 
    //In case of problem, please repackage with jarjar to change the class packages 
    exclude group: 'org.json', module: 'json' 
} 
androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5+' 
+1

感谢萨姆在Wiremock支持Android的更新!之后我转向了mockwebserver,但是了解一个人也可以在Android环境中依赖Wiremock(我倾向于通过mockwebserver),这很有用。 – 2016-01-09 18:04:49

+0

是的,我编写的示例项目(https://github.com/handstandsam/AndroidHttpMockingExamples)展示了两者,但WireMock功能更丰富。缺点是它确实伴随着Jetty等需求的大量依赖。但是,它们只需要包含在androidTestCompile中,而不是在您的发布APK中。另一个缺点是我必须启用multi-dex才能获得演示应用程序来运行它。但好处包括一把瑞士军刀,用于嘲笑和测试。 – 2016-01-10 03:38:52