Search code examples
androidandroid-testing

What is the difference between AndroidJUnit4::class and AndroidJUnit4ClassRunner::class?


I am a newbie to android testing and I have observer both of these classes being suggested when I declare a class with @RunWith() annotation. I searched for more documentation but there does not seem to be much. When and why should I use each one?


Solution

  • AndroidJUnit4

    • Is used with roboelectric types of tests that run on your work machines/JVM.
    • Extends from Standand JUnit Runner.
    • Use it for when you need to quickly run android units tests which may not benefit much from full android environment.(Business logic etc.) You can customize the test environment to your needs.

    AndroidJUnitRunner

    • Is used to run instrumented JUnit 4 tests on Android devices, including those using the Espresso, UI Automator, and Compose testing frameworks.
    • Extends from Instrumentation class which provides functionality to handle app activity functions like calling OnActivityDestroy on some activity.
    • Use it for UI and other types of tests where test environment is not sandboxed and needs to interact with device(Real or emulator).