No tests found when running instrumented tests with AndroidX - android

I'm trying to run the standard ExampleInstrumentedTest in my Android project (which uses AndroidX), but get "No tests found" error instead. I've looked through the other questions and the documentation and I'm pretty sure I've done everything right, but maybe I'm overlooking anything?
Here is my app's build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Gradle automatically adds 'android.test.runner' as a dependency.
useLibrary 'android.test.runner'
useLibrary 'android.test.base'
useLibrary 'android.test.mock'
}
repositories {
mavenCentral()
maven {
url("https://oss.sonatype.org/content/repositories/snapshots")
}
maven { url 'https://jitpack.io' }
}
dependencies {
// Core library
androidTestImplementation 'androidx.test:core:1.0.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
// More dependencies...
and ExampleInstrumentedTest.java:
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
Context appContext = ApplicationProvider.getApplicationContext();
assertEquals("XXX", appContext.getPackageName());
}
}
when I run the code, I get "No tests found".

I replaced -
testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"
with -
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

I needed to replace my runner
"androidx.test.ext.junit.runners.AndroidJUnit4"
with
"androidx.test.runner.AndroidJUnitRunner"

Look for app/src/androidTest/AndroidManifest.xml
change android:name to androidx.multidex.MultiDexApplication
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="android.support.multidex.MultiDexApplication">
To
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="androidx.multidex.MultiDexApplication">

I got the "No tests found" issue fixed by following the instructions of Jim Andreas here: https://github.com/googlecodelabs/android-testing/issues/27
"If I click on the package and not the test, then AS does find the test and executes it. "
In that way, the test was run as "Android Instrumented Tests" rather than "Android JUnit". I think that is the root cause of the issue.

Its a silly mistake, but for me I forgot to add
#Test
above my test function.

For me this was caused by running Instrumented tests on an emulator with API 30 (Android 'R').
When I switch to another emulator with API 29 (Android 'Q') the error goes away.

Be sure to have this
android {
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
}
and this in dependencies
androidTestImplementation 'androidx.test:runner:1.2.0'

Related

Untiy android plugin class not found exception

I'm writing an android plugin for unity to be able to check if notifications are enabled for the game. I have one java class with a method for checking if notifications are enabled. When i build the plugin and then the .apk with unity everything works fine. But after installation, when calling the mehthod i get the following exception:
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/NotificationManagerCompat;
The Java class
package com.example.plugin;
import android.app.Activity;
import androidx.core.app.NotificationManagerCompat;
public class NotificationPlugin {
public static boolean areNotificationEnabled(Activity unityActivity) {
return NotificationManagerCompat.from(unityActivity).areNotificationsEnabled();
}
}
buld.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
gradle.properties
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
Haven't found a solution anywhere else yet. Thank in advance. It's probably a super simple fix that i miss.
Update
Custom gradle.properties in unity set with android.enableJetifier and android.useAndroidX set to true
Jetifier was enabled in unity
Androidx.Core libary was added to the dependencies of the gradle.build file of the plugin
None of the above solved the issue
Solution
As Hamid Yusifli suggested in his answer a custom gradle build template needs to be enabled (Project Settings>Player>Publishing Settings>Custom Main Gralde Template) and the dependencies for the libary need to be added (implementation 'androidx.core:core:1.5.0' in my case). This solved the issue.
So, seems like in your case unity ignores your library gradle dependencies,
there are many different reasons why this could happen. To force unity to include missing dependencies you must provide a custom Gradle build template and add your dependencies in that file.

Cannot resolve symbol 'test' in Android Support library

I'm just starting to play around with Android and I'm having trouble with some androidTest examples that I've come across. Basically, I get a Cannot resolve symbol 'test' error message from Android Studio in the import statements for the InstrumentationRegistry and AndroidJUnit4 classes of my instrumented test file:
ExampleInstrumentedTest.java
package com.example.androidtestexample;
import android.content.Context;
import android.support.test.platform.app.InstrumentationRegistry;
import android.support.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.androidtestexample", appContext.getPackageName());
}
}
As a consequence, the AndroidJUnit4 symbol of the #RunWith statement can't be resolved either and when I try to run the instrumentation test I get an 'Edit configuration' window with an 'Instrumentation runner class not specified' error.
I'm using Android Studio 3.5.3, but this example uses 'com.android.tools.build:gradle:2.2.3'. The app gradle.build is as follows (I had to add the aaptOptions statements as suggested here to get the minimum reproducible example working):
build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.example.androidtestexample"
minSdkVersion 22
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
As suggested in this question, I have double-checked that the debug build variant was selected and that the instrumentation test source file was in the src/androidTest/java/ folder.
I understand that this example is quite outdated and, from another of the answers to the aforementioned question and from this page, that the android.support.test package is deprecated and AndroidX should be used instead. However, from this page I also understand that it should still be possible to use the Android Support library for historical artifacts, hence my question:
Is there a way for me to fix this problem and run such old examples with the android.support.test package without needing to migrate them to AndroidX?

