3

Here is an example of Parameterized test in JUnit 5.

@ParameterizedTest
@MethodSource("generalDataset")
fun shouldCreateItem(color: String, size: String) {
    val item = Item(color, size)

    assertThat(item.color).isEqualTo(color)
}

@JvmStatic
fun generalDataset() =
    Stream.of(
        arguments("blue", "S"),
        arguments("red", "S"),
        arguments("green", "S"),
        arguments("blue", "L"),
        arguments("red", "L"),
        arguments("green", "L")
    )

See that generalDataset is a multiplcation of two sets {"blue", "red", "green"} x {"S", "L"}.

To avoid duplication it would be great to describe it explicitly like this pseudocode

@ParameterizedTest
@MethodSource("colorDataset"  %MULTIPLY%  "sizeDataset")
fun shouldCreateItem(color: String, size: String) {
    val item = Item(color, size)

    assertThat(item.color).isEqualTo(color)
}

@JvmStatic
fun colorDataset() =
    Stream.of(
        arguments("blue"),
        arguments("red"),
        arguments("green") 
    ) 

@JvmStatic
fun sizeDataset() =
    Stream.of(
        arguments("S"),
        arguments("L") 
    )

Is it possible to implement something similar (Parameterized test with multiplied sources) in JUnit 5?

diziaq
  • 6,881
  • 16
  • 54
  • 96
  • 2
    I found this a while ago: https://junit-pioneer.org/docs/cartesian-product/. Is that what you're looking for? – Rob Spoor Feb 07 '22 at 14:06
  • @RobSpoor Wow! Thank you. Looks promising. Thought that JUnit have this out of the box. – diziaq Feb 07 '22 at 14:12
  • Not really an answer, but you already have static methods for defining your test parameters. Such an annotation would not remove the need for them. Therefore I would question wether it is usefull to have such an implementation when there is no escaping writing custom methods, and you could end up with `colors.flatMap(color -> sizes.stream(size -> arguments(color, size)))` Just a thought. Unless you have lots of places you really need this that is. – GPI Feb 07 '22 at 14:13

2 Answers2

1

If you want to avoid using external library I wrote some code to do Cartesian product, not that much tested, but simple and works

Edit: I have optimized the code to memorize recursive call

class SomeTestClass {

    @ParameterizedTest
    @MethodSource("SomeTestClassKt#provideSomeData")
    fun someTest(first: String, second: Boolean, third: Int) {
        println("first = $first, second = $second, third = $third")
    }

}

private fun provideSomeData(): Stream<Arguments> {
    return cartesianArguments(
        listOf("Product1", "Product2", "Product3"),
        listOf(true, false),
        listOf(1, 5, 12)
    )
}

inline fun <reified T> cartesianArguments(vararg input: List<T>): Stream<Arguments> {
    return cartesianRecurrence(input.toList())
        .stream()
        .map { Arguments.of(*it.toTypedArray()) }

}


fun <T> cartesianRecurrence(input: List<List<T>>): List<List<T>> {
    if (input.size < 2)
        return input.flatten().map { listOf(it) }

    val result = cartesianRecurrence(input.tail)
    return input.head.flatMap { headElement ->
        result.map { headElement + it }
    }
}

operator fun <T> T.plus(tail: List<T>): List<T> {
    val list = ArrayList<T>(1 + tail.size)

    list.add(this)
    list.addAll(tail)

    return list
}

val <T> List<T>.tail: List<T>
    get() = drop(1)

val <T> List<T>.head: T
    get() = first()

This produces result:

first = Product1, second = true, third = 1
first = Product1, second = true, third = 5
first = Product1, second = true, third = 12
first = Product1, second = false, third = 1
first = Product1, second = false, third = 5
first = Product1, second = false, third = 12
first = Product2, second = true, third = 1
first = Product2, second = true, third = 5
first = Product2, second = true, third = 12
first = Product2, second = false, third = 1
first = Product2, second = false, third = 5
first = Product2, second = false, third = 12
first = Product3, second = true, third = 1
first = Product3, second = true, third = 5
first = Product3, second = true, third = 12
first = Product3, second = false, third = 1
first = Product3, second = false, third = 5
first = Product3, second = false, third = 12
wokstym
  • 143
  • 1
  • 1
  • 12
0

You can specify a method as the source. In that method you can provide the cartesian product.

Simple example:

@ParameterizedTest
@MethodSource("provideStringsForIsBlank")
...

private static Stream<Arguments> provideStringsForIsBlank() {
    return Stream.of(
      Arguments.of(null, true),
      Arguments.of("", true),
      Arguments.of("  ", true),
      Arguments.of("not blank", false)
    );
}

Reference: https://www.baeldung.com/parameterized-tests-junit-5

pringi
  • 3,987
  • 5
  • 35
  • 45