DEV Community

Rafał Garbowski
Rafał Garbowski

Posted on • Edited on • Originally published at rg9.dev

2

JUnit's @CsvSource.quoteCharacter

@CsvSource used with text blocks is really awesome! Similarly to validation files, it can be used to create easily readable tests with readily visible inputs and outputs placed side by side.

For example:

    @ParameterizedTest
    @CsvSource(delimiterString = "->", textBlock = """
        ABC -> abc
        Abc -> abc
        """)
    void toLowercase(String input, String expected) {
        assertThat(input.toLowerCase())
            .isEqualTo(expected);
    }
Enter fullscreen mode Exit fullscreen mode

What recently confused me was the following error:

org.junit.jupiter.api.extension.ParameterResolutionException:
No ParameterResolver registered for parameter [java.lang.String arg1] in method [void MyTest.test(java.lang.String,java.lang.String)].
Enter fullscreen mode Exit fullscreen mode

It came out that it was caused by a parameter value starting with a single quote ', that wasn't closed by another ' at the end of value. In my case the culprit was 'S-GRAVENHAGE, which is Belgian street name.

The solution is to set parameter quoteCharacter to double quote ", provided we are using text block. This way we can test empty string with "".

Example:

    @ParameterizedTest
    @CsvSource(quoteCharacter = '\"', delimiterString = "->", textBlock = """
        'S-GRAVENHAGE -> 's-gravenhage
        "" -> ""
        """)
    void toLowercase(String input, String expected) {
        assertThat(input.toLowerCase())
            .isEqualTo(expected);
    }
Enter fullscreen mode Exit fullscreen mode

I hope it was helpful, cheers!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay