I'm trying to get the androidTest (instrumentation tests) working for the openScale Android app using the following build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.health.openscale"
testApplicationId "com.health.openscale.test"
minSdkVersion 18
targetSdkVersion 22 // don't set target sdk > 22 otherwise bluetooth le discovery need permission to ACCESS_COARSE_LOCATION
versionCode 22
versionName "1.7 (beta)"
javaCompileOptions {
annotationProcessorOptions { arguments = ["room.schemaLocation":"$projectDir/schemas".toString()] }
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
ext {
supportLibVersion = '27.0.2'
}
dependencies {
implementation "com.android.support:design:${supportLibVersion}"
implementation "com.android.support:support-v4:${supportLibVersion}"
implementation "com.android.support:appcompat-v7:${supportLibVersion}"
// HelloCharts
implementation 'com.github.lecho:hellocharts-library:1.5.8#aar'
// Simple CSV
implementation 'com.j256.simplecsv:simplecsv:2.2'
// CustomActivityOnCrash
implementation 'cat.ereza:customactivityoncrash:2.2.0'
// Room
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
androidTestImplementation 'android.arch.persistence.room:testing:1.0.0'
// Local unit tests
testImplementation 'junit:junit:4.12'
// Instrumented unit tests
androidTestImplementation "com.android.support:support-annotations:${supportLibVersion}"
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
}
tasks.withType(Test) {
testLogging {
exceptionFormat "full"
events "started", "skipped", "passed", "failed"
showStandardStreams true
}
}
There's a single test file under android_app/app/src/androidTest/java/com.health.openscale/DatabaseTest.java
I'm using the command ./gradlew --no-daemon --no-build-cache -i connectedDebugAndroidTest to build and run the tests and it works fine the first time: both tests in the file are executed and pass.
But now, if I change the test file (small change to trigger a rebuild) and run the above command again I get the following output:
...
Putting task artifact state for task ':app:compileDebugAndroidTestJavaWithJavac' into context took 0.0 secs.
file or directory '/<path>/android_app/app/src/androidTestDebug/java', not found
Executing task ':app:compileDebugAndroidTestJavaWithJavac' (up-to-date check took 0.007 secs) due to:
Input property 'source' file /<path>/android_app/app/src/androidTest/java/com.health.openscale/DatabaseTest.java has changed.
Compiling with source level 1.7 and target level 1.7.
...
Putting task artifact state for task ':app:transformClassesWithDexBuilderForDebugAndroidTest' into context took 0.0 secs.
Executing task ':app:transformClassesWithDexBuilderForDebugAndroidTest' (up-to-date check took 0.024 secs) due to:
Input property '$3' file /<path>/android_app/app/build/intermediates/classes/androidTest/debug/com/health/openscale/DatabaseTest.class has been removed.
Transform inputs calculations based on following changes
/<path>/android_app/app/build/intermediates/classes/androidTest/debug/com/health/openscale/DatabaseTest.class:REMOVED
...
Starting 0 tests on Nexus_5X_API_26(AVD) - 8.0.0
[XmlResultReporter]: XML test result file generated at /<path>/android_app/app/build/outputs/androidTest-results/connected/TEST-Nexus_5X_API_26(AVD) - 8.0.0-app-.xml. Total tests 0,
com.android.builder.testing.ConnectedDevice > No tests found.[Nexus_5X_API_26(AVD) - 8.0.0] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack #Test annotations).
[XmlResultReporter]: XML test result file generated at /<path>/android_app/app/build/outputs/androidTest-results/connected/TEST-Nexus_5X_API_26(AVD) - 8.0.0-app-.xml. Total tests 1, failure 1,
...
Does anyone know why the rebuild doesn't work? Why is the class removed from the build?
I have verified with the APK analyzer in Android studio that the DatabaseTest class is indeed missing from the APK after the rebuild.
It seems that moving the test case from .../com.health.openscale/DatabaseTest.java to .../com/health/openscale/DatabaseTest.java fixed the problem.
Related
I have defined these into my build.gradle, to enable Orchestrator in my Android Instrumented tests:
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
animationsDisabled = true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
dependencies {
androidTestImplementation 'androidx.test:runner:1.5.1'
androidTestUtil 'androidx.test:orchestrator:1.4.2'
}
Sadly, when I start tests, data is not cleared and it persists between test runs.
I tried both, start test from Android Studio and by gradle cli ./gradlew connectedStageDebugAndroidTest
Any idea what I am doing wrong?
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.
I'm using the AndroidJUnitRunner in my project, but the unit tests are painfully slow to execute on a device and emulator both in Android Studio and from the command-line via gradlew.
I'm using the master branch of this open-source project, OneBusAway (as of this commit):
https://github.com/OneBusAway/onebusaway-android
I'm seeing execution time of all 142 tests that is upwards of 3 minutes in real elapsed time, but Android only registers a smaller portion of this in the execution time it shows under "Test Results". Before switching to the AndroidJUnitRunner all of these unit tests executed under 20 seconds consistently.
Here's what an example test looks like:
/**
* Tests to evaluate utility methods related to math conversions
*/
#RunWith(AndroidJUnit4.class)
public class MathUtilTest {
#Test
public void testOrientationToDirection() {
// East
double direction = MathUtils.toDirection(0);
assertEquals(90.0, direction);
}
}
Here's the build.gradle config:
android {
dexOptions {
preDexLibraries true
}
compileSdkVersion this.ext.compileSdkVersion
buildToolsVersion "27.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 93
versionName "2.3.8"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
...
testOptions {
execution 'ANDROID_TEST_ORCHESTRATOR'
unitTests.includeAndroidResources true
}
...
}
dependencies {
...
// Unit tests
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
...
}
Why is this running unit tests so slow?
Apparently the slowdown is related to including a dependency on Android Test Orchestrator, which I don't need (even if I'm running tests on a device that require an Android context). It wasn't clear to me from the existing documentation that Orchestrator wasn't required for these tests.
I removed the following lines from build.gradle, and my total execution time via Android Studio dropped back down into the ~15 second range:
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
...
execution 'ANDROID_TEST_ORCHESTRATOR'
...
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
So here's the new build.gradle that executes tests in ~15 seconds:
android {
dexOptions {
preDexLibraries true
}
compileSdkVersion this.ext.compileSdkVersion
buildToolsVersion "27.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 93
versionName "2.3.8"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
testOptions {
unitTests.includeAndroidResources true
}
...
}
...
dependencies {
...
// Unit tests
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
I thought that maybe the testInstrumentationRunnerArguments clearPackageData: 'true' was the primary culprit causing increased execution time to clear the package data, but removing that line alone didn't change the total execution time of tests - I had to completely remove the Orchestrator dependency.
Here's the commit on Github that removed Orchestrator:
https://github.com/OneBusAway/onebusaway-android/commit/a1657c443258ac49b1be83a75399cf2ced71080e
I'm quite new with the Android gradle build.
I built and application and it work quite ok. Now I would like to add unit test and instrument test into it, and execute them on command line.
I have to test files:
MainActivityTest.java <== This used instrumentation test which should be executed on a device.
CustomerFragmentTest.java <== This used junit test which should be executed on JVM only.
Now from the command line in Project Directory I called
gradlew test --continue
And it executed all tests (both JVM tests and Device Tests) while I expect only the JVM tests to be run.
So:
How can I run the junit test cases on JVM only (Ignore the instrumentation test cases)?
When I call gradlew connectedCheck I got the execution failed message:
"Task 'connectedCheck' not found in root project'Android5Camera'"
How can I execute the instrumentation tests
(on Device) using command line?
Thank for any help and advice.
Here by my app.gradle config in case you need:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.huynhle.android5camera"
minSdkVersion 16
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
main.setRoot('src/main')
androidTest.setRoot('src/test')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}
As i can assume, you have put both class-files to the same directory src/test. This directory is appropriate only for unit tests, and test runner don't distinguish test files between Intsrumentation or simple JUnit. That's why the gradlew test --continue command launches both classes.
Android Instrumentation Tests classes should be located in the src/androidTest directory and should be heirs (direct or no) of InstrumentationTestCase or AndroidTestCase, then you can launch instrumentation tests using gradlew connectedCheck.
So I've set up a gradle project with android and tried to get some tests to run. Unfortunately they don't seem to. It's possible that I'm missing something obvious but here goes...
I am running gradle 1.11 and as I understand the documentation that's the new folder (since 0.9 I believe?) that should be used for tests.
So I have my testclass ::
package se.coinhunter.multigradle.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class HelloAndroidTests {
#Test
public void testHelper() {
assertEquals(1,1);
}
}
}
That lives in src/androidTest and here is my build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':MultiGradleSubmodule')
}
This is a multi-project build and the submodule mentioned in the dependencies block is a plain java project that has its' own scource and unit tests running quite smoothly. I was able to specify that it should tell me when it runs its' tests and give me some feedback and that works fine. That was achieved for that project using
test {
testLogging {
events 'started', 'passed'
}
}
in its' build.gradle. I havn't come across anything like this for android projects. The whole project builds and runs, but I either can't get the tests to run, or they're running but I'm not getting any output.
You're using jUnit 4 (package name "org.junit" with #Test annotation). Android gradle only works with jUnit 3 (package name "junit.framework" with no annotations).
Android tests run in the Dalvik virtual machine on a device or emulator so your test class should also extend "AndroidTestCase" (or one of the other junit subclasses in Android - depending on what you're testing).
UPDATED: also add the following to default config:
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testFunctionalTest true
Run the test using
gradle connectedCheck