简体   繁体   中英

JUnit 5 - Parameterized test default display name with @CsvSource when the parameter is an user defined class

I have a parameterized test that uses an user defined class as the parameter,

    @ParameterizedTest
    @CsvSource({ "1 a", "2 b" })
    @Order(8)
    void csvSourceIntString(IntString is) {
    }

IntString is define as follows, notice that I defined toString :

import java.util.Scanner;

class IntString {
    int i;
    String s;

    public IntString(int i, String s) {
        this.i = i;
        this.s = s;
    }

    public IntString(String is) {
        try (Scanner scanner = new Scanner(is)) {
            i = scanner.nextInt();
            s = scanner.next();
        }
    }

    @Override
    public String toString() {
        return "i=" + i + ",s=" + s;
    }
}

I was expecting the default display names of the tests to be

[1] i=1,s=a
[2] i=2,s=b

Instead, when I ran this in Eclipse, they are what is in @CsvSource

[1] 1 a
[2] 2 b

Is this expected?

BTW, @MethodSource gave me expected default display names.

You could try to set the name of the parameterized test explicitely:

@ParameterizedTest(name = "[{index}] {0}")
@CsvSource({ "1 a", "2 b" })
@Order(8)
void csvSourceIntString(IntString is) {
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM