https://github.com/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.
https://github.com/apter-tech/junit5-robolectric-extension
android-junit junit-5 junit-jupiter junit5 robolectric
Last synced: 3 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/apter-tech/junit5-robolectric-extension
- Owner: apter-tech
- License: apache-2.0
- Created: 2024-03-07T23:49:25.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-02T14:45:47.000Z (4 months ago)
- Last Synced: 2025-01-02T15:32:25.440Z (4 months ago)
- Topics: android-junit, junit-5, junit-jupiter, junit5, robolectric
- Language: Kotlin
- Homepage:
- Size: 401 KB
- Stars: 52
- Watchers: 3
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - 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. (Kotlin)
README
# JUnit5 Robolectric Extension (Experimental)
[](https://github.com/apter-tech/junit5-robolectric-extension/actions/workflows/build.yml?query=branch%3Amain)
[](https://central.sonatype.com/search?q=tech.apter.junit5.jupiter)
[](https://plugins.gradle.org/plugin/tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin)
[](https://www.apache.org/licenses/LICENSE-2.0)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
* **JUnit 5 Compatibility:** Run your tests with the latest JUnit 5 features and syntax.
## Current Limitations
* **Parallel Execution:** Parallel test execution only experimentally supported with classes.
* **Configuration:**
* Robolectric `@Config`'s sdk parameter annotation can only be set on outermost test class.
* `@GraphicsMode` annotations can only be set on outermost test class.
* **Experimental Status:** This extension is still under development, and its API might change in
future versions.## 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()
}
}
```2. 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)
```2. 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 = falseoverride 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;
}
}
}```