Instrumentation run failed due to 'Process crashed.' - android

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.

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?

No tests found when running instrumented tests with AndroidX

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'

Log during Android Unit Test in Intellij gives java.lang.RuntimeException: Stub

I am currently trying to migrate from Android Studio to Intellij.
The problem arises when I try to run my code through unit tests. A method in the code under test calls Log.e and an exception is thrown in Intellij IDE. The same code runs fine in Android Studio. Probably my project settings may configured incorrectly in Intellij. Any one knows where to look for the problem?
java.lang.RuntimeException: Stub!
at android.util.Log.e(Log.java:31)
My Intellij command when running the unit test.
D:\software\Java\jdk1.8.0_181\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576
"-javaagent:D:\software\IntelliJ IDEA Community Edition 2018.2.2\lib\idea_rt.jar=4006:D:\software\IntelliJ IDEA Community Edition 2019.2.2\bin"
-Dfile.encoding=UTF-8
-classpath
"D:\software\IntelliJ IDEA Community Edition 2018.2.2\lib\idea_rt.jar;
D:\software\IntelliJ IDEA Community Edition 2018.2.2\plugins\junit\lib\junit-rt.jar;
D:\software\IntelliJ IDEA Community Edition 2018.2.2\plugins\junit\lib\junit5-rt.jar;
D:\software\Android\sdk\platforms\android-28\android.jar;
D:\software\Android\sdk\platforms\android-28\data\res;
D:\software\Android\sdk\platforms\android-27\data\res;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\classes\debug;
D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy-agent\1.7.9\a6c65f9da7f467ee1f02ff2841ffd3155aee2fc9\byte-buddy-agent-1.7.9.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\runner-1.0.2.aar\d44dda11e7de1711127415e572906e3c\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.6\639033469776fd37c08358c6b92a4761feb2af4b\objenesis-2.6.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.0\edf3f7bfb84a7521d0599efa3b0113a0ee90f85\common-1.1.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\27.1.1\39ded76b5e1ce1c5b2688e1d25cdc20ecee32007\support-annotations-27.1.1.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-27.1.1.aar\87b44b5092008154f1171a969696f102\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\res;
D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy\1.7.9\51218a01a882c04d0aba8c028179cce488bbcb58\byte-buddy-1.7.9.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.0.aar\8d37b7feb2744759cd91b03ad94efb30\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\2.15.0\b84bfbbc29cd22c9529409627af6ea2897f4fa85\mockito-core-2.15.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.nhaarman\mockito-kotlin\1.5.0\25faa884f76375f76cdbd6651c4cebcde36d4117\mockito-kotlin-1.5.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.squareup\javawriter\2.1.1\67ff45d9ae02e583d0f9b3432a5ebbe05c30c966\javawriter-2.1.1.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.github.kotlin-graphics\kotlin-unsigned\v2.1\52408e5d299c5d1fb669188dae56fa5bb37cbc12\kotlin-unsigned-v2.1.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-27.1.1.aar\8949c790c8f1bf421289f293aa4e1cc2\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\res;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.1.1.aar\fb3bae07f3360874234d36f26caa5ebb\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.google.code.findbugs\jsr305\2.0.1\516c03b21d50a644d538de0f0369c620989cd8f0\jsr305-2.0.1.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\espresso-core-3.0.2.aar\369dec0c3f47cc8d75f961fddc94d0f7\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject\1\6975da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-fragment-27.1.1.aar\0464588052e7fe27a8be9d866a5cd81f\jars\classes.jar;
D:\software\Android\.gradle\caches\modul:es-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\4aa45f99c024430a3ccf7c0e257fb60a\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\monitor-1.0.2.aar\94d037806c32cc9e5663a80cfb787b67\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\res;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\espresso-idling-resource-3.0.2.aar\3b7ac85279e2aa734ba776a985e5273e\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-reflect\1.2.30\9758025a415ee400e1f90ff222bcfec247779133\kotlin-reflect-1.2.30.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.2.30\2dfac33f8b4e92c9dd1422cd286834701a6f6d6\kotlin-stdlib-1.2.30.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.0.aar\8c471aa13e8c6e412a4748be6e503cbc\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-integration\1.3\5de0c73fef18917cd85d0ab70bb23818685e4dfd\hamcrest-integration-1.3.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.0\8007981f7d7540d89cd18471b8e5dcd2b4f99167\common-1.1.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.2.30\ca12c47fc1e3a7316067b2a51e2f214745ebf8c5\kotlin-stdlib-jdk7-1.2.30.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\net.sf.kxml\kxml2\2.3.0\ccbc77a5fd907ef863c29f3596c6f54ffa4e9442\kxml2-2.3.0.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\f827adbdf97ad86b4140dcb5e1073a50\jars\classes.jar;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\sourceFolderJavaResources\test\debug;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\unitTestConfig\test\debug;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\tmp\kotlin-classes\debugUnitTest;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\intermediates\sourceFolderJavaResources\debug;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\tmp\kotlin-classes\debug;
D:\code\android\MyProjectAndroid\myProjectandroidgc\build\generated\mockable-android-26.default-values.v3.jar"
com.intellij.rt.execution.junit.JUnitStarter
-ideVersion5 #w#C:\Users\cjf12\AppData\Local\Temp\idea_working_dirs_junit.tmp #C:\Users\cjf12\AppData\Local\Temp\idea_junit.tmp
My Android studio command for running the unit test.
"D:\software\Android\Android Studio\jre\bin\java"
-ea
-Didea.test.cyclic.buffer.size=1048576
-Didea.launcher.port=3647
"-Didea.launcher.bin.path=D:\software\Android\Android Studio\bin"
-Dfile.encoding=UTF-8
-classpath
"D:\software\Android\Android Studio\lib\idea_rt.jar;
D:\software\Android\Android Studio\plugins\junit\lib\junit-rt.jar;
D:\software\Android\Android Studio\plugins\junit\lib\junit5-rt.jar;
D:\software\Android\sdk\platforms\android-27\data\res;
D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\classes\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\tmp\kotlin-classes\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\rs\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\resValues\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\unitTestConfig\test\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\tmp\kotlin-classes\debugUnitTest;
D:\code\android\myProjectAndroid\myprojectandroid\build\tmp\kotlin-classes\debugAndroidTest;
D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\rs\androidTest\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\generated\res\resValues\androidTest\debug;
D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy-agent\1.7.9\a6c65f9da7f467ee1f02ff2841ffd3155aee2fc9\byte-buddy-agent-1.7.9.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.6\639033469776fd37c08358c6b92a4761feb2af4b\objenesis-2.6.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.0\edf3f7bfb84a7521d0599efa3b0113a0ee90f85\common-1.1.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\27.1.1\39ded76b5e1ce1c5b2688e1d25cdc20ecee32007\support-annotations-27.1.1.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-27.1.1.aar\87b44b5092008154f1171a969696f102\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-compat-27.1.1.aar\5e64a504ec19504fa9b2687a61b079c4\res;
D:\software\Android\.gradle\caches\modules-2\files-2.1\net.bytebuddy\byte-buddy\1.7.9\51218a01a882c04d0aba8c028179cce488bbcb58\byte-buddy-1.7.9.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.0.aar\8d37b7feb2744759cd91b03ad94efb30\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\2.15.0\b84bfbbc29cd22c9529409627af6ea2897f4fa85\mockito-core-2.15.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.nhaarman\mockito-kotlin\1.5.0\25faa884f76375f76cdbd6651c4cebcde36d4117\mockito-kotlin-1.5.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\com.github.kotlin-graphics\kotlin-unsigned\v2.1\52408e5d299c5d1fb669188dae56fa5bb37cbc12\kotlin-unsigned-v2.1.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-27.1.1.aar\8949c790c8f1bf421289f293aa4e1cc2\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\jars\classes.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-ui-27.1.1.aar\1c49103dd8f3bd81aa06fd4de90258d6\res;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-core-utils-27.1.1.aar\fb3bae07f3360874234d36f26caa5ebb\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains\annotations\13.0\919f0dfe192fb4e063e7dacadee7f8bb9a2672a9\annotations-13.0.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\support-fragment-27.1.1.aar\0464588052e7fe27a8be9d866a5cd81f\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\4aa45f99c024430a3ccf7c0e257fb60a\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\res;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\appcompat-v7-27.1.1.aar\2668a362e92dc09e5e5a2ec0e09458ea\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-reflect\1.2.30\9758025a415ee400e1f90ff222bcfec247779133\kotlin-reflect-1.2.30.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib\1.2.30\2dfac33f8b4e92c9dd1422cd286834701a6f6d6\kotlin-stdlib-1.2.30.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.0.aar\8c471aa13e8c6e412a4748be6e503cbc\jars\classes.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.0\8007981f7d7540d89cd18471b8e5dcd2b4f99167\common-1.1.0.jar;
D:\software\Android\.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jdk7\1.2.30\ca12c47fc1e3a7316067b2a51e2f214745ebf8c5\kotlin-stdlib-jdk7-1.2.30.jar;
D:\software\Android\.gradle\caches\transforms-1\files-1.1\runtime-1.1.0.aar\f827adbdf97ad86b4140dcb5e1073a50\jars\classes.jar;
D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\sourceFolderJavaResources\test\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\intermediates\sourceFolderJavaResources\debug;
D:\code\android\myProjectAndroid\myprojectandroid\build\generated\mockable-android-27.default-values.v3.jar"
com.intellij.rt.execution.application.AppMainV2
com.intellij.rt.execution.junit.JUnitStarter
-ideVersion5
#w#C:\Users\cjf12\AppData\Local\Temp\idea_working_dirs_junit.tmp
#C:\Users\cjf12\AppData\Local\Temp\idea_junit.tmp
-socket3646
This is my build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "org.mycompany.myproject"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
main.java.srcDirs += 'src/main/java'
test.java.srcDirs += 'src/test/java'
}
buildTypes {
debug {
testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.github.kotlin-graphics:kotlin-unsigned:v2.1'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.15.0'
testImplementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation "com.nhaarman:mockito-kotlin:1.5.0"
testImplementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
testImplementation 'com.github.kotlin-graphics:kotlin-unsigned:v2.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
1- Add this to your build.gradle
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
testImplementation "org.powermock:powermock-api-mockito:${powerMockVersion}"
Checkout the supported versions of Mockito and PowerMock
For example if you use v1.9.5 for Mockito use v1.6.6 for PowerMock
2- Annotate your test class with this
#RunWith(PowerMockRunner.class)
#PrepareForTesting(Log.class)
public class YourTestClass {
And your test cases should run now.
Also as #Code-Apprentice said if you want to log something inside the testing class use System.out.println(). Do not use Android dependencies for testing, this just to bypass the logs inside the methods you are unit testing.
UPDATE
Adding this to your build.gradle will also help with no need for PowerMock (not highly recommend). Please refer to this answer
android {
//...
testOptions {
unitTests.returnDefaultValues = true
}
}
When running unit tests, no methods from the Android API are available. The error "java.lang.RuntimeException: Stub!" occurs when you try to call any of these functions, including Log.e(). If you need output, use the standard System.out.println() instead. Or use the IntelliJ debugger.

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'

Categories

Resources