Running unit tests locally on Android Studio works fine but when I run it with gradle using ./gradlew test. The tests fails with the exception saying Mockito cannot spy/mock final classes. I thought using mockito with the inline extension was supposed to fix this. I have this dependency
in my gradle file testImplementation "org.mockito:mockito-inline:2.18.0"
Is there something that I'm missing?
What helped me, is to create a MockMaker file and to put it into test resources.
File
org.mockito.plugins.MockMaker (literally) that contains only this line
mock-maker-inline
and place it into
test/resources/mockito-extensions.
Answer - https://stackoverflow.com/a/50449830/3569545
Related
When I used JUnit4 in my projects it seemed that each time a test ran, it would generate a TEST-*.xml report in app/build/test-result. Jenkins would use these XML reports to display failing and passing tests on each build.
I've replaced JUnit4 with JUnit5 with the following in build.gradle:
testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.2"
When running tests with JUnit 5, I'm no longer seeing these TEST-*.xml files being generated. As soon as I drop back to JUnit4, they are.
Is this no longer available in JUnit5 or is there something I have to set on each test in order to get these XML reports?
Found the solution. In order for the XML reports to be generated for each test you need to include the following in your build.gradle:
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.2'
As well as:
tasks.withType(Test) {
useJUnitPlatform()
}
This other post may also be of use to others: JUnit5 integration tests with Gradle 4.6
I'm facing a problem when running tests on my project. I'm using JUnit, Mockito and PowerMock.
I have a test class which I can run from Android Studio and works well (6 out of 6 working). But, if instead of doing it from AS i use the command ./gradlew test three of them are failing with the error:
java.lang.NoClassDefFoundError at MyTestsClass.java:166
Searching that line I can see that's calling a method (so I suppose the error is not exactly there but inside the call, somewhere).
How can it be failing from the command and working from AS? What's the difference between this two? What could it be causing that error?
It could be because of your dependencies.
You don't have them defined on the test it won't compile. For example:
// Needed to compile
compileOnly 'com.madgag.spongycastle:prov:1.54.0.0'
// Needed to compile tests
testImplementation 'com.madgag.spongycastle:prov:1.54.0.0'
Could it be?
I am writing junit tests on android project using the new unit test support http://tools.android.com/tech-docs/unit-testing-support.
While the unit tests run on the 'com.android.application' module perfectly but they always fail on the 'com.android.library' modules. This has not been documented in http://tools.android.com/tech-docs/unit-testing-support . So I wonder whether I am the culprit.
When I write those tests on library modules, the tests can not find the classes on the module and always gives following errors:
package does not exist
error: cannot find symbol
The android unit test support is in experimental phase right now, but is there a solution to it.
UPDATE
I have added this issue to android issue tracker https://code.google.com/p/android/issues/detail?id=161038
It looks like the task to compile the unit tests doesn't depend on the task to compile the library code.
The following fixed it for me:
afterEvaluate {
tasks['assembleDebugUnitTest'].dependsOn(tasks['assembleDebug'])
}
I run the tests using
./gradlew testDebug
If you don't want to modify your build.gradle, manually specify the assembleDebug task on the command line should also do the trick:
./gradlew assembleDebug testDebug
In my android library project I also failed to get the tests running. What I did was create a test application that uses the library and wrote tests in the application that call the library methods.
This might not be the ideal solution, but was the way we got this to work.
Have a look over here https://github.com/nenick/AndroidStudioAndRobolectric
There you can run unit tests on libraries and flavors. And no you don't need to use Robolectric as Gaurav Vashisth stated. You can if you want to.
Here is an example of JUnit test in a library module
A new AndroidStudio 1.1 version introduced the unit testing support. This URL http://tools.android.com/tech-docs/unit-testing-support provides step-by-step instruction how to setup IDE to run JUnit tests for Android sources.
This plugin https://bitbucket.org/hvisser/android-apt used to provide Dagger2 generated files to AS and it works OK for usual Android code but unfortunately there is no generated Dagger2 files for any JUnit test class. I tried to configure dependency like
androidTestApt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
according to android-apt plugin documentation but without success.
I think the problem is in different sources directory for Unit tests - it's src/test/java instead of src/androidTest/java that used by android instrumentation tests.
Can you please provide any help or info how to resolve this trouble?
Having
// You version may vary
androidTestApt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
in your dependencies, open a terminal in your project, run
./gradlew assembleTest
This will generate the Dagger component classes living under your androidTest source set.
Go back to Android Studio, the class now exists and can be used.
I have read around, there are a number of extensive answers (like this one) but the Android world evolves so fast that they seem to be a bit outdated and the official documentation still refers to Eclipse with ADT.
I am running AS 1.1 and I am trying to setup simple junit tests to run on the emulator, without Robolectric. If I don't include junit in my build.gradle, it can't find #After, #Before and #Test and I get package org.junit does not exist. Upon adding
// unit tests
androidTestCompile 'junit:junit:4.11'
the error becomes
Error:duplicate files during packaging of APK
[...]/app/build/outputs/apk/app-debug-test-unaligned.apk
Path in archive: LICENSE.txt
Origin 1: [...]/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
Origin 2: [...]/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'LICENSE.txt'
}
}
Following the console suggestion of excluding LICENSE.txt, it then works but it feels like a hack. So I'm wondering, am I maybe missing something? Thanks.
Android Studio unit testing support comes in 1.1 Beta 4 (release announcement) with Gradle plugin version 1.1.0-rc1.
More info in official document.
However it is experimental feature for now. E.g. it breaks installDebug gradle task.
For using JUnit in instrumentation tests there is good guide for Espresso library and another covering new AndroidJUnitRunner.
If it's any use I set up a boiler plate project allowing the use of Unit tests and Espresso tests by the use of switching build variants. You won't need the use of any third party plugins with this.
https://github.com/hitherejoe/Android-Boilerplate