apter-tech / junit5-robolectric-extension

This repository aims to bridge the gap between JUnit 5 and Robolectric, enabling developers to leverage the benefits of both frameworks for unit testing Android applications. While Robolectric currently lacks a dedicated JUnit 5 extension, this project proposes a community-driven solution to achieve seamless integration.
Apache License 2.0
49 stars 2 forks source link
android-junit junit-5 junit-jupiter junit5 robolectric

JUnit5 Robolectric Extension (Experimental)

GitHub Actions Workflow Status Maven Central Version Gradle Plugin Portal Version GitHub license

This is an experimental project that aims to bridge the gap between JUnit 5 and Robolectric, providing a way to run your Android unit tests using the modern JUnit 5 framework while leveraging Robolectric's in-memory environment.

Key features

Current Limitations

Installation

  1. Add the Gradle Plugin Portal and Maven Central and Google's Maven repository to your project's settings.gradle file:
Kotlin ```kotlin pluginManagement { repositories { gradlePluginPortal() mavenCentral() google() } } dependencyResolutionManagement { repositories { mavenCentral() google() } } ```
Groovy ```groovy pluginManagement { repositories { gradlePluginPortal() mavenCentral() google() } } dependencyResolutionManagement { repositories { mavenCentral() google() } } ```
  1. Enable JUnit Platform Launcher Interceptors and add the dependency to your app or library module's build.gradle:
Kotlin ```kotlin plugins { id("tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin") version ("") } ```
Groovy ```groovy plugins { id 'tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin' version '' } ```

Basic usage

  1. Annotate your test class with @ExtendWith. This extension will manage the Robolectric environment for your tests:
Kotlin ```kotlin @ExtendWith(RobolectricExtension::class) ```
Java ```java @ExtendWith(RobolectricExtension.class) ```
  1. Utilize the standard JUnit 5 annotations (@Test, @BeforeEach, @AfterEach, etc.) within your test methods. You could also use org.jetbrains.kotlin:kotlin-test-junit5 package if you want to.
Kotlin ```kotlin import android.app.Application import android.content.Context import androidx.test.core.app.ApplicationProvider import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Assertions.assertInstanceOf import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith import org.robolectric.annotation.Config import tech.apter.junit.jupiter.robolectric.RobolectricExtension @ExtendWith(RobolectricExtension::class) @Config(application = RobolectricExtensionSelfTest.MyTestApplication::class) class RobolectricExtensionSelfTest { @BeforeEach fun setUp() { } @AfterEach fun tearDown() { } @Test fun shouldInitializeAndBindApplicationAndCallOnCreate() { val application = ApplicationProvider.getApplicationContext() assertInstanceOf(MyTestApplication::class.java, application) assertTrue((application as MyTestApplication).onCreateWasCalled) } companion object { @BeforeAll @JvmStatic fun setUpClass() { } @AfterAll @JvmStatic fun tearDownClass() { } } class MyTestApplication : Application() { internal var onCreateWasCalled = false override fun onCreate() { this.onCreateWasCalled = true } } } ```
Java ```java import android.app.Application; import androidx.test.core.app.ApplicationProvider; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions.assertInstanceOf; import org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.robolectric.annotation.Config; import tech.apter.junit.jupiter.robolectric.RobolectricExtension; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertTrue; @ExtendWith(RobolectricExtension.class) @Config(application = RobolectricExtensionSelfTest.MyTestApplication::class) public class RobolectricExtensionSelfTest { @BeforeEach public void setUp() { } @AfterEach public void tearDown() { } @Test public void shouldInitializeAndBindApplicationAndCallOnCreate() { final Application application = ApplicationProvider.getApplicationContext(); assertInstanceOf(MyTestApplication.class, application); assertTrue(((MyTestApplication) application).onCreateWasCalled); } @BeforeAll public static void setUpClass() { } @AfterAll public static void tearDownClass() { } static class MyTestApplication extends Application { public boolean onCreateWasCalled = false; @Override public void onCreate() { this.onCreateWasCalled = true; } } } ```