Search code examples
androidandroid-studiojunit4android-productflavors

How to Unit test (junit) Android Flavor/Variant - class does not exist


We have 2 product flavors in our app and one flavor has a class (FlavorSpecificClass) that the other does not. We have a junit test for FlavorSpecificClass in the src/test folder which will not compile when we are on our core variant because the class does not exist in that variant.

I cannot find a way to have separate unit tests per variant and the code will not compile in our core variant unless I comment out the references to FlavorSpecificClass. How can I get around this?


Solution

  • Assuming you are using gradle.

    So for this you need to have another directory which rests in the same place as your 'test' directory. Name the directory as 'test[BuildVariant]' and have the tests for that build variant inside that directory.

    Suppose you have built variant debug, Your directory structure would look something like this, if you want to test classes that are just in debug build variant

    -debug/java/...
    -main/java/...
    -test/java/[your tests]
    -testDebug/java/[your tests for build variant debug]