I wanted to run the following test:
package com.xxx.yyy;
import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.xxx.yyy", appContext.getPackageName());
}
}
But I get the error in the console:
$ adb shell am instrument -w -r -e debug false -e class 'com.xxx.yyy.ExampleInstrumentedTest' com.xxx.yyy.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.
I can not figgure out why its not working.
Here is my gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'org.greenrobot.greendao' // das kann dann später weg
//apply plugin: 'kotlin-kapt' // if using Kotlin
//apply plugin: 'io.objectbox'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.xxx.yyy"
minSdkVersion 21
targetSdkVersion 28
versionCode 130
versionName "1.3.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
greendao {
schemaVersion 4
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
implementation 'org.greenrobot:greendao:3.2.2'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-ads:15.0.1'
testImplementation 'junit:junit:4.12'
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
}
apply plugin: 'com.google.gms.google-services'
Any suggestions?
答案 0 :(得分:32)
我自己找到了解决方案。
我已更新为AndroidX,因此还需要从以下位置更新build.gradle
文件:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
到
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
答案 1 :(得分:6)
我得到了错误:
Test running failed: Instrumentation run failed due to 'Process crashed.'
就我而言,android测试控制台仅显示以上错误,没有任何细节
但是,在logcat中,它显示了错误的详细信息,就我而言,这是因为我忘记在AndroidManifest.xml中添加admob app_id
如此
有关详细信息,请参见logcat
有关详细信息,请参见logcat
有关详细信息,请参见logcat
答案 2 :(得分:3)
从以下位置更新JUnit4后,我得到了相同的错误:
androidTestImplementation 'junit:junit:4.12
到
androidTestImplementation 'junit:junit:4.13
当我降级到以下级别时,错误消失了:
androidTestImplementation 'junit:junit:4.12
答案 3 :(得分:0)
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.1'
除了androidx.test.runner.AndroidJunitRunner配置外,还请检查依赖项。上面的代码对我有用。
答案 4 :(得分:0)
所以我有相同的症状,但是在进行所有这些更改之后,我发现我的项目build.gradle文件中的以下配置是个问题
buildTypes{
debug {
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
似乎在升级到androidx之后,minifyEnable和rinkleResources的行为有所不同,可能早些发生,但我现在才意识到。注释掉这些行以解决我的“未找到测试”问题。
buildTypes{
debug {
debuggable true
//minifyEnabled true
//shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
答案 5 :(得分:0)
出现此错误是因为我在build.gradle中将testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
更改为testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4"
。在LogCat中,我看到了:
java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{com.example.debug.test/androidx.test.ext.junit.runners.AndroidJUnit4}: java.lang.ClassNotFoundException: Didn't find class "androidx.test.ext.junit.runners.AndroidJUnit4" on path: DexPathList[[zip file ...