Gaurav Vashisth
Gaurav Vashisth

Reputation: 7737

Android unit test support does not work in android library modules

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

Upvotes: 5

Views: 2714

Answers (3)

cketti
cketti

Reputation: 1377

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

Upvotes: 7

Niklas
Niklas

Reputation: 25401

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

Upvotes: 1

Fred
Fred

Reputation: 17085

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.

Upvotes: 2

Related Questions