All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.apter.robolectric.junit.jupiter.gradle.plugin.RobolectricJUnitJupiterGradlePlugin.kt Maven / Gradle / Ivy

Go to download

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.

The newest version!
package tech.apter.robolectric.junit.jupiter.gradle.plugin

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.testing.Test

@Suppress("LibraryEntitiesShouldNotBePublic")
class RobolectricJUnitJupiterGradlePlugin : Plugin {
    private inline val logger get() = Logging.getLogger(javaClass)

    override fun apply(target: Project) {
        logger.trace("${javaClass.name} is applied.")

        with(target) {
            val extension = registerExtension()
            setupTestTasks()
            enableIncludeAndroidResources()
            afterEvaluate {
                if (!extension.doNotAddDependencies.get()) {
                    addDependencies()
                }
            }
        }
    }

    private fun Project.registerExtension(): RobolectricJUnitJupiterGradlePluginExtension = extensions.create(
        "robolectricJUnitJupiter",
        RobolectricJUnitJupiterGradlePluginExtension::class.java
    ).apply {
        doNotAddDependencies.convention(false)
    }

    private fun Project.setupTestTasks() {
        tasks.withType(Test::class.java).configureEach { testTask ->
            testTask.prepareForRobolectric()
        }
        tasks.whenTaskAdded { task ->
            if (task is Test) {
                task.prepareForRobolectric()
            }
        }
    }

    private fun Test.prepareForRobolectric() {
        val jvmArgs = listOf(
            "-Djunit.platform.launcher.interceptors.enabled=true",
        )
        useJUnitPlatform()
        jvmArgs(jvmArgs)
    }

    private fun Project.enableIncludeAndroidResources() {
        val androidExtension = extensions.findByName("android") as? CommonExtension<*, *, *, *>
        @Suppress("UnstableApiUsage")
        androidExtension?.testOptions?.unitTests?.isIncludeAndroidResources = true
    }

    private fun Project.addDependencies() {
        with(dependencies) {
            add(
                "testImplementation",
                "tech.apter.junit5.jupiter:robolectric-extension:${BuildConfig.ROBOLECTRIC_EXTENSION_VERSION}"
            )
            add("testImplementation", platform("org.junit:junit-bom:${BuildConfig.JUNIT5_BOM_VERSION}"))
            add("testRuntimeOnly", "org.junit.jupiter:junit-jupiter-engine")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy