j2emanue
j2emanue

Reputation: 62549

android - gradle task called mockableAndroidJar how to actually use it in unitTests

When i ran ./gradlew tasks i notice a build task called mockableAndroidJar. The description says the following :

Creates a version of android.jar that's suitable for unit tests.

Can someone give me an example how i can use this jar mock in code ?

From the android tools page i saw the following:

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.

But my question is can someone show me how i can make my test use this new android.jar file instead of the current one that has finals on the classes ? How to point tests to use this jar instead ? I saw this question but its not showing me how to use this in tests, only gives theory. I actually ran the following command from shell:

./gradlew mockableAndroidJar

and the output was the following:

    $./gradlew mockableAndroidJar
Picked up JAVA_TOOL_OPTIONS: -Xmx512m -Xms64m
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=768m; support was removed in 8.0
Incremental java compilation is an incubating feature.
:app:mockableAndroidJar UP-TO-DATE

BUILD SUCCESSFUL

Total time: 1.226 secs

So i thought there would be some change in my directory structure but i dont see any. Is the jar file automatically swapped for me ?

Upvotes: 1

Views: 2317

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317808

When I run that target in my project that targets API level 23, it produces this file:

./build/generated/mockable-android-23.jar

This jar files contains all the stubbed, public class definitions for API level 23, except all the final modifiers have been removed so they can be mocked.

This jar file is automatically added to your test classpath so that when you compile and run tests, you have access to those classes for mocking when you run unit tests on the command line:

./gradlew test

You will have to define your own tests. Usually you place them in src/test/main and use the JUnit framework to run them.

Upvotes: 3

Related Questions