How to share Test Classes between Modules in an Android multi-module project?


In my project, I have 3 basic modules.

  1. app-mobile
  2. network
  3. cache

As could be understood, each module represents a functional layer.
The network module takes care of all the network interactions, and similarly, the cache module takes care of the local database interactions.

The network and the cache modules are thoroughly tested, and each of them also contains the TestData Factories used for mocking the data.

I want to now test the mobile app module, and want to use the same data factories implemented in the network and the cache modules.

How could this be achieved?


If you really believe the TestData is of vital importance to more than one module, you should pull it out as its own module and make it a testImplementation dependency of the modules you want to use it in for testing.

On a hunch, I’d be more inclined to create new TestData Factories for your app module and although you may have duplicate code, you may not have duplication. Have a read of this: