Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a gradle plugin #30

Merged
merged 6 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,17 @@ jobs:
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@db19848a5fa7950289d3668fb053140cf3028d43 # v3.3.2
- name: Publish Gradle Plugin
run: |
./publishGradlePlugin.sh
env:
JUNIT5_ROBOLECTRIC_EXTENSION_GRADLE_PLUGIN_PORTAL_KEY: '${{ secrets.JUNIT5_ROBOLECTRIC_EXTENSION_GRADLE_PLUGIN_PORTAL_KEY }}'
JUNIT5_ROBOLECTRIC_EXTENSION_GRADLE_PLUGIN_PORTAL_SECRET: '${{ secrets.JUNIT5_ROBOLECTRIC_EXTENSION_GRADLE_PLUGIN_PORTAL_SECRET }}'
JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_KEY: '${{ secrets.JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_KEY }}'
JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_PASSWORD: '${{ secrets.JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_PASSWORD }}'
- name: Build with Gradle Wrapper
run: ./gradlew build koverXmlReport publish
run: |
./gradlew build koverXmlReport publish
env:
JUNIT5_ROBOLECTRIC_EXTENSION_MAVEN_USERNAME: '${{ secrets.JUNIT5_ROBOLECTRIC_EXTENSION_MAVEN_USERNAME }}'
JUNIT5_ROBOLECTRIC_EXTENSION_MAVEN_PASSWORD: '${{ secrets.JUNIT5_ROBOLECTRIC_EXTENSION_MAVEN_PASSWORD }}'
Expand Down
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 39 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# JUnit5 Robolectric Extension (Experimental)

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.
providing a way to run your Android unit tests using the modern JUnit 5 framework while leveraging
Robolectric's in-memory environment.

## Key features

Expand All @@ -14,21 +13,34 @@ in-memory environment.
* **Parallel Execution:** Parallel test execution is not yet supported. We're actively working on
addressing this limitation in future releases.
* **Configuration:**
* Robolectric `@Config`'s sdk parameter annotation can only be set on most outer test class.
* `@ResourcesMode`, `@LooperMode`, `GraphicsMode` annotations can only be set on most outer test class.
* Robolectric `@Config`'s sdk parameter annotation can only be set on outermost test class.
* `@ResourcesMode`, `@LooperMode`, `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 Maven Central repository to your project's `build.gradle`:
1. Add the Gradle Plugin Portal and Maven Central and Google's Maven repository to your project's
`settings.gradle` file:

<details open>
<summary>Kotlin</summary>

```kotlin
repositories {
mavenCentral()
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
google()
}
}

dependencyResolutionManagement {
repositories {
mavenCentral()
google()
}
}
```

Expand All @@ -38,8 +50,19 @@ repositories {
<summary>Groovy</summary>

```groovy
repositories {
mavenCentral()
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
google()
}
}

dependencyResolutionManagement {
repositories {
mavenCentral()
google()
}
}
```

Expand All @@ -52,30 +75,8 @@ repositories {
<summary>Kotlin</summary>

```kotlin
android {
testOptions {
unitTests {
isIncludeAndroidResources = true
all { test ->
test.useJUnitPlatform()
test.jvmArgs(
listOf(
"-Djunit.platform.launcher.interceptors.enabled=true",
"--add-exports", "java.base/jdk.internal.loader=ALL-UNNAMED",
"--add-opens", "java.base/jdk.internal.loader=ALL-UNNAMED",
)
)
}
}
}
}

