Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply test data sets (cartesian product) in JUnit 5 Parameterized test?

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?

like image 892
diziaq Avatar asked Nov 18 '25 12:11

diziaq


1 Answers

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
like image 111
wokstym Avatar answered Nov 20 '25 03:11

wokstym