How Do We Inherit Test Classes Across Android Library Modules?-ThrowExceptions

Exception or error:

I have an Android Studio project with two library modules: foo-module and bar-module. Each implements a library, with foo-module defining a strategy interface and bar-module depending upon foo-module and implementing such a strategy. foo-module has instrumentation tests (foo-module/src/androidTest/) to test its core code, using a stub strategy implementation, and bar-module should have its own instrumentation tests.

I defined an AbstractTests class in foo-module/src/androidTest/ that does most of the actual testing. I also have a StubTests class in foo-module/src/androidTest/ that extends AbstractTests and implements the necessary abstract methods to complete the test case (providing an implementation of the strategy, etc.). This all works great.

In bar-module/src/androidTest/, I created a BarStrategyTests class, designed to mirror StubTests, but provide the strategy implemented in bar-module. However, BarStrategyTests cannot see AbstractTests, even though I have compile project(':foo-module') in my build.gradle file, and the main (non-test) classes in bar-module can work fine with the main (non-test) classes in foo-module. IOW, while the project() dependency handles regular code, it does not handle androidTest/ code. I get “error: package com.commonsware.foo.test does not exist”.

I tried also adding androidTestCompile project(':foo-module'), with the same result.

What is the recipe for sharing instrumentation test code between modules?

Temporarily, I can clone AbstractTests, but that’s not a great long-term solution.

This SO question covers similar ground for ordinary Java. Has anyone tried the options in the one answer and gotten them to work for Android instrumentation tests? The first option (move the common testing code into yet another module as regular non-test code) seems plausible, but I have no idea if the other two will work well with the com.android.library plugin instead of the java plugin.

How to solve:

Due to the fact that all the test classes (unit and instrumentation) are not a part of any module, including aar, they are not available via dependency to that module. I faced to this issue as well and solved it by creating test-module and put all the required classes in it (src/main/java).
So, in your case you can move AbstractTests in this module and use this module as a androidTestCompile dependency.

Leave a Reply

Your email address will not be published. Required fields are marked *