I am using Android Studio/IntelliJ
to build on an existing Android
project and would like to add some simple JUnit
unit tests. What is
Normally, you can't. Welcome to the world of Android, where all tests must run on a device(except Robolectric).
The main reason is that you don't actually have the framework's sources - even if you convince the IDE to run the test locally, you will immediately get a "Stub! Not implemented" exception.
"Why?" you might wonder? Because the android.jar
that the SDK gives you is actually all stubbed out - all the classes and methods are there but they all just throw an exception. It's there to provide an API but not there to give you any actual implementation.
There's a wonderful project called Robolectric which implements a lot of the framework just so you can run meaningful tests. Coupled with a good mock framework (e.g., Mockito), it makes your job manageable.
Gradle plugin: https://github.com/robolectric/robolectric-gradle-plugin