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

前端 未结 6 1781
死守一世寂寞
死守一世寂寞 2020-12-12 12:39

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

6条回答
  •  感情败类
    2020-12-12 13:31

    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

提交回复
热议问题