Skip to content

Commit

Permalink
JUnit 5.11 (#349)
Browse files Browse the repository at this point in the history
* Update to JUnit 5.11 and add unit tests for the new FieldSource

* Change readme generation task to abstract

* JUnit 5.11 stable
  • Loading branch information
mannodermaus authored Aug 14, 2024
1 parent 2cb0313 commit 4a842b7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 28 deletions.
6 changes: 3 additions & 3 deletions build-logic/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
object libs {
object versions {
const val kotlin = "1.9.23"
const val junitJupiter = "5.10.3"
const val junitVintage = "5.10.3"
const val junitPlatform = "1.10.3"
const val junitJupiter = "5.11.0"
const val junitVintage = "5.11.0"
const val junitPlatform = "1.11.0"

const val composeBom = "2024.04.00"
const val androidXTest = "1.6.1"
Expand Down
12 changes: 6 additions & 6 deletions build-logic/src/main/kotlin/Deployment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,12 @@ private fun MavenPublication.configurePom(deployConfig: Deployed) = also {
}
}

url.set(Artifacts.githubUrl)
url.set(Artifacts.GITHUB_URL)

licenses {
license {
name.set(Artifacts.license)
url.set("${Artifacts.githubUrl}/blob/main/LICENSE")
name.set(Artifacts.LICENSE)
url.set("${Artifacts.GITHUB_URL}/blob/main/LICENSE")
}
}

Expand All @@ -260,9 +260,9 @@ private fun MavenPublication.configurePom(deployConfig: Deployed) = also {
}

scm {
connection.set("scm:git:${Artifacts.githubRepo}.git")
developerConnection.set("scm:git:ssh://github.com/${Artifacts.githubRepo}.git")
url.set("${Artifacts.githubUrl}/tree/main")
connection.set("scm:git:${Artifacts.GITHUB_REPO}.git")
developerConnection.set("scm:git:ssh://github.com/${Artifacts.GITHUB_REPO}.git")
url.set("${Artifacts.GITHUB_URL}/tree/main")
}
}
}
Expand Down
12 changes: 3 additions & 9 deletions build-logic/src/main/kotlin/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ class Deployed internal constructor(
val currentVersion: String,
val latestStableVersion: String,
val description: String,
val license: String
)

