Unable to run Android Instrumented Tests - android

I am trying to run some UI tests using espresso on AS Arctic Fox 2020.3.1 Patch 3 ARM preview. Following Google instructions for setting up these are my dependencies:
// Core library
androidTestImplementation 'androidx.test:core:1.4.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestUtil 'androidx.test:orchestrator:1.4.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.ext:truth:1.4.0'
androidTestImplementation 'com.google.truth:truth:1.0.1'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.4.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.4.0'
I have also added the testInstrumentationRunner in my Gradle to testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
I am trying to run the simplest possible test below:
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
#RunWith(AndroidJUnit4::class)
class DummyTest {
#Test
fun test() {
assert(false)
}
}
but when I click the Run of my test it looks like I have 0 tests. Is there something I am missing? Thanks

If AS shows TestResult 0/0 , delete that dependency
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'

Found a way to fix this one after spending many hours. I noticed that the issue had to do with the
androidx.test.espresso:espresso-contrib:3.4.0
check this answer for more insight link
Thought I was not getting any errors or issues. I had to disable the Test Matrix of the AS. After disabling I was getting more clear framework crash errors in the logical which helped me identify the issue.

Related

Android instrumented test failed to instantiate test runner when using assertFailsWith from kotlin.test

I want to use assertFailsWith in an instrumented test to verify that a function fails with a certain exception.
However, when I add the dependency and use assertFailsWith in a test case, I get the following error:
java.lang.RuntimeException: Failed to instantiate test runner class androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
Test case:
#Test
fun test_float() = runBlocking {
val testValue = Random(System.currentTimeMillis()).nextFloat()
assertFailsWith<UnsupportedOperationException>("Float not implemented and failed accordingly") {
doSomething(testValue)
}
}
The instrumented test dependencies are
// Instrumented tests
// Core library
androidTestImplementation "androidx.test:core:$androidXTestVersion"
androidTestImplementation "androidx.test:core-ktx:$androidXTestVersion"
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation "androidx.test:runner:$testRunnerVersion"
androidTestImplementation "androidx.test:rules:$testRulesVersion"
// To use android test orchestrator
androidTestUtil "androidx.test:orchestrator:1.4.1"
// Assertions
androidTestImplementation "androidx.test.ext:junit:$testJunitVersion"
androidTestImplementation "androidx.test.ext:junit-ktx:$testJunitVersion"
androidTestImplementation "androidx.test.ext:truth:$truthVersion"
// Instrumentation unit tests (that will require a real device or emulator)
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlinTestVersion"
androidTestImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
Versions are latest stable versions.
What am I doing wrong?

Instrumented Test not running in Android Tests Passed 0 Passed

I have been trying to run a test case , present in androidTest Package.
But as i execute the test, Emulator launches and I get tests passed : 0 Passed.
and getting this Error in logcat
E/AndroidJUnitRunner: An unhandled exception was thrown by the app.
E/InstrumentationResultPrinter: Failed to mark test No Tests as finished after process crash
here is my code.
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
#RunWith(AndroidJUnit4::class)
class MyAndroidTest {
#Test
fun test_simple() {
assertEquals(2, 1+1)
}
}
I have added all required dependencies.
testImplementation 'junit:junit:4.12'
androidTestImplementation 'junit:junit:4.12'
testImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
testImplementation "org.robolectric:robolectric:4.6"
testImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation "com.google.truth:truth:1.1.3"
testImplementation 'org.mockito:mockito-core:2.24.5'
// required if you want to use Mockito for Android tests
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
When I run the same test case in test package, it runs successfully.
One thing is, I created The androidTest package by myself. It was somehow deleted earlier..
I get this after I run the test case,
plz help.
Add testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" in defaultConfig block inside gradle module file
Is missing in that project and it's necessary.

Android kotlin mockMaker issues

