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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️