object Artifacts {
val githubUrl = "https://github.com/mannodermaus/android-junit5"
val githubRepo = "mannodermaus/android-junit5"
val license = "Apache-2.0"
const val GITHUB_URL = "https://github.com/mannodermaus/android-junit5"
const val GITHUB_REPO = "mannodermaus/android-junit5"
const val LICENSE = "Apache-2.0"

/**
* Retrieve the artifact configuration based on a Gradle project reference.
Expand All @@ -90,7 +89,6 @@ object Artifacts {
artifactId = "android-junit5",
currentVersion = "1.11.0.0-SNAPSHOT",
latestStableVersion = "1.10.3.0",
license = license,
description = "Unit Testing with JUnit 5 for Android."
)

Expand All @@ -108,7 +106,6 @@ object Artifacts {
artifactId = "android-test-core",
currentVersion = currentVersion,
latestStableVersion = latestStableVersion,
license = license,
description = "Extensions for instrumented Android tests with JUnit 5."
)

Expand All @@ -118,7 +115,6 @@ object Artifacts {
artifactId = "android-test-extensions",
currentVersion = currentVersion,
latestStableVersion = latestStableVersion,
license = license,
description = "Optional extensions for instrumented Android tests with JUnit 5."
)

Expand All @@ -128,7 +124,6 @@ object Artifacts {
artifactId = "android-test-runner",
currentVersion = currentVersion,
latestStableVersion = latestStableVersion,
license = license,
description = "Runner for integration of instrumented Android tests with JUnit 5."
)

Expand All @@ -138,7 +133,6 @@ object Artifacts {
artifactId = "android-test-compose",
currentVersion = currentVersion,
latestStableVersion = latestStableVersion,
license = license,
description = "Extensions for Jetpack Compose tests with JUnit 5."
)
}
Expand Down
17 changes: 9 additions & 8 deletions build-logic/src/main/kotlin/Tasks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMEN
import org.gradle.api.attributes.plugin.GradlePluginApiVersion
import org.gradle.api.attributes.plugin.GradlePluginApiVersion.GRADLE_PLUGIN_API_VERSION_ATTRIBUTE
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import java.io.File
import java.time.ZonedDateTime
Expand Down Expand Up @@ -176,8 +177,7 @@ fun Copy.configureCreateVersionClassTask(
* Using a template file, the plugin's version constants & other dependency versions
* are automatically injected into the README.
*/
open class GenerateReadme : DefaultTask() {

abstract class GenerateReadme : DefaultTask() {
companion object {
private val PLACEHOLDER_REGEX = Regex("\\\$\\{(.+)}")
private val EXTERNAL_DEP_REGEX = Regex("libs\\.(.+)")
Expand All @@ -204,18 +204,19 @@ open class GenerateReadme : DefaultTask() {
""".trimIndent()
}

@InputFile
lateinit var inputTemplateFile: File
@get:InputFile
abstract val inputTemplateFile: RegularFileProperty

@OutputFile
lateinit var outputFile: File
@get:OutputFile
abstract val outputFile: RegularFileProperty

@TaskAction
fun doWork() {
val templateText = inputTemplateFile.readText()
val templateText = inputTemplateFile.asFile.get().readText()
val constants = parseConstantsFile()
val replacedText = replacePlaceholdersInTemplate(templateText, constants)
outputFile.writeText(replacedText)

outputFile.asFile.get().writeText(replacedText)
}

/* Private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.FieldSource
import org.junit.jupiter.params.provider.ValueSource
import java.util.function.Supplier
import java.util.stream.Stream

class ActivityOneTest {
companion object {
val someLettersOfTheAlphabet = Supplier { Stream.of("A", "B", "C") }
}

@JvmField
@RegisterExtension
Expand Down Expand Up @@ -62,6 +68,20 @@ class ActivityOneTest {
}
}

@FieldSource("someLettersOfTheAlphabet")
@ParameterizedTest
fun parameterizedTestWithFieldSource(letter: String) {
scenarioExtension.scenario.onActivity {
it.setButtonLabel(letter)
}

onView(withText(letter)).perform(click())

scenarioExtension.scenario.onActivity {
assertEquals(1, it.getClickCount())
}
}

@RepeatedTest(3)
fun repeatedTestExample(repetitionInfo: RepetitionInfo, scenario: ActivityScenario<ActivityOne>) {
val count = repetitionInfo.currentRepetition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeAll
Expand All @@ -19,6 +20,7 @@ import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.TestInfo
import org.junit.jupiter.api.function.Executable
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.FieldSource
import org.junit.jupiter.params.provider.MethodSource
import org.junit.jupiter.params.provider.ValueSource

Expand Down Expand Up @@ -51,6 +53,8 @@ class ExampleKotlinTest {

@JvmStatic
fun getNames() = listOf("Alice" to "ALICE", "Bob" to "BOB", "Carol" to "CAROL")

val somePrimeNumbers = intArrayOf(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
}

@BeforeEach
Expand Down Expand Up @@ -107,10 +111,18 @@ class ExampleKotlinTest {

@ParameterizedTest(name = "Upper case for {0}")
@MethodSource("getNames")
fun parameterizedMethodTest (names: Pair<String, String>) {
fun parameterizedMethodTest(names: Pair<String, String>) {
assertEquals(names.second, names.first.uppercase())
}

@ParameterizedTest(name = "New FieldSource from 5.11")
@FieldSource("somePrimeNumbers")
fun parameterizedFieldTest(number: Int) {
for (i in 2 until number) {
assertNotEquals(0, number % i)
}
}

@Nested
@DisplayName("Nested Class With Distinct Name")
internal inner class NestedTestClass {
Expand Down
1 change: 1 addition & 0 deletions plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Change Log
==========

## Unreleased
- JUnit 5.11

## 1.10.3.0 (2024-08-14)
- JUnit 5.10.3
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ apiValidation {

tasks.create<GenerateReadme>("generateReadme") {
// Find folder containing README.md
// (required because this script file is included through symlinks in sub-projects)
// (required because this script file is included through symlinks in subprojects)
var rootFolder: File? = project.rootDir
while (rootFolder != null && rootFolder.exists()) {
val inFile = File(rootFolder, "README.md.template")
Expand Down

0 comments on commit 4a842b7

Please sign in to comment.