Git Product home page Git Product logo

apter-tech / junit5-robolectric-extension Goto Github PK

View Code? Open in Web Editor NEW
41.0 3.0 2.0 379 KB

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.

License: Apache License 2.0

Kotlin 87.59% Java 10.06% Shell 2.35%
android-junit junit-5 junit-jupiter junit5 robolectric

junit5-robolectric-extension's People

Contributors

mannodermaus avatar renovate-self-hosted-apter[bot] avatar warnyul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

junit5-robolectric-extension's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update all non-major dependencies (actions/setup-java, actions/upload-artifact, gradle, org.junit:junit-bom, com.google.guava:guava, com.android.library, com.android.tools.build:gradle-api, com.android.tools:common)
  • chore(deps): update kotlin to v2.0.10 (org.jetbrains.kotlin.jvm, org.jetbrains.kotlin.android, org.jetbrains.kotlin:kotlin-reflect, org.jetbrains.kotlin:kotlin-test-junit5)
  • chore(deps): update gradle/actions action to v4
  • chore(deps): lock file maintenance

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/build.yml
  • actions/checkout v4.1.7@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/setup-java v4.2.1@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9
  • gradle/actions v3.5.0@d9c87d481d55275bb5441eef3fe0e46805f9ef70
  • actions/upload-artifact v4.3.4@0b2256b8c012f0828dc542b3febcab082c67f72b
  • actions/upload-artifact v4.3.4@0b2256b8c012f0828dc542b3febcab082c67f72b
  • actions/checkout v4.1.7@692973e3d937129bcbf40652eb9f2f61becf3332
  • actions/download-artifact v4.1.8@fa0a91b85d4f404e444e00e005971372dc801d16
  • mikepenz/action-junit-report v4.3.1@db71d41eb79864e25ab0337e395c352e84523afe
  • actions/download-artifact v4.1.8@fa0a91b85d4f404e444e00e005971372dc801d16
  • mi-kas/kover-report v1.9@5f58465b6f395c8fa3adc2665e27250bad87de50
  • mi-kas/kover-report v1.9@5f58465b6f395c8fa3adc2665e27250bad87de50
  • ubuntu 22.04
gradle
gradle.properties
settings.gradle
  • org.gradle.toolchains.foojay-resolver-convention 0.8.0
build.gradle
buildSrc/settings.gradle
buildSrc/build.gradle
gradle/dokka.gradle
gradle/libs.versions.toml
  • com.android.tools.build:gradle-api 8.5.1
  • com.android.tools:common 31.5.1
  • androidx.test.ext:junit 1.2.1
  • io.gitlab.arturbosch.detekt:detekt-formatting 1.23.6
  • io.gitlab.arturbosch.detekt:detekt-rules-libraries 1.23.6
  • com.google.guava:guava 33.2.1-jre
  • com.google.guava:guava [32.0.1-jre,]
  • junit:junit 4.13.2
  • org.junit:junit-bom 5.10.3
  • org.jetbrains.kotlin:kotlin-test-junit5 2.0.0
  • org.jetbrains.kotlin:kotlin-reflect 2.0.0
  • org.robolectric:robolectric 4.13
  • com.android.library 8.5.1
  • com.github.gmazzo.buildconfig 5.4.0
  • io.gitlab.arturbosch.detekt 1.23.6
  • org.jetbrains.dokka 1.9.20
  • com.gradle.plugin-publish 1.2.1
  • org.jetbrains.kotlin.android 2.0.0
  • org.jetbrains.kotlin.jvm 2.0.0
  • org.jetbrains.kotlinx.kover 0.8.3
  • tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin 0.7.0
gradle/pom.gradle
gradle/setupMavenCentralForPublishing.gradle
gradle/signing.gradle
integration-tests/agp-groovy-dsl/build.gradle
integration-tests/agp-kotlin-dsl/build.gradle.kts
integration-tests/java-11/build.gradle
robolectric-extension/build.gradle
robolectric-extension-gradle-plugin/build.gradle
gradle-wrapper
gradle/wrapper/gradle-wrapper.properties
  • gradle 8.9
regex
.github/workflows/build.yml
  • windows github actions runner 22
  • windows github actions runner 22
  • windows github actions runner 22
.github/workflows/build.yml
  • ubuntu github actions runner 22
  • ubuntu github actions runner 22
  • ubuntu github actions runner 22
  • ubuntu github actions runner 22
renovate.json5
  • github>apter-tech/renovate-config v0.7.0

Espresso interactions deadlock somewhere after the first successful test