Instrumentation run failed due to 'Process crashed.'

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 Testing documentation
*/
#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?
Found the solution by myself.
I updated to AndroidX, therefor I needed also to update my build.gradle file from:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
to
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
I got the error:
Test running failed: Instrumentation run failed due to 'Process crashed.'
In my case, the android test console only showed the error above without any details.
But in the logcat, the full error was shown. In my case, I forgot to add the AdMob app_id in AndroidManifest.xml
So always remember to check the logcat for more error details!
androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
androidTestImplementation "androidx.test:runner:1.3.0"
androidTestImplementation "androidx.test:core:1.3.0"
androidTestImplementation "androidx.test.ext:junit:1.1.2"
androidTestImplementation "androidx.test:rules:1.3.0"
except for the androidx.test.runner.AndroidJunitRunner config, please also check the dependency. The above code is worked for me.
So I had the same symptoms but after making all these changes I found that the following config in my project build.gradle file that was an issue
buildTypes{
debug {
debuggable true
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
seems that minifyEnable and shrinkResources are behaving differently after the upgrade to androidx, could have happened earlier but I just realized it now. Commenting out the lines fixed my No Tests Found Issues.
buildTypes{
debug {
debuggable true
//minifyEnabled true
//shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
I got the same error after updating JUnit4 from:
androidTestImplementation 'junit:junit:4.12
to
androidTestImplementation 'junit:junit:4.13
The error went away when I downgraded back to:
androidTestImplementation 'junit:junit:4.12
Generally you'd want the whole suite to match at once: https://developer.android.com/jetpack/androidx/releases/test
So for example, the latest as of writing is
Core 1.4.0
Espresso 3.4.0
Intents 3.4.0
JUnit 1.1.3
Monitor 1.4.0
Orchestrator 1.4.0
Runner 1.4.0
Rules 1.4.0
Truth 1.4.0
Test Services 1.4.0
This error appeared because I changed testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" to testInstrumentationRunner "androidx.test.ext.junit.runners.AndroidJUnit4" in build.gradle. In LogCat I saw:
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 ...
For me, I was working with runtime permission and revoking the permission (tried adb commands and uiAutomation.revokeRuntimePermission) caused this issue, as an alternate approach, I used Android Test Orchestrator, that seems to be working great so far.

Android Studio Junit tests package org.junit does not exist

I'm trying to implement a test class in Android Studio to make some test on a DBAdapter. Since I need to run the test on my mobile phone in order to use the database, I have created an Instrumentation Unit Test (cause I've tried to do it just with a Unit test but I need to use the database and so, and those are only run locally).
The problem is that when I try to run the test class, using my mobile phone as running device, the compiler throws the following error:
error: package org.junit does not exist
I've been looking for a solutione, but I found none.
This is my test class (just the skeleton):
import org.junit.Test;
import android.support.test.runner.AndroidJUnit4;
import static org.junit.Assert.*;
public class DbAdapterTest {
#Test
public void testCreateSeries() throws Exception {
}
}
And this one is the build.gradle script:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.w2w.whattowatch"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
I also have another issue. As you can see, I also imported this:
import android.support.test.runner.AndroidJUnit4;
But, even before running, it says of "runner" that "cannot resolve the symbol". I've added the TestInstrumentationRunner on build.gradle, but still not working.
OK, I solve it so, this is the solution that worked for me.
I didn't have this dependences, so I add them to the build.gradle script:
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

Android Studio cannot find JUnit package

I just can't seem to get a test file to find Junit4. I've been trying forever and I'm just frustrated. It says it can't find symbol junit under org. Everything I've googled (for days), and even the android docs say this is how to do it. What am I doing wrong?
Here is my test class
import org.junit.Test;
import android.app.Application;
public class ApplicationTest {
// #Test
public ApplicationTest() {
}
}
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
//buildToolsVersion "21.1.2"
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "stuff.morestuffs"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
compile files('libs/gson-2.3.1.jar')
testCompile 'junit:junit:4.12'
}
heres my project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
I'm using Android Studio 1.4
For unit tests (JVM tests) the root source directory should be test, not androidTest as for Android instrument tests.
Under "Build Variants", I had "Android Instrument Tests" selected. When I changed this to "Unit Tests", Android Studio correctly recognized my file.
I solved this problem when I added following dependency to build.gradle:
androidTestCompile 'junit:junit:4.12'
If you have to test more than some separate methods and you want to test your Activity or Fragments you have to add it.

Categories

Resources