Running simple JUnit tests on Android Studio (IntelliJ) when using a Gradle-based configuration

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)