dependencies {
testImplementation("tech.apter.junit.jupiter:robolectric-extension:<latest.release>")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:<latest.release>") // JUnit 5 Jupiter Engine
// Optional dependencies if you want to use different version than used by the extension
testImplementation("org.junit.jupiter:junit-jupiter-api:<latest.release>") // Latest JUnit 5 Jupiter API
testImplementation("org.robolectric:robolectric:<latest.release>") // Latest Robolectric version
plugins {
id("tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin") version ("<latest.release>")
}
```

Expand All @@ -85,29 +86,8 @@ dependencies {
<summary>Groovy</summary>

```groovy
android {
testOptions {
unitTests {
includeAndroidResources = true
all {
useJUnitPlatform()
jvmArgs(
'-Djunit.platform.launcher.interceptors.enabled=true',
'--add-exports', 'java.base/jdk.internal.loader=ALL-UNNAMED',
'--add-opens', 'java.base/jdk.internal.loader=ALL-UNNAMED',
)
}
}
}
}

dependencies {
testImplementation 'tech.apter.junit.jupiter:robolectric-extension:<latest.release>'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:<latest.release>'
// Latest JUnit 5 Jupiter Engine
// Optional dependencies if you want to use different versions than used by the extension
testImplementation 'org.junit.jupiter:junit-jupiter-api:<latest.release>' // Latest JUnit 5 Jupiter API
testImplementation 'org.robolectric:robolectric:<latest.release>' // Latest Robolectric version
plugins {
id 'tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin' version '<latest.release>'
}
```

Expand Down Expand Up @@ -137,8 +117,8 @@ dependencies {
</details>

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.
test methods. You could also use `org.jetbrains.kotlin:kotlin-test-junit5` package if you want
to.

<details open>
<summary>Kotlin</summary>
Expand Down Expand Up @@ -270,10 +250,5 @@ public class RobolectricExtensionSelfTest {

## Important Notes

* Ensure `isIncludeAndroidResources` is set to true in your testOptions configuration to access
Android resources in your tests.
* JUnit Platform Launcher Interceptors must be
enabled (`junit.platform.launcher.interceptors.enabled=true`), otherwise
test instances will not be created by Robolectric's classloader.
* Parallel execution is currently not supported. Run tests sequentially for now.

9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@ allprojects {
version = property('tech.apter.junit5.robolectric.extension.version')
}

subprojects {
configurations.configureEach {
resolutionStrategy.dependencySubstitution {
substitute module("tech.apter.junit5.jupiter:robolectric-extension:$version") using project(':robolectric-extension')
}
}
}

dependencies {
kover(project(':integration-tests:agp-groovy-dsl'))
kover(project(':integration-tests:agp-kotlin-dsl'))
kover(project(':robolectric-extension'))
kover(project(':robolectric-extension-gradle-plugin'))
}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1024M
android.useAndroidX=true
kotlin.code.style=official
tech.apter.junit5.robolectric.extension.version=0.2.0
tech.apter.junit5.robolectric.extension.version=0.2.1-SNAPSHOT
5 changes: 5 additions & 0 deletions gradle/dokka.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tasks.register('dokkaJavadocJar', Jar) {
dependsOn dokkaJavadoc
archiveClassifier.set 'javadoc'
from javadoc.destinationDir
}
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ androidCompileSdk = "34"
androidGradle = "8.3.2"
androidMinimumSdk = "14"
androidxTestExtJunit = "1.1.5"
buildConfig = "5.3.5"
detekt = "1.23.6"
dokka = "1.9.20"
gradlePluginPublish = "1.2.1"
guava = "33.1.0-jre"
junit4 = "4.13.2"
junit5 = "5.10.2"
Expand Down Expand Up @@ -37,8 +39,10 @@ robolectricAndroidAll = { module = "org.robolectric:android-all", version.ref =

[plugins]
androidLibrary = { id = "com.android.library", version.ref = "androidGradle" }
buildConfig = { id = "com.github.gmazzo.buildconfig", version.ref = "buildConfig" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
gradlePluginPublish = { id = "com.gradle.plugin-publish", version.ref = "gradlePluginPublish" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlinxKover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }
37 changes: 37 additions & 0 deletions gradle/pom.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
final void setupPom(final MavenPublication publication, final String publicationName) {
publication.pom { pom ->
name = publicationName
description = """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.""".stripMargin()
url = 'https://github.com/apter-tech/junit5-robolectric-extension'
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
organization {
name = 'Apter Technologies Ltd.'
url = 'https://apter.tech'
}
developers {
developer {
id = 'warnyul'
name = 'Balázs Varga'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:git://github.com:apter-tech/junit5-robolectric-extension.git'
developerConnection = 'scm:git:ssh://github.com:apter-tech/junit5-robolectric-extension.git'
url = 'https://github.com/apter-tech/junit5-robolectric-extension'
}
}
}

ext {
setupPom = this.&setupPom
}
79 changes: 0 additions & 79 deletions gradle/publishing.gradle

This file was deleted.

16 changes: 16 additions & 0 deletions gradle/setupMavenCentralForPublishing.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'maven-publish'

publishing {
repositories {
maven {
name = 'MavenCentral'
final releasesRepoUrl = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2'
final snapshotsRepoUrl = 'https://s01.oss.sonatype.org/content/repositories/snapshots'
url = version.toString().endsWith("SNAPSHOT") ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username = System.getenv('JUNIT5_ROBOLECTRIC_EXTENSION_MAVEN_USERNAME')
password = System.getenv('JUNIT5_ROBOLECTRIC_EXTENSION_MAVEN_PASSWORD')
}
}
}
}
18 changes: 18 additions & 0 deletions gradle/signing.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
final JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_KEY = System.getenv(
'JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_KEY'
) ?: ""
final JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_PASSWORD = System.getenv(
'JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_PASSWORD'
) ?: ""

if (!JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_KEY.isEmpty() && !JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_PASSWORD.isEmpty()) {
apply plugin: 'signing'

signing {
useInMemoryPgpKeys(
JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_KEY,
JUNIT5_ROBOLECTRIC_EXTENSION_GPG_SIGNING_PASSWORD,
)
sign(publishing.publications)
}
}
Loading
Loading