I have yet to dive deep into what causes this, but here is a side effect I have observed with Espresso assertions running in a JUnit 5 context that uses RobolectricExtension. It's probably the magic of switching thread contexts and class loaders that trips it up, but I wanted to hear your thoughts on the matter first. If I remove the Espresso stuff and only keep ActivityScenario around, then it seems to work no matter how many tests I run. Also, this happens with any kind of test (@ParameterizedTest, @RepeatedTest etc), but I am using the base @Test to make it as simple as possible.

With JUnit 5

The following test class will execute "test 1" successfully, then deadlock in "test 2" just before the first onView().check() assertion:

// ...
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(RobolectricExtension::class)
class ActivityScenarioTest {
    @Test
    fun `test 1`() {
        println("1")
        val scenario = ActivityScenario.launch(MainActivity::class.java)

        println(" - check")
        onView(withText("Changed")).check(doesNotExist())
        println(" - click")
        onView(withText("Click me")).perform(click())
        println(" - verify")
        onView(withText("Changed")).check(matches(isDisplayed()))

        scenario.close()
    }

    @Test
    fun `test 2`() {
        println("2")
        val scenario = ActivityScenario.launch(MainActivity::class.java)

        println(" - check")
        onView(withText("Changed")).check(doesNotExist())
        println(" - click")
        onView(withText("Click me")).perform(click())
        println(" - verify")
        onView(withText("Changed")).check(matches(isDisplayed()))

        scenario.close()
    }
}
Screenshot 2024-07-22 at 15 47 24

With JUnit 4

When changing this to a JUnit 4 environment (i.e. change @Test annotations, then replace @ExtendWith with @RunWith), all tests are okay:

// ...
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class ActivityScenarioTest {
    @Test
    fun `test 1`() {
        println("1")
        val scenario = ActivityScenario.launch(MainActivity::class.java)

        println(" - check")
        onView(withText("Changed")).check(doesNotExist())
        println(" - click")
        onView(withText("Click me")).perform(click())
        println(" - verify")
        onView(withText("Changed")).check(matches(isDisplayed()))

        scenario.close()
    }

    @Test
    fun `test 2`() {
        println("2")
        val scenario = ActivityScenario.launch(MainActivity::class.java)

        println(" - check")
        onView(withText("Changed")).check(doesNotExist())
        println(" - click")
        onView(withText("Click me")).perform(click())
        println(" - verify")
        onView(withText("Changed")).check(matches(isDisplayed()))

        scenario.close()
    }
}
Screenshot 2024-07-22 at 15 50 00

I have pushed an example to a branch in my fork: https://github.com/mannodermaus/junit5-robolectric-extension/blob/check/androidx-robolectric-espresso-deadlock/integration-tests/agp-kotlin-dsl/src/test/kotlin/tech/apter/junit/jupiter/robolectric/integration/tests/agp/kotlin/dsl/ActivityScenarioTest.kt

Possibility to reduce targetCompatibility to Java 11?

I'm trying to introduce support for JUnit 5 to Roborazzi, a helpful screenshot testing library that integrates with Robolectric. That library builds its JARs with a target compatibility of Java 11, and this clashes when paired with junit5-robolectric-extension, which outputs Java 17-compatible bytecode. We could keep using Java 17 to build its artifacts, but would you consider dropping its output to Java 11 compatibility? This way, we could extend the potential user base to all projects that still run on Android Gradle Plugin 7.x, for instance. If not, we can probably consider a split target requirement over at the Roborazzi repository (so that only the JUnit 5 stuff requires Java 17 compatibility). Thanks in advance!

Couldn't create lock file in attempt to run an example of Basic usage

Hello!
Seems that incorrect files naming is used on Windows.
The plugin version:
id 'tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin' version '0.7.0'
And the Robolectric dependency is:
testImplementation 'org.robolectric:robolectric:4.12.1'

