我正在使用Espresso进行黑盒测试 . 我从另一个帖子(Android Espresso how to write tests using apk?)跟随指南 . 但我的测试无法在DexPathList上找到类 . 错误消息是:
引起:java.lang.ClassNotFoundException:在路径上找不到类“com.twitter.android.DispatchActivity”:DexPathList [[zip file“/system/framework/android.test.runner.jar”,zip file“ /data/app/...test-2.apk“,zip文件”/data/app/...-2.apk"],nativeLibraryDirectories=[/data/app-lib/....test-2 ,/ data / app-lib /... 2,/ vendor / lib,/ system / lib]]
我正在测试的应用程序是Twitter,我没有源代码 . 所以我在Android Studio中创建了一个Android项目 .
清单文件如下所示:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="my.test">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.twitter.android">
</instrumentation>
</manifest>
测试类:
@RunWith(AndroidJUnit4.class)
public class Replayer {
private static final String CLASSNAME = "com.twitter.android.DispatchActivity";
private static Class<? extends Activity> activityClass;
static {
try {
activityClass = (Class<? extends Activity>) Class.forName(CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@Rule
public final ActivityTestRule<?> activityRule
= new ActivityTestRule<>(activityClass);
@Test
public void launchMain() {
Espresso.onView(ViewMatchers.withText("Log in")).perform(ViewActions.click());
}
}
我没有在DexPathList上看到目标应用程序 . 我错误配置了我的项目吗?
1 回答
好吧,经过近一个月的探索,我想出来了......目标应用程序需要在build.gradle文件中指定,如下所示:
gradle将自动生成android清单文件中的检测节点 . 请注意,在清单中手动添加android:targetPackage将不起作用 .