How to pass a space as value in @CsvSource

272 Views Asked by At

Cannot figure out the syntax to pass a space as a value in jupiter's @CsvSource

@ParameterizedTest
@CsvSource({
                " ,lastName"
})
void test(String firstName, String lastName) {
...
}

but firstName is null and not a space character. Is it doable at all with this annotation?

2

There are 2 best solutions below

0
On BEST ANSWER

Figured it out. I had to surround the space with single quotation marks as shown below:

@CsvSource({
                "' ',lastName"
})
4
On

you can try one of this solutions:

  1. You can escape space value using double quotes, like this:
@ParameterizedTest
@CsvSource({
    "\" \",lastName"
})
void test(String firstName, String lastName) {
    // Your test logic
}

  1. You can use an escape sequence for a space, such as \s. This should ensure that a space character is passed as the value for firstName, like this:
@ParameterizedTest
@CsvSource({
    "\\s,lastName"
})
void test(String firstName, String lastName) {
     // Your test logic
}