Hi i am trying to mock a final class(as all classes in kotlin are final by default) and added the following dependencies in my gradle:
testImplementation 'junit:junit:4.12'
testImplementation 'au.com.dius:pact-jvm-consumer-junit_2.11:3.5.10'
testImplementation "org.mockito:mockito-android:2.13.0"
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation "org.mockito:mockito-core:2.13.0"
//testImplementation 'io.mockk:mockk:1.8'
testImplementation 'org.assertj:assertj-core:3.8.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "org.mockito:mockito-core:2.13.0"
androidTestImplementation "org.mockito:mockito-android:2.13.0"
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
the mockito-inline is supposed to enable you to mock a final kotlin class and so i added to both my java unit test and my instrumental test using testImplementation and androidTestImplementation
On building the project i get the following error:
More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'
Any ideas whats going on? if i remove the androidTestImplementation of the mockitio inline, it compiles fine but when running intrumental test i get the mockito error saying it cannot mock a final class.
In order to be able to mock final classes in Kotlin, you’ll need to create a file
org.mockito.plugins.MockMaker (literally) that contains only this line
mock-maker-inline
and place it into
test/resources/mockito-extensions.
For more info please read https://antonioleiva.com/mockito-2-kotlin/.
mockito-inline won't work for instrumentation tests. In order to be able to mock final classes in instrumentation tests you just need to include the following line only:
androidTestImplementation com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.0
Refer to its Github page for more details
This thread might also be useful.
As per this answer you cannot mock final classes in androidTest- it is a limitation explained in the Mockito Github issue tracker here:
There is no real possibility to make this [mocking of final classes] work in Android at the moment as it lacks the instrumentation API on top of which we are operating. The Android VM is not a standard VM and only implements a subset of the Java specification. As long as Google does not choose to extend its JVM, I am afraid that this feature will not work.
In my case, I got this error because I use implementation "org.mockito:mockito-inline:2.13.0" instead of testImplementation "org.mockito:mockito-inline:2.13.0"

Mockito+PowerMock gradle configuration

I need to use in my android instrumented tests mockito and powermock.
The main problem is that both of them have some problems with configuring it in gradle because of conflicts and other stuff.
Maybe somebody who has working configuration of .gradle file for mockito+powermock in android instrumented tests could share it?
This is my gradle configuration to use mockito and powerMock:
dependencies {
...
/**Power mock**/
testCompile "org.powermock:powermock-core:1.7.3"
testCompile "org.powermock:powermock-module-junit4:1.7.3"
testCompile "org.powermock:powermock-api-mockito2:1.7.3"
/**End of power mock **/
}
NOTE: I had to remove the mockito dependency in order to make it works:
//Remove this line
testImplementation "org.mockito:mockito-core:2.13.0"
Here is the configuration I am using and it's working perfectly fine.
after 1.7.0 powermock-api-mockito change to powermock-api-mockito2
testImplementation 'org.mockito:mockito-all:1.10.19'
testImplementation "org.powermock:powermock-module-junit4:2.0.7"
testImplementation "org.powermock:powermock-module-junit4-rule:2.0.7"
testImplementation "org.powermock:powermock-api-mockito2:2.0.7"
testImplementation "org.powermock:powermock-classloading-xstream:1.6.6"

AndroidStudio/Gradle with powermock

I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions.
Could anybody show a correct way to do it?
Thanks.
I'm posting in order to help future readers, you need to add these dependencies for powermock in AS
testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-api-mockito:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4:1.6.2'
Add the following lines to your dependencies{} block:
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
And if you would like to use PowerMockito, add the following line:
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
In the build script, add the following:
sourceSets {
unitTest {
java.srcDir file('*your test directory*') //for example: tests/java
}
}
android {
sourceSets {
instrumentTest.setRoot('*your root test directory*') //for example: tests
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.powermock:powermock-mockito-release-full:1.4.9'
}
Then, do gradle unitTest from the command line.
Hope that works. If it doesn't, post the output of the command line.
If you want to use more recent versions of Mockito, you can use something like this, which is adapted from the mockito 2 Powermock docs. Do make sure you use the right version of PowerMock for the given version of Mockito.
...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.4.0"
testCompile 'org.powermock:powermock-module-junit4:1.7.0RC2',
'org.powermock:powermock-api-mockito2:1.7.0RC2'
// mockito
testImplementation 'org.mockito:mockito-core:2.4.0'
androidTestImplementation 'org.mockito:mockito-core:2.4.0'
// PowerMock
testImplementation 'org.powermock:powermock-core:1.7.0RC2'
testImplementation 'org.powermock:powermock-module-junit4:1.7.0RC2'
testImplementation 'org.powermock:powermock-api-mockito2:1.7.0RC2'
I have used same as #Bhargav used with some additional features added with it
code coverage for test case (if testCoverageEnabled is true, then it enable Jacoco tool)
unit test will test only your code and do not depend on any particular behaviour of Android platform by using (UnitTests.returnDefaultValues = true)
Add this marked lines in build.gradle to enable JUnit, PowerMockito, JaCoCo
My example compiled from all the other answers I could find, with latest versions at the time of writing:
app\build.gradle
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.13'
...
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.7'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
}
A test class where say Android Log class was static mocked.
import android.util.Log;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({Log.class})
public class DateUtilsTest {
#BeforeClass
public static void beforeClass() {
PowerMockito.mockStatic(Log.class);
}
...
}

Categories

Resources