Junit5: Make emptyValue configurable in @CsvSource and @CsvFileSource

Created on 19 Feb 2019  路  7Comments  路  Source: junit-team/junit5

I did not find a way to set the emptyValue of CsvParserSettings in case of having "null" and not "" for empty values.

For example, the values could be
1;null;20.7;6.75;9; 6.25
And it is not neccessary to have
1;;20.7;6.75;9; 6.25

I could provide a pull request with the proposal that at the end could be so configured:

@CsvFileSource(numLinesToSkip = 1, delimiter = ';', emptyValue='null', resources =[....]

By default is
@CsvFileSource(numLinesToSkip = 1, delimiter = ';', emptyValue='', resources =[....]
@CsvFileSource(numLinesToSkip = 1, delimiter = ';', resources =[....]

Jupiter parameterized tests enhancement

Most helpful comment

@marcphilipp yes, I am. I will start next week.

All 7 comments

Tentatively slated for 5.5 M1 for _team discussion_

Is it a good idea if I provide a pull request for the discussion? Does it help?

No, I would await the team decision before proceeding with any form of coding.

Team Decision: Let's add an emptyValue attribute to both @CsvSource and @CsvFileSource.

@Ciruman Are you interested in submitting a PR?

@marcphilipp yes, I am. I will start next week.

During the development, I found out that emptyValue in settings another meaning had to the issue I wanted to address.

In CsvParserSettings emptyValue means:

When reading, if the parser does not read any character from the input, and the input is within quotes, the empty is used instead of an empty string

    @Test
    void emptyValueIsAnEmptyWithCustomEmptyValueString() {
        Stream<Object[]> arguments = provideArguments(',', "vacio","null , , empty , ''");

        assertThat(arguments).containsExactly(new String[] { "null", null, "empty", "vacio" });
    }

    private Stream<Object[]> provideArguments(char delimiter, String emptyValue, String... value) {
        CsvSource annotation = mock(CsvSource.class);
        when(annotation.value()).thenReturn(value);
        when(annotation.delimiter()).thenReturn(delimiter);
        when(annotation.emptyValue()).thenReturn(emptyValue);

        CsvArgumentsProvider provider = new CsvArgumentsProvider();
        provider.accept(annotation);
        return provider.provideArguments(null).map(Arguments::get);
    }

Might be that you understood it like this? If yes, I implemented in the following pull request the previous feature description: #1839

Otherwise, I think emptyValue is not the best name for it.

What I meant in this issue was a value definition:
"When reading, if the parser reads a value with this string, null is returned"

I think this parameter name could be nullValue.

Currently:
1;;20.7;6.75;9; 6.25 -> Second Value(not present) is null

In this issue, I wanted to address the possibility to set an alternative to "not present/null" values

1;null;20.7;6.75;9; 6.25 here, if nullValue is set to "null", second value after parsing would be null
1;vacio;20.7;6.75;9; 6.25 here, if nullValue is set to "vacio", second value after parsing would be null
1;nill;20.7;6.75;9; 6.25 here, if nullValue is set to "nill", second value after parsing would be null

Then a test would look like this:

    @Test
    void emptyValueIsAnEmptyWithCustomNullValueString() {
        Stream<Object[]> arguments = provideArguments(',', "","empty", "vacio , , empty , ''");

        assertThat(arguments).containsExactly(new String[] { "vacio", null, null, "" });
    }
    private Stream<Object[]> provideArguments(char delimiter, String emptyValue, String nullValue, String... value) {
        CsvSource annotation = mock(CsvSource.class);
        when(annotation.value()).thenReturn(value);
        when(annotation.delimiter()).thenReturn(delimiter);
        when(annotation.emptyValue()).thenReturn(emptyValue);
        when(annotation.nullValue()).thenReturn(nullValue);

        CsvArgumentsProvider provider = new CsvArgumentsProvider();
        provider.accept(annotation);
        return provider.provideArguments(null).map(Arguments::get);
    }

Should I create a new Issue for nullValue?

Was this page helpful?
0 / 5 - 0 ratings