Couldn't create lock file C:\Users\vkuts\org.robolectric:android-all-instrumented:13-robolectric-9030017-i6.lock
java.lang.IllegalStateException: Couldn't create lock file C:\Users\vkuts\org.robolectric:android-all-instrumented:13-robolectric-9030017-i6.lock
at tech.apter.junit.jupiter.robolectric.internal.JUnit5MavenDependencyResolver.whileLocked(JUnit5MavenDependencyResolver.kt:81)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5MavenDependencyResolver.getLocalArtifactUrls(JUnit5MavenDependencyResolver.kt:48)
at org.robolectric.internal.dependency.MavenDependencyResolver.getLocalArtifactUrls(MavenDependencyResolver.java:78)
at org.robolectric.internal.dependency.MavenDependencyResolver.getLocalArtifactUrl(MavenDependencyResolver.java:129)
at org.robolectric.plugins.DefaultSdkProvider$DefaultSdk.getJarPath(DefaultSdkProvider.java:143)
at org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader.(AndroidSandbox.java:128)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxBuilder.createClassLoader(JUnit5RobolectricSandboxBuilder.kt:62)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxBuilder.build(JUnit5RobolectricSandboxBuilder.kt:37)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxManager.getAndroidSandbox(JUnit5RobolectricSandboxManager.kt:43)
at org.robolectric.RobolectricTestRunner.getSandbox(RobolectricTestRunner.java:248)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunner.sdkEnvironment(JUnit5RobolectricTestRunner.kt:51)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunner.bootstrapSdkEnvironment(JUnit5RobolectricTestRunner.kt:48)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.sdkEnvironment(JUnit5RobolectricTestRunnerHelper.kt:29)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.proceedInvocation(JUnit5RobolectricTestRunnerHelper.kt:66)
at tech.apter.junit.jupiter.robolectric.RobolectricExtension.interceptBeforeAllMethod(RobolectricExtension.kt:63)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
Suppressed: java.lang.IllegalStateException: Couldn't create lock file C:\Users\vkuts\org.robolectric:android-all-instrumented:13-robolectric-9030017-i6.lock
at tech.apter.junit.jupiter.robolectric.internal.JUnit5MavenDependencyResolver.whileLocked(JUnit5MavenDependencyResolver.kt:81)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5MavenDependencyResolver.getLocalArtifactUrls(JUnit5MavenDependencyResolver.kt:48)
at org.robolectric.internal.dependency.MavenDependencyResolver.getLocalArtifactUrls(MavenDependencyResolver.java:78)
at org.robolectric.internal.dependency.MavenDependencyResolver.getLocalArtifactUrl(MavenDependencyResolver.java:129)
at org.robolectric.plugins.DefaultSdkProvider$DefaultSdk.getJarPath(DefaultSdkProvider.java:143)
at org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader.(AndroidSandbox.java:128)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxBuilder.createClassLoader(JUnit5RobolectricSandboxBuilder.kt:62)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxBuilder.build(JUnit5RobolectricSandboxBuilder.kt:37)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricSandboxManager.getAndroidSandbox(JUnit5RobolectricSandboxManager.kt:43)
at org.robolectric.RobolectricTestRunner.getSandbox(RobolectricTestRunner.java:248)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunner.sdkEnvironment(JUnit5RobolectricTestRunner.kt:51)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunner.bootstrapSdkEnvironment(JUnit5RobolectricTestRunner.kt:48)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.sdkEnvironment(JUnit5RobolectricTestRunnerHelper.kt:29)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.proceedInvocation(JUnit5RobolectricTestRunnerHelper.kt:66)
at tech.apter.junit.jupiter.robolectric.RobolectricExtension.interceptAfterAllMethod(RobolectricExtension.kt:145)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at java.base/java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1085)
... 1 more
Caused by: java.io.FileNotFoundException: C:\Users\vkuts\org.robolectric:android-all-instrumented:13-robolectric-9030017-i6.lock (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.RandomAccessFile.open0(Native Method)
at java.base/java.io.RandomAccessFile.open(RandomAccessFile.java:345)
at java.base/java.io.RandomAccessFile.(RandomAccessFile.java:259)
at java.base/java.io.RandomAccessFile.(RandomAccessFile.java:214)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5MavenDependencyResolver.whileLocked(JUnit5MavenDependencyResolver.kt:73)
... 17 more
Caused by: java.io.FileNotFoundException: C:\Users\vkuts\org.robolectric:android-all-instrumented:13-robolectric-9030017-i6.lock (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.RandomAccessFile.open0(Native Method)
at java.base/java.io.RandomAccessFile.open(RandomAccessFile.java:345)
at java.base/java.io.RandomAccessFile.(RandomAccessFile.java:259)
at java.base/java.io.RandomAccessFile.(RandomAccessFile.java:214)
at tech.apter.junit.jupiter.robolectric.internal.JUnit5MavenDependencyResolver.whileLocked(JUnit5MavenDependencyResolver.kt:73)
... 15 more

Unify main thread naming with mainline Robolectric

I'm investigating the integration of the Roborazzi library with JUnit 5 via this extension and it works pretty well already. However, one thing I found is that it expects the main thread spun up by Robolectric to conform to a specific naming convention. You can see here that they expect the thread to have either Main Thread or Test worker in its name.

In the mainline Robolectric repository, the naming convention seems to be SDK xx Main Thread (link), but the JUnit 5 extension uses Main-Thread-<threadId>-SDK-xx instead (link). This causes the file name lookup function from Roborazzi to fail to detect the test method correctly.

FWIW, I have to submit some changes to Roborazzi anyway so that it can fully embrace JUnit 5, so I can also just extend their main thread checking logic while I'm at it. However, can we unify this name to match up with mainline Robolectric, or is there a reason for the difference (parallel test execution, maybe)? I can provide a pull request if necessary. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.