Gradle module output configuration for test dependencies only - android

I have a Gradle project containing the two modules app and test, where test contains utilities for testing only. Now I'd like to setup this module, so it doesn't include any outputs into the main configuration of app; even not accidentally.
implementation project(':test') # should fail or not contain any inputs
testImplementation project(':test') # should include all inputs
androidTestImplementation project(':test') # should include all inputs
How do I configure test to behave like this?
I'd assume it would be similarly to how the Android plugin handles configurations for build types and flavors, but I'm also not sure on how to figure this out.
I think these are handled with Gradle consumer attributes. Maybe there's a filter or an attribute which could be applied to it to make it only available to tests.

You could use annotations for that:
https://developer.android.com/studio/write/annotations#visible
Another way would be exclude testing code to new module named as sub_test and use testImplementation or androidTestImplementation of that sub_test module in your test module

Found: Use separate test modules for instrumented tests, which tells the how to do it:
... apply the com.android.test plugin instead of com.android.library.

The java-test-fixtures plugin almost behaves like I wished, just a bit more verbose.
implementation project(':test') # should fail or not contain any inputs
testImplementation testFixtures(project(':test'))
androidTestImplementation testFixtures(project(':test'))
Some other downsides for now
As Tom Tresansky Is the java-test-fixtures plugin incompatible with the Build and run using IntelliJ IDEA setting? asked and answered, the Android Studio doesn't handle the testFixtures source set well.
Also it is not yet available for Kotlin or Android. But as Michael Evans pointed out, there's tasks already open for Android and on YouTrack.
But creating the test fixtures with a Kotlin JVM project and consuming these with an Android project works fine for me.

Related

JUnit5 not generating test-result TEST-*.xml that JUnit4 used to

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

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'

More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker' getting this error while adding
androidTestImplementation "org.mockito:mockito-inline:2.15.0"
in gradle to mock final class
For Android, you usually just want
androidTestImplementation "org.mockito:mockito-android:<latest-version>"
You especially don't want mockito-inline because it configures the wrong MockMaker (mock-maker-inline) instead of AndroidByteBuddyMockMaker which is the only one working on Android that is distributed by the Mockito project. If you need advanced capabilities or faster mocking, head over to the dexmaker project.
In my case I resolved this issue by removing dexmaker dependency from the build.gradle
So, I removed the below dependency
androidTestImplementation "com.linkedin.dexmaker:dexmaker-mockito:2.12.1"
and kept only the below Mockito dependency
androidTestImplementation "org.mockito:mockito-android:2.25.0"
Not sure if that your case, but it solves mine.
If you are using Koin dependency injector, I found the solution on this Github issue.
Try androidTestImplementation ("org.koin:koin-test:$koin_version") { exclude group: 'org.mockito' }
It worked for me, I hope this may help others in the future.
The only correct answer is PowerMockito does not support the Davik VM Android uses, it is meant for a standard JVM. So you can't use it with instrumented tests, only unit tests. I have heard there's a library called OpenDex that allows you to use PowerMockito even with Android but it seems a bit involved to setup and I haven't personally tried it.

Testing using Mockito

Apologies for what may seem an idiotic post.
How do you run Mockito on the newest version of Android Studio SDK?
and can you run multiple tests using Mockito using the Android Studio platform?
I've used Mockito on Eclipse and ran as much as 6 tests in the same window. But I'm trying to figure out how to do this on the Android Studio platform and I cannot find any website or tutorial with an answer.
Android Studio 1.1 now has built-in support for unit testing. From Unit testing support - Android Tools Project Site:
Unit tests run on a local JVM on your development machine. Our gradle plugin will compile source code found in src/test/java and execute it using the usual Gradle testing mechanisms. At runtime, tests will be executed against a modified version of android.jar where all final modifiers have been stripped off. This lets you use popular mocking libraries, like Mockito.
You will have to specify your testing dependencies in the build.gradle file of your android module. For example:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
The page also contains a step-by-step guide for setting up Android Studio for unit testing, including creating a separate directory for unit tests:
Create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the Project view in the Project tool window. The new directory should be highlighted in green at this point. Note: names of the test source directories are determined by the gradle plugin based on a convention.
I'm currently working on a project using junit 4.12 and Mockito 2.0.5 beta for unit testing in Android Studio 1.1, and haven't had any issues:
dependencies {
// ...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.0.5-beta"
}
As far as running multiple tests at the same time, do you mean test cases? Test classes? Test suites? Please clarify, and I'll update my answer, if needed.
Open your app/build.gradle file in your application and add mockito to the dependencies, if dependencies isn't there you can go ahead and create it.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.mockito:mockito-core:1.10.8'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
}
Then in your unit tests, just create a mock object as you normally would:
http://site.mockito.org/#how
Unit tests should be under the app/src/androidTest/ folder.
I can verify that the accepted answer is correct however just to further to the answer, there will be an androidTest folder alongside your main folder. Normally you would use the androidTest folder for instrumentation tests. Just make sure that under the build variants panel, the Test Artifact: is selected to be "Unit Tests" otherwise the testCompile in build.gradle will not work. It took me a while to figure this part of out.
Hope it helps.

Android Studio 1.1, simple junit test setup

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

Android testCompile project not working

I am using Robolectric and the "android-unit-test" plugin in my android project. I have multiple modules (eg foo and bar) and would like to use the same test runner across all the modules. To achieve this I created a Test module to hold my test runner and any custom shadow classes. In foo's build.gradle I have testCompile project (":main:Test")however android studio will not import my runner unless I also include androidTestCompile (":main:test"). Furthermore, when I run ./gradlew test from the command line I get various errors saying that my test runner and shadow classes cannot be found. Anyone have any advice?
With your given information I guess you missed one little aspect: test classes appear not in jars.
When your shared TestRunner is under src/test/java then other modules will not see it. When I'm correct then place you TestRunner at src/main/java and all modules should have access to our support classes.
Here is also an example where i did the same https://github.com/nenick/AndroidAppDevelopment

Categories

Resources