Skip to content

Commit 52703c9

Browse files
committed
Update to JUnit 5.11 and add unit tests for the new FieldSource
1 parent 7524f11 commit 52703c9

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

build-logic/src/main/kotlin/Dependencies.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
object libs {
44
object versions {
55
const val kotlin = "1.9.23"
6-
const val junitJupiter = "5.10.2"
7-
const val junitVintage = "5.10.2"
8-
const val junitPlatform = "1.10.2"
6+
const val junitJupiter = "5.11.0-M2"
7+
const val junitVintage = "5.11.0-M2"
8+
const val junitPlatform = "1.11.0-M2"
99

1010
const val composeBom = "2024.04.00"
1111
const val androidXTest = "1.5.0"

instrumentation/sample/src/androidTest/kotlin/de/mannodermaus/sample/ActivityOneTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@ import org.junit.jupiter.api.Tag
1818
import org.junit.jupiter.api.Test
1919
import org.junit.jupiter.api.extension.RegisterExtension
2020
import org.junit.jupiter.params.ParameterizedTest
21+
import org.junit.jupiter.params.provider.FieldSource
2122
import org.junit.jupiter.params.provider.ValueSource
23+
import java.util.function.Supplier
24+
import java.util.stream.Stream
2225

2326
class ActivityOneTest {
27+
companion object {
28+
val someLettersOfTheAlphabet = Supplier { Stream.of("A", "B", "C") }
29+
}
2430

2531
@JvmField
2632
@RegisterExtension
@@ -62,6 +68,20 @@ class ActivityOneTest {
6268
}
6369
}
6470

71+
@FieldSource("someLettersOfTheAlphabet")
72+
@ParameterizedTest
73+
fun parameterizedTestWithFieldSource(letter: String) {
74+
scenarioExtension.scenario.onActivity {
75+
it.setButtonLabel(letter)
76+
}
77+
78+
onView(withText(letter)).perform(click())
79+
80+
scenarioExtension.scenario.onActivity {
81+
assertEquals(1, it.getClickCount())
82+
}
83+
}
84+
6585
@RepeatedTest(3)
6686
fun repeatedTestExample(repetitionInfo: RepetitionInfo, scenario: ActivityScenario<ActivityOne>) {
6787
val count = repetitionInfo.currentRepetition

instrumentation/sample/src/test/kotlin/de/mannodermaus/sample/ExampleKotlinTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.junit.jupiter.api.AfterAll
44
import org.junit.jupiter.api.AfterEach
55
import org.junit.jupiter.api.Assertions.assertAll
66
import org.junit.jupiter.api.Assertions.assertEquals
7+
import org.junit.jupiter.api.Assertions.assertNotEquals
78
import org.junit.jupiter.api.Assertions.assertNotNull
89
import org.junit.jupiter.api.Assertions.assertTrue
910
import org.junit.jupiter.api.BeforeAll
@@ -19,6 +20,7 @@ import org.junit.jupiter.api.TestFactory
1920
import org.junit.jupiter.api.TestInfo
2021
import org.junit.jupiter.api.function.Executable
2122
import org.junit.jupiter.params.ParameterizedTest
23+
import org.junit.jupiter.params.provider.FieldSource
2224
import org.junit.jupiter.params.provider.MethodSource
2325
import org.junit.jupiter.params.provider.ValueSource
2426

@@ -51,6 +53,8 @@ class ExampleKotlinTest {
5153

5254
@JvmStatic
5355
fun getNames() = listOf("Alice" to "ALICE", "Bob" to "BOB", "Carol" to "CAROL")
56+
57+
val somePrimeNumbers = intArrayOf(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
5458
}
5559

5660
@BeforeEach
@@ -107,10 +111,18 @@ class ExampleKotlinTest {
107111

108112
@ParameterizedTest(name = "Upper case for {0}")
109113
@MethodSource("getNames")
110-
fun parameterizedMethodTest (names: Pair<String, String>) {
114+
fun parameterizedMethodTest(names: Pair<String, String>) {
111115
assertEquals(names.second, names.first.uppercase())
112116
}
113117

118+
@ParameterizedTest(name = "New FieldSource from 5.11")
119+
@FieldSource("somePrimeNumbers")
120+
fun parameterizedFieldTest(number: Int) {
121+
for (i in 2 until number) {
122+
assertNotEquals(0, number % i)
123+
}
124+
}
125+
114126
@Nested
115127
@DisplayName("Nested Class With Distinct Name")
116128
internal inner class NestedTestClass {

0 commit comments

Comments
 (0)