【问题标题】:Blackbox Espresso test didn't find class on pathBlackbox Espresso 测试在路径上找不到类
【发布时间】:2016-02-09 01:29:30
【问题描述】:

我正在使用 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文件“/data/app/...test-2.apk”,压缩文件“/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 上看到目标应用程序。我是否错误配置了我的项目?

【问题讨论】:

    标签: android android-studio android-espresso black-box-testing


    【解决方案1】:

    嗯,经过将近一个月的探索,我想通了……需要在 build.gradle 文件中指定目标应用程序,如下所示:

    android {
        ....
        defaultConfig {
            ...
            applicationId "com.twitter.android"
        }
        ...
    }
    

    gradle 会在 android manifest 文件中自动生成检测节点。请注意,在清单中手动添加 android:targetPackage 将不起作用。

    【讨论】:

    • 根据DSL referencetestApplicationId表示“测试应用ID”,与被测应用的包名无关。
    • 另外,User Guide 表示:测试应用清单中instrumentation 节点的targetPackage 属性的值会自动填充被测试应用的包名,即使它通过 defaultConfig 和/或 Build Type 对象自定义。这是自动生成这部分清单的原因之一。
    • @JeremyKao 非常感谢您指出这一点。其实是打错了,应该是applicationId而不是testApplicatoinId。
    猜你喜欢
    • 1970-01-01
    • 2014-10-20
    • 2013-12-17
    • 1970-01-01
    • 2016-02-04
    • 2017-09-12
    • 2014-05-29
    • 2013-07-22
    相关资源
    最近更新 更多