The best way to quickly run JUnit tests in an Android project in Android Studio

I have some simple old Java classes and simple JUnit tests in my Android Studio project. They currently live in my Android module. If I run them as tests on Android, they run fine, but they are very slow, since it takes to run or connect to the emulator.

If I try to run it as a JUnit test, I got into a version issue !!! JUnit version 3.8 or later expected !!! JUnit version 3.8 or later expected JUnit. This answer explains how to work around this problem. Unfortunately, I then clicked on java.lang.NoClassDefFoundError: junit/textui/ResultPrinter , which I cannot figure out how to fix.

Then I tried to port the JUnit tests to a separate Java module, which depends on the Android module. Everything compiles fine. I added a new configuration to run JUnit tests (: MyJUnitTests: test), but the tests do not work with package com.myproject.util does not exist . It seems that the classpath does not include classes from the Android dependent module.

Does anyone know how to fix this class problem? I looked through a lot of related answers and none of them seem to work for me. Or is it just a bad idea to try and save JUnit tests in your own module?

I can get the JUnit tests to work quickly and pass if I translate my simple Java classes and their JUnit tests into a completely separate module (like here ) and change the dependency, so the Android module depends on the Java module. Is this the preferred solution?

+6
source share
1 answer

The main problem is that the Android Framework classes do not work very well outside the context of the Android OS, so any code that has dependencies on the Framework classes does not work in the usual JUnit environment, as you already found.

Your decision to try moving JUnit tests to a separate module will not work, because you cannot have a simple Java module depending on the Android module. Android Gradle modules do not act like Java modules, because Android builds are much more complex, and since the end result of assembling an Android module is APK or AAR, which other types of modules will not understand.

If you can port your simple Java classes and unit tests to a simple Java module and depend on Android modules, this would be the best approach that will get the most support from official functions in the IDE. It will also have an architectural advantage in that it saves these classes free of charge from Android abstractions, making them more portable and allowing the separation of business logic with a user interface or storage or other things more specific to the device.

If this is difficult for you, then many developers in your shoes come with Robolectric , which is a test harness that allows code depending on many parts of the Android Framework to run in the JUnit environment without an emulator or device. It is not officially supported by Google, but Google knows that it is widely used and is trying not just not to violate it.

+8
source

Source: https://habr.com/ru/post/971991/


All Articles