自定义AndroidJUnitRunner上的ClassNotFoundException

时间:2017-03-03 12:01:14

标签: android android-espresso

当我运行连接的Android测试时,我遇到以下问题:

java.lang.RuntimeException: Unable to instantiate instrumentation
ComponentInfo{my.package.app.debug.test/my.package.app.ui.AppTestRunner}:
java.lang.ClassNotFoundException: Didn't find class "my.package.app.ui.AppTestRunner" on 
path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app
my.package.app.debug.test-1/base.apk", zip file "/data/app/my.package.app.debug-1
base.apk"],nativeLibraryDirectories=[/data/app/my.package.app.debug.test-1/lib/arm, /data
app/my.package.app.debug-1/lib/arm, /data/app/my.package.app.debug.test-1/base.apk!/lib
armeabi-v7a, /data/app/my.package.app.debug-1/base.apk!/lib/armeabi-v7a, /vendor/lib, 
system/lib]]

同样的问题,无论我是通过命令行还是通过Android Studio运行测试。

build.gradle的一部分

android {
    defaultConfig {
        testInstrumentationRunner "my.package.app.ui.AppTestRunner"
        multiDexEnabled true

// rest is missing

    }
}
dependencies {
    // Android Connected Tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support:multidex-instrumentation:1.0.1'
    androidTestCompile "com.android.support:support-annotations:${supportLibVersion}"
    androidTestCompile 'org.mockito:mockito-android:2.6.2'

// rest is missing
}

我的自定义测试跑步者

package my.package.app.ui;

import android.app.Application;
import android.content.Context;
import android.support.test.runner.AndroidJUnitRunner;

public class AppTestRunner extends AndroidJUnitRunner {
    @Override
    public Application newApplication(ClassLoader cl, String className, Context context)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return super.newApplication(cl, TestApplication.class.getName(), context);
    }
}

这是构建文件夹

中生成的测试清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package.app.debug.test" >

    <uses-sdk
        android:minSdkVersion="23"
        android:targetSdkVersion="23" />

    <instrumentation
        android:name="my.package.app.ui.AppTestRunner"
        android:functionalTest="false"
        android:handleProfiling="false"
        android:label="Tests for my.package.app.debug"
        android:targetPackage="my.package.app.debug" />

    <application>
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

我的应用AndroidManifest.xml(所有关于删除的活动)

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package=“my.package.app"
          android:installLocation="auto">

    <application
        android:name=".App"
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".ui.main.MainActivity"

       //... more stuff

    </application>

</manifest>

切换到testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"testInstrumentationRunner "android.support.multidex.MultiDexTestRunner"

时会发生同样的问题

任何关于如何解决问题的提示都将不胜感激!

1 个答案:

答案 0 :(得分:0)

我可以通过删除我正在创建的一个测试类来解决问题。令人遗憾的是,该项目正在编译,我得到的错误完全是误导。