Android Studio 2.3.1,Java 8
在文件夹“androidTest”中,我使用Espresso单元测试编写了我的Android Instrumented。它工作正常。 这里代码:
package com.myproject.fragment;
import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.myproject.R;
import com.myproject.activity.MainActivity;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.myproject.CustomMatchers.withListSize;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.containsString;
@RunWith(AndroidJUnit4.class)
public class MyInstrumentedClassTest {
private Context context;
private static final String EXIST_SURNAME = "TestSurName";
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, false);
@Before
public void init() {
context = InstrumentationRegistry.getTargetContext();
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
mActivityRule.launchActivity(intent);
onView(withId(R.id.peopleTextView)).perform(click());
}
@Test
public void searchBySurname() {
onView(withId(R.id.searchView)).perform(click());
onView(withId(android.support.design.R.id.search_src_text))
.perform(typeText(EXIST_SURNAME))
.perform(closeSoftKeyboard());
onView(withId(R.id.containerNotEmptyListView)).check(matches(withListSize(1)));
onData(anything()).atPosition(0).onChildView(withText(containsString(EXIST_SURNAME)))
.check(matches(isDisplayed()));
}
}
这是我的build.gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
testBuildType "my_build_type"
}
dependencies {
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:1.10.19"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
当我运行仪表化Espresso单元测试“searchBySurname”时,它运行良好。 OK!
现在我想使用WireMock为http请求/响应创建单元测试。 所以我添加了新的依赖项。 这是我的新build.gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled = true
}
testBuildType "my_build_type"
}
dependencies {
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:1.10.19"
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
//WireMock
androidTestCompile("com.github.tomakehurst:wiremock:2.6.0") {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'org.ow2.asm', module: 'asm'
exclude group: 'org.json', module: 'json'
}
androidTestCompile('com.squareup.okhttp3:mockwebserver:3.5.0') {
exclude module: 'okhttp'
}
}
但现在当我运行仪表化的Espresso单元测试“searchBySurname”时,我收到错误:
Testing started at 17:20 ...
05/15 17:20:26: Launching searchOneContactBy...() (1)
$ adb push D:\dev\myproject\app\build\outputs\apk\app-my_build_type.apk /data/local/tmp/com.myproject
$ adb shell pm install -r "/data/local/tmp/com.myproject"
pkg: /data/local/tmp/com.myproject
Success
$ adb push D:\dev\myproject\app\build\outputs\apk\app-my_build_type-androidTest.apk /data/local/tmp/com.myproject.test
$ adb shell pm install -r "/data/local/tmp/com.myproject.test"
pkg: /data/local/tmp/com.myproject.test
Success
Running tests
$ adb shell am instrument -w -r -e debug false -e class com.myprojectd.fragment.MyInstrumentedClassTest#searchOneContactBySurname com.myproject.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Instrumentation run failed due to 'java.lang.NoClassDefFoundError'
Empty test suite.