Skip to content

Commit c8e2fe1

Browse files
committed
Make plugin tests run with JU5&6
1 parent 33b32a6 commit c8e2fe1

8 files changed

Lines changed: 93 additions & 66 deletions

File tree

plugin/android-junit5/build.gradle.kts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ tasks.named("processTestResources", Copy::class.java).configure {
112112
"TARGET_SDK_VERSION" to Android.targetSdkVersion.toString(),
113113

114114
"KOTLIN_VERSION" to libs.versions.kotlin.get(),
115-
"JUNIT_JUPITER_VERSION" to libs.versions.junit5.get(), // TODO
115+
"JUNIT5_VERSION" to libs.versions.junit5.get(),
116+
"JUNIT6_VERSION" to libs.versions.junit6.get(),
116117
"JUNIT5_ANDROID_LIBS_VERSION" to Artifacts.Instrumentation.Core.latestStableVersion,
117118

118119
// Collect all supported AGP versions into a single string.

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/ConfigurationCacheTests.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package de.mannodermaus.gradle.plugins.junit5
22

33
import de.mannodermaus.gradle.plugins.junit5.annotations.DisabledOnCI
44
import de.mannodermaus.gradle.plugins.junit5.util.TestEnvironment
5+
import de.mannodermaus.gradle.plugins.junit5.util.TestedJUnit
56
import de.mannodermaus.gradle.plugins.junit5.util.assertThat
67
import de.mannodermaus.gradle.plugins.junit5.util.prettyPrint
78
import de.mannodermaus.gradle.plugins.junit5.util.projects.FunctionalTestProjectCreator
@@ -16,6 +17,8 @@ import org.junit.jupiter.api.Test
1617
import org.junit.jupiter.api.TestInstance
1718
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
1819
import org.junit.jupiter.api.io.TempDir
20+
import org.junit.jupiter.params.ParameterizedTest
21+
import org.junit.jupiter.params.provider.EnumSource
1922
import java.io.File
2023

2124
@TestInstance(PER_CLASS)
@@ -36,11 +39,12 @@ class ConfigurationCacheTests {
3639
Runtime.getRuntime().exec("adb disconnect".splitToArray(" "))
3740
}
3841

39-
@Test
40-
fun `test instrumentation tasks`() {
42+
@EnumSource(TestedJUnit::class)
43+
@ParameterizedTest
44+
fun `test instrumentation tasks`(junit: TestedJUnit) {
4145
// Test configuration cache with one specific project and AGP version
4246
val spec = projectCreator.specNamed("instrumentation-tests")
43-
val project = projectCreator.createProject(spec, agp)
47+
val project = projectCreator.createProject(spec, agp, junit)
4448

4549
// Run it once; this is supposed to fail, but JUST because of 'no connected device',
4650
// not because of other errors including the configuration cache.
@@ -55,10 +59,11 @@ class ConfigurationCacheTests {
5559
}
5660
}
5761

58-
@Test
59-
fun `test unit tasks`() {
62+
@EnumSource(TestedJUnit::class)
63+
@ParameterizedTest
64+
fun `test unit tasks`(junit: TestedJUnit) {
6065
val spec = projectCreator.specNamed("product-flavors")
61-
val project = projectCreator.createProject(spec, agp)
66+
val project = projectCreator.createProject(spec, agp, junit)
6267

6368
runGradle(project, "help", expectSuccess = true).assertWithLogging {
6469
assertThat(it).task(":help").hasOutcome(SUCCESS)

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/FunctionalTests.kt

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import de.mannodermaus.gradle.plugins.junit5.annotations.DisabledOnCI
55
import de.mannodermaus.gradle.plugins.junit5.util.BuildResultSubject
66
import de.mannodermaus.gradle.plugins.junit5.util.TestEnvironment
77
import de.mannodermaus.gradle.plugins.junit5.util.TestedAgp
8+
import de.mannodermaus.gradle.plugins.junit5.util.TestedJUnit
89
import de.mannodermaus.gradle.plugins.junit5.util.prettyPrint
910
import de.mannodermaus.gradle.plugins.junit5.util.projects.FunctionalTestProjectCreator
1011
import de.mannodermaus.gradle.plugins.junit5.util.withPrunedPluginClasspath
@@ -67,60 +68,62 @@ class FunctionalTests {
6768

6869
// Generate a container for all tests with this specific AGP/Language combination
6970
dynamicContainer("AGP ${agp.shortVersion}",
70-
7171
// Exercise each test project within the given environment
72-
projectCreator.allSpecs.filterSpecs().map { spec ->
73-
dynamicTest(spec.name) {
74-
// Required for visibility inside IJ's logging console (display names are still bugged in the IDE)
75-
println(buildList {
76-
add("AGP: ${agp.version}")
77-
add("Project: ${spec.name}")
78-
add("Gradle: ${agp.requiresGradle}")
79-
agp.requiresCompileSdk?.let { add("SDK: $it") }
80-
}.joinToString(", "))
81-
82-
// Create a virtual project with the given settings & AGP version.
83-
// This call will throw a TestAbortedException if the spec is not eligible for this version,
84-
// marking the test as ignored in the process
85-
val project = projectCreator.createProject(spec, agp)
86-
87-
// Execute the tests of the virtual project with Gradle
88-
val taskName = spec.task ?: "test"
89-
val result = runGradle(agp, taskName)
90-
.withProjectDir(project)
91-
.build()
92-
93-
// Print Gradle logs from the embedded invocation
94-
result.prettyPrint()
95-
96-
// Check that the task execution was successful in general
97-
val outcome = result.task(":$taskName")?.outcome
98-
when {
99-
outcome == TaskOutcome.UP_TO_DATE -> {
100-
// Nothing to do, a previous build already checked this
101-
println("Task '$taskName' up-to-date; skipping assertions.")
102-
}
72+
projectCreator.allSpecs.filterSpecs().flatMap { spec ->
73+
TestedJUnit.entries.map { junit ->
74+
dynamicTest("${spec.name} ($junit)") {
75+
// Required for visibility inside IJ's logging console (display names are still bugged in the IDE)
76+
println(buildList {
77+
add("AGP: ${agp.version}")
78+
add("Project: ${spec.name}")
79+
add("Gradle: ${agp.requiresGradle}")
80+
agp.requiresCompileSdk?.let { add("SDK: $it") }
81+
add(junit.name)
82+
}.joinToString())
83+
84+
// Create a virtual project with the given settings & AGP version.
85+
// This call will throw a TestAbortedException if the spec is not eligible for this version,
86+
// marking the test as ignored in the process
87+
val project = projectCreator.createProject(spec, agp, junit)
88+
89+
// Execute the tests of the virtual project with Gradle
90+
val taskName = spec.task ?: "test"
91+
val result = runGradle(agp, taskName)
92+
.withProjectDir(project)
93+
.build()
94+
95+
// Print Gradle logs from the embedded invocation
96+
result.prettyPrint()
97+
98+
// Check that the task execution was successful in general
99+
val outcome = result.task(":$taskName")?.outcome
100+
when {
101+
outcome == TaskOutcome.UP_TO_DATE -> {
102+
// Nothing to do, a previous build already checked this
103+
println("Task '$taskName' up-to-date; skipping assertions.")
104+
}
103105

104-
outcome == TaskOutcome.SUCCESS -> {
105-
// Based on the spec's configuration in the test project,
106-
// assert that all test classes have been executed as expected
107-
for (expectation in spec.expectedTests) {
108-
result.assertAgpTests(
109-
buildType = expectation.buildType,
110-
productFlavor = expectation.productFlavor,
111-
tests = expectation.testsList
112-
)
106+
outcome == TaskOutcome.SUCCESS -> {
107+
// Based on the spec's configuration in the test project,
108+
// assert that all test classes have been executed as expected
109+
for (expectation in spec.expectedTests) {
110+
result.assertAgpTests(
111+
buildType = expectation.buildType,
112+
productFlavor = expectation.productFlavor,
113+
tests = expectation.testsList
114+
)
115+
}
113116
}
114-
}
115117

116-
outcome == TaskOutcome.SKIPPED && spec.allowSkipped -> {
117-
// It might be acceptable to allow "skipped" as the result depending on the test spec
118-
println("Task '$taskName' was skipped.")
119-
}
118+
outcome == TaskOutcome.SKIPPED && spec.allowSkipped -> {
119+
// It might be acceptable to allow "skipped" as the result depending on the test spec
120+
println("Task '$taskName' was skipped.")
121+
}
120122

121-
else -> {
122-
// Unexpected result; fail
123-
fail { "Unexpected task outcome: $outcome\n\nRaw output:\n\n${result.output}" }
123+
else -> {
124+
// Unexpected result; fail
125+
fail { "Unexpected task outcome: $outcome\n\nRaw output:\n\n${result.output}" }
126+
}
124127
}
125128
}
126129
}

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/util/TestEnvironment.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ private const val COMPILE_SDK_PROP_NAME = "COMPILE_SDK_VERSION"
1313
private const val MIN_SDK_PROP_NAME = "MIN_SDK_VERSION"
1414
private const val TARGET_SDK_PROP_NAME = "TARGET_SDK_VERSION"
1515
private const val KOTLIN_VERSION_PROP_NAME = "KOTLIN_VERSION"
16-
private const val JUNIT_JUPITER_PROP_NAME = "JUNIT_JUPITER_VERSION"
16+
private const val JUNIT5_PROP_NAME = "JUNIT5_VERSION"
17+
private const val JUNIT6_PROP_NAME = "JUNIT6_VERSION"
1718
private const val JUNIT5_ANDROID_PROP_NAME = "JUNIT5_ANDROID_LIBS_VERSION"
1819
private const val AGP_VERSIONS_PROP_NAME = "AGP_VERSIONS"
1920

@@ -37,7 +38,8 @@ class TestEnvironment {
3738
val targetSdkVersion: Int
3839

3940
val kotlinVersion: String
40-
val junitJupiterVersion: String
41+
val junit5Version: String
42+
val junit6Version: String
4143
val junit5AndroidLibsVersion: String
4244

4345
val supportedAgpVersions: List<TestedAgp>
@@ -47,7 +49,8 @@ class TestEnvironment {
4749
minSdkVersion = envProps.getProperty(MIN_SDK_PROP_NAME).toInt()
4850
targetSdkVersion = envProps.getProperty(TARGET_SDK_PROP_NAME).toInt()
4951
kotlinVersion = envProps.getProperty(KOTLIN_VERSION_PROP_NAME)
50-
junitJupiterVersion = envProps.getProperty(JUNIT_JUPITER_PROP_NAME)
52+
junit5Version = envProps.getProperty(JUNIT5_PROP_NAME)
53+
junit6Version = envProps.getProperty(JUNIT6_PROP_NAME)
5154
junit5AndroidLibsVersion = envProps.getProperty(JUNIT5_ANDROID_PROP_NAME)
5255

5356
// Each entry in this string is separated by semicolon.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package de.mannodermaus.gradle.plugins.junit5.util
2+
3+
enum class TestedJUnit(val envPropName: String) {
4+
JUnit5("JUNIT5_VERSION"),
5+
JUnit6("JUNIT6_VERSION")
6+
}

plugin/android-junit5/src/test/kotlin/de/mannodermaus/gradle/plugins/junit5/util/projects/FunctionalTestProjectCreator.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.uchuhimo.konf.ConfigSpec
55
import com.uchuhimo.konf.source.toml
66
import de.mannodermaus.gradle.plugins.junit5.util.TestEnvironment
77
import de.mannodermaus.gradle.plugins.junit5.util.TestedAgp
8+
import de.mannodermaus.gradle.plugins.junit5.util.TestedJUnit
89
import org.gradle.util.GradleVersion
910
import org.junit.jupiter.api.Assumptions.assumeTrue
1011
import org.opentest4j.TestAbortedException
@@ -43,7 +44,7 @@ class FunctionalTestProjectCreator(
4344
?: throw IllegalAccessException("No test project named '$name' found in src/test/resources/test-projects")
4445

4546
@Throws(TestAbortedException::class)
46-
fun createProject(spec: Spec, agp: TestedAgp): File {
47+
fun createProject(spec: Spec, agp: TestedAgp, junit: TestedJUnit): File {
4748
// Validate the spec requirement against the executing AGP version first
4849
validateSpec(spec, agp)
4950

@@ -86,6 +87,11 @@ class FunctionalTestProjectCreator(
8687
replacements["INCLUDE_ANDROID_RESOURCES"] = spec.includeAndroidResources
8788
replacements["DISABLE_TESTS_FOR_BUILD_TYPES"] = spec.disableTestsForBuildTypes
8889

90+
replacements["JUNIT_VERSION"] = when (junit) {
91+
TestedJUnit.JUnit5 -> environment.junit5Version
92+
TestedJUnit.JUnit6 -> environment.junit6Version
93+
}
94+
8995
agp.requiresCompileSdk?.let {
9096
replacements["OVERRIDE_SDK_VERSION"] = it
9197
}

plugin/android-junit5/src/test/resources/de/mannodermaus/gradle/plugins/junit5/testenv.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ COMPILE_SDK_VERSION = @COMPILE_SDK_VERSION@
44
MIN_SDK_VERSION = @MIN_SDK_VERSION@
55
TARGET_SDK_VERSION = @TARGET_SDK_VERSION@
66

7-
KOTLIN_VERSION = @KOTLIN_VERSION@
8-
JUNIT_JUPITER_VERSION = @JUNIT_JUPITER_VERSION@
7+
KOTLIN_VERSION = @KOTLIN_VERSION@
8+
JUNIT5_VERSION = @JUNIT5_VERSION@
9+
JUNIT6_VERSION = @JUNIT6_VERSION@
910
JUNIT5_ANDROID_LIBS_VERSION = @JUNIT5_ANDROID_LIBS_VERSION@
1011

1112
AGP_VERSIONS = @AGP_VERSIONS@

plugin/android-junit5/src/test/resources/test-projects/build.gradle.kts.template

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
1414

1515
val androidGradlePluginVersion: String = "{{ AGP_VERSION }}"
1616
val kotlinVersion: String = "{{ KOTLIN_VERSION }}"
17-
val junitJupiterVersion: String = "{{ JUNIT_JUPITER_VERSION }}"
17+
val junitVersion: String = "{{ JUNIT_VERSION }}"
1818
val junit5AndroidLibsVersion: String = "{{ JUNIT5_ANDROID_LIBS_VERSION }}"
1919
val javaVersion = JavaVersion.VERSION_17
2020

@@ -163,12 +163,14 @@ dependencies {
163163
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
164164
{% endif %}
165165

166-
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
167-
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
166+
testImplementation(platform("org.junit:junit-bom:${junitVersion}"))
167+
testImplementation("org.junit.jupiter:junit-jupiter-api")
168+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
168169

169170
{% if INCLUDE_ANDROID_RESOURCES %}
170171
androidTestImplementation("androidx.test:runner:1.4.0")
171-
androidTestImplementation("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
172+
androidTestImplementation(platform("org.junit:junit-bom:${junitVersion}"))
173+
androidTestImplementation("org.junit.jupiter:junit-jupiter-api")
172174

173175
androidTestImplementation("de.mannodermaus.junit5:android-test-core:${junit5AndroidLibsVersion}")
174176
androidTestRuntimeOnly("de.mannodermaus.junit5:android-test-runner:${junit5AndroidLibsVersion}")

0 commit comments

Comments
 (0)