我正在尝试为Android应用运行Espresso测试。它在硬件设备上运行良好。当我在新创建的 AVD模拟器上运行时,失败,如下所示:
:ExampleApp:connectedDebugAndroidTest
com.example.MainFragmentTest > initializationError[Nexus_5_API_19(AVD) - 4.4.2]
FAILED
java.lang.NoClassDefFoundError: com/example/MainActivity
at java.lang.Class.getDeclaredFields(Native Method)
:ExampleApp:connectedDebugAndroidTest FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':ExampleApp:connectedDebugAndroidTest'.
> There were failing tests. See the report at:
file:///home/user/work/ExampleApp/build/reports/androidTests/connected/index.html
HTML报告包含以下堆栈跟踪:
java.lang.NoClassDefFoundError: com/example/MainActivity
at java.lang.Class.getDeclaredFields(Native Method)
at java.lang.Class.getDeclaredFields(Class.java:610)
at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:38)
at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:57)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:45)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:701)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:664)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:329)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:226)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.MainActivity" on path: DexPathList[[zip file
"/system/framework/android.test.runner.jar", zip file
"/data/app/com.example.debug.test-1.apk", zip file
"/data/app/com.example.debug-1.apk"],
nativeLibraryDirectories=[/data/app-lib/com.example.debug.test-1,
/data/app-lib/com.example.debug-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 28 more
我基本上遵循espresso/BasicSample/ChangeTextBehaviorTest.java中的结构。此测试在模拟器上运行!
对于我的测试,当我使用以下代码时,我可以将测试结果显示已经失败:
@RunWith(AndroidJUnit4.class)
public class MainFragmentTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
}
以下是完整的测试类:
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import com.example.R;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class MainFragmentTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
MainActivity mMainActivity;
MainFragment mMainFragment;
@Before
public void setUp() {
mMainActivity = mActivityRule.getActivity();
mMainFragment = (MainFragment) mMainActivity
.getSupportFragmentManager()
.findFragmentByTag(MainFragment.FRAGMENT_TAG);
}
@Test
public void testHeadline() {
onView(withId(R.id.headline)).check(matches(withText(R.string.headline)));
}
}
我目前正在使用以下工具链:
答案 0 :(得分:5)
经过一些试验和错误后,我通过这个调整得到了我的运行:
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
更改为
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.android.support', module: 'support-v4'
}
espresso-contrib中包含的内容阻止了support-v4
的正确版本被包含在测试包中,并导致类加载器失败。
答案 1 :(得分:2)
经过一些试验和错误后,我意识到特定AVD仿真器时出现错误,而其他测试执行成功。我在问题中没有提到的一个重要的注意事项是我使用运行测试的应用程序使用Google Play服务。因此,我需要选择提供框架的模拟器。
答案 2 :(得分:2)
作为安装问题的解决方法,您还可以手动运行检测:
<强> 1。生成目标&amp;测试apk
gradle connectedAndroidTest
安装apk会失败,但仍然会将其生成为输出。
<强> 2。安装测试&amp;手动定位apk
adb install XXX-debug-androidTest-unaligned.apk
adb install XXX-debug.apk
第3。手动触发入侵
adb shell am instrument -w -e your.package.test/.common.Instrumentation
这将显示所有已安装的仪器:
adb shell pm list instrumentation
有关详细信息,请查看official instrumentation documentation。