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
51 stars 2 forks source link

project kotlin multiplatform : error failed to resolve tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin 0.8.0 #95

Closed fpitpit closed 2 weeks ago

fpitpit commented 1 month ago

In a Kotlin mutliplatform I try to add plugin for android module using version catalog but build failed to resolve the plugin

version catalog :

[versions]
junit5-robolectric = "0.8.0"
[plugins]
junit5-robolectric = { id = "tech.apter.junit5.jupiter.robolectric-extension-gradle-plugin", version.ref = "junit5-robolectric" } 

build.gradle.kts (root project)

plugins {
    // this is necessary to avoid the plugins to be loaded multiple times
    // in each subproject's classloader
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.androidLibrary) apply false
    alias(libs.plugins.jetbrainsCompose) apply false
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.kotlinMultiplatform) apply false
    alias(libs.plugins.junit5.robolectric) apply  false
}

build.gradle.kts (composeApp/android)

android {
    apply(plugin = libs.plugins.junit5.robolectric.get().pluginId)
    ....
}

I've got error : Failed to resolve: tech.apter.junit5.jupiter:robolectric-extension:0.8.0

I've same error on a classic project android

all tests failed now on kotlin multiplatform : if I reuse junit4 and run with robolectric test runner it works but not with your plugin

fpitpit commented 1 month ago

in kotlin multiplatform all android all tests passed but it said ` Task :composeApp:testDebugUnitTest FAILED FAILURE: Build failed with an exception.

` when I launch gradlew composeApp:testDebugUnitTest or testReleaseUnitTest.

This is my actual class for android :

`

@ExtendWith(RobolectricExtension::class) @Config( application = WhoIsApplication::class, sdk = [Config.OLDEST_SDK, Build.VERSION_CODES.UPSIDE_DOWN_CAKE]) @Suppress("EXPECT_ACTUAL_CLASSIFIERS_ARE_IN_BETA_WARNING") actual abstract class UsingContext actual constructor() { init { println("test is executed on ${buildPlatform().name}") }

@BeforeTest
open fun setUp() {
    ShadowLog.stream = System.out
}

@OptIn(ExperimentalResourceApi::class)
@Composable
actual fun initPreviewContextConfiguration() {
    CompositionLocalProvider(LocalInspectionMode provides true) {
        PreviewContextConfigurationEffect()
    }
}

}

`

`

Caused by: org.junit.runners.model.InvalidTestClassError: Invalid test class '.........UsingContext':

  1. No runnable methods at org.junit.runners.ParentRunner.validate(ParentRunner.java:525) at org.junit.runners.ParentRunner.(ParentRunner.java:92) at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:74) at org.robolectric.internal.SandboxTestRunner.(SandboxTestRunner.java:77) at org.robolectric.RobolectricTestRunner.(RobolectricTestRunner.java:105) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunner.(JUnit5RobolectricTestRunner.kt:27) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunner.(JUnit5RobolectricTestRunner.kt:21) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.createTestEnvironmentForClass(JUnit5RobolectricTestRunnerHelper.kt:37) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.(JUnit5RobolectricTestRunnerHelper.kt:21) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper.(JUnit5RobolectricTestRunnerHelper.kt) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper$Companion.getInstance$robolectric_extension(JUnit5RobolectricTestRunnerHelper.kt:99) at tech.apter.junit.jupiter.robolectric.internal.RobolectricClassLoaderFactoryKt.robolectricClassLoaderFactory(RobolectricClassLoaderFactory.kt:17) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper$Companion$setUp$2.invoke(JUnit5RobolectricTestRunnerHelper.kt:84) at tech.apter.junit.jupiter.robolectric.internal.JUnit5RobolectricTestRunnerHelper$Companion$setUp$2.invoke(JUnit5RobolectricTestRunnerHelper.kt:84) at tech.apter.junit.jupiter.robolectric.internal.RobolectricTestClassClassLoader.loadClass(RobolectricTestClassClassLoader.kt:29) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:495) at java.base/java.lang.Class.forName(Class.java:474) at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.loadClass(JUnitPlatformTestClassProcessor.java:173) at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.access$100(JUnitPlatformTestClassProcessor.java:62) at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.execute(JUnitPlatformTestClassProcessor.java:109) at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.execute(JUnitPlatformTestClassProcessor.java:99) at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:54) at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:53)

`

fpitpit commented 1 month ago

ok the plugin believes that my actual class is a Test so it failed (Gradle Test Executor 1)

To fix it I must exclude my actual class in test block in my gradle file :

tasks.withType<Test>().configureEach { useJUnitPlatform() exclude("**/UsingContext.class") }

warnyul commented 3 weeks ago

Why your UsingContext class is annotated with @ExtendWith(RobolectricExtension::class) if it is not a test class?

Probably that causing the issue.

mannodermaus commented 3 weeks ago

I'm seeing this too, but with a pure-Android project. It seems like version 0.8.0 of the Gradle plugin exists on the Gradle Plugin Portal, however the extension itself has no such version available on Maven Central. Is the extension deployed to a different repository with the latest version, or did something in the deployment go awry?

Screenshot 2024-10-23 at 11 08 52
warnyul commented 3 weeks ago

Apologies for the confusion. I mistakenly only closed the release on OSS. Could you please check again? Sorry for the inconvenience. https://repo1.maven.org/maven2/tech/apter/junit5/jupiter/robolectric-extension/0.8.0/

warnyul commented 3 weeks ago

@fpitpit Could you confirm this is solved your issue? Am I close this issue?

mannodermaus commented 3 weeks ago

I can confirm that resolving version 0.8.0 of the plugin now also fetches the extension without issue. Thank you for checking this!

Screenshot 2024-10-23 at 17 54 49
fpitpit commented 3 weeks ago

Yes good job. But I see that reports testDebugUnitTest and testReleaseUnitTest not included in all tests report. I've must go in reports/tests/testDebugUnitTest to see them.

And with junit4 I saw result by API android but not with your plugin. I put config robolectric with API 24 and API 34

warnyul commented 2 weeks ago

@fpitpit Thank you for your confirmation. Please open a separate issue about the test report, and attach a test project where the issue can be reproduced.