Skip to content

Commit

Permalink
Update to JUnit 5.11 and add unit tests for the new FieldSource
Browse files Browse the repository at this point in the history
  • Loading branch information
mannodermaus committed May 22, 2024
1 parent 050ccce commit 3af93b8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 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.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"
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

0 comments on commit 3af93b8

Please sign in to comment.