Android Studio 2.3.1:
我的build.gradle
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 44
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
依赖{ //对于文件夹“test” testCompile'junit:junit:4.12' testCompile'org.powermock:powermock-api-mockito:1.6.6' testCompile'org.powermock:powermock-module-junit4:1.6.6' testCompile“org.mockito:mockito-core:+”
//------ Compile Dependencies ------
compile('com.squareup.retrofit2:retrofit:2.1.0') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
compile "com.squareup.okhttp3:logging-interceptor:3.5.0"
compile "org.slf4j:slf4j-api:1.7.12"
//------ Android Test Dependencies ------
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.google.guava', module: 'guava'
exclude group: 'junit', module: 'junit'
}
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'junit:junit:4.12'
//MockWebServer - Version 2.2 of mockwebserver doesn't work because of an issue, so forcing v2.1 - https://github.com/square/okhttp/issues/1069
androidTestCompile('com.squareup.okhttp3:mockwebserver:3.5.0') {
exclude module: 'okhttp'
}
//WireMock
androidTestCompile("com.github.tomakehurst:wiremock:2.5.0") {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'org.slf4j', module: 'slf4j-api'
exclude group: 'org.ow2.asm', module: 'asm'
exclude group: 'org.json', module: 'json'
}
androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
androidTestCompile 'commons-io:commons-io:2.4'
androidTestCompile "com.squareup.spoon:spoon-client:1.5.6"
}
我的电线和咖啡测试:
public class WireMockApplicationTestCase2 {
Logger logger = LoggerFactory.getLogger(WireMockApplicationTestCase2.class);
WireMockServer wireMockServer;
@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
private Context androidTestContext;
private Context applicationContext;
@Before
public void setUp() throws Exception {
this.androidTestContext = InstrumentationRegistry.getContext();
this.applicationContext = InstrumentationRegistry.getTargetContext().getApplicationContext();
wireMockServer = new WireMockServer(8089);
wireMockServer.start();
}
/**
* Test WireMock, but just the Http Call. Make sure the response matches the mock we want.
*/
@Test
public void testWiremockPlusOkHttp() throws IOException {
logger.debug("testWiremockPlusOkHttp>>>>>>>>>>>>>");
String serviceEndpoint = "http://127.0.0.1:" + 8089;
logger.debug("WireMock Endpoint: " + serviceEndpoint);
OkHttpClient okHttpClient = null;
String uri = "/hello-world";
Request request = new Request.Builder()
.url(serviceEndpoint + uri)
.build();
Response response = okHttpClient.newCall(request).execute();
String responseBodyStr = response.body().string();
logger.debug("Response Body>>>>>>>: " + responseBodyStr);
assertEquals("hello world", responseBodyStr);
}
}
但是当我在Android 7.0上开始测试时出现错误:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/impl/client/HttpClientBuilder;
at com.github.tomakehurst.wiremock.http.HttpClientFactory.createClient(HttpClientFactory.java:48)
at com.github.tomakehurst.wiremock.http.ProxyResponseRenderer.<init>(ProxyResponseRenderer.java:53)
at com.github.tomakehurst.wiremock.core.WireMockApp.buildStubRequestHandler(WireMockApp.java:124)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:71)
at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:108)
at com.myproject.android.transport.WireMockApplicationTestCase2.setUp(WireMockApplicationTestCase2.java:46)
at java.lang.reflect.Method.invoke(Native Method)