From 3af93b86158b07c7511ccd283653db34a0421f00 Mon Sep 17 00:00:00 2001 From: Marcel Schnelle Date: Wed, 22 May 2024 22:55:39 +0900 Subject: [PATCH] Update to JUnit 5.11 and add unit tests for the new FieldSource --- build-logic/src/main/kotlin/Dependencies.kt | 6 +++--- .../de/mannodermaus/sample/ActivityOneTest.kt | 20 +++++++++++++++++++ .../mannodermaus/sample/ExampleKotlinTest.kt | 14 ++++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/build-logic/src/main/kotlin/Dependencies.kt b/build-logic/src/main/kotlin/Dependencies.kt index cec34331..f60eb160 100644 --- a/build-logic/src/main/kotlin/Dependencies.kt +++ b/build-logic/src/main/kotlin/Dependencies.kt @@ -3,9 +3,9 @@ object libs { object versions { const val kotlin = "1.9.23" - const val junitJupiter = "5.10.2" - const val junitVintage = "5.10.2" - const val junitPlatform = "1.10.2" + const val junitJupiter = "5.11.0-M2" + const val junitVintage = "5.11.0-M2" + const val junitPlatform = "1.11.0-M2" const val composeBom = "2024.04.00" const val androidXTest = "1.5.0" diff --git a/instrumentation/sample/src/androidTest/kotlin/de/mannodermaus/sample/ActivityOneTest.kt b/instrumentation/sample/src/androidTest/kotlin/de/mannodermaus/sample/ActivityOneTest.kt index e928a877..86bf8c4d 100644 --- a/instrumentation/sample/src/androidTest/kotlin/de/mannodermaus/sample/ActivityOneTest.kt +++ b/instrumentation/sample/src/androidTest/kotlin/de/mannodermaus/sample/ActivityOneTest.kt @@ -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 @@ -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) { val count = repetitionInfo.currentRepetition diff --git a/instrumentation/sample/src/test/kotlin/de/mannodermaus/sample/ExampleKotlinTest.kt b/instrumentation/sample/src/test/kotlin/de/mannodermaus/sample/ExampleKotlinTest.kt index 093ca19b..6c95568e 100644 --- a/instrumentation/sample/src/test/kotlin/de/mannodermaus/sample/ExampleKotlinTest.kt +++ b/instrumentation/sample/src/test/kotlin/de/mannodermaus/sample/ExampleKotlinTest.kt @@ -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 @@ -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 @@ -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 @@ -107,10 +111,18 @@ class ExampleKotlinTest { @ParameterizedTest(name = "Upper case for {0}") @MethodSource("getNames") - fun parameterizedMethodTest (names: Pair) { + fun parameterizedMethodTest(names: Pair) { 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 {