Duskry
Duskry

Reputation: 33

Junit5 csv exception in a very simple example?

A very simple code,I can't understand this error.pleace help me !

Does '#' need "\" ? I tried but it doesn't work!

Or if you have any more helpful test this 'charTest' method or code please tell me!

public class Stringtest {
String charTest(char first,char second){
    System.out.println("first = " + first);
    System.out.println("second = " + second);
    String res="";
    if(first=='#'||first=='*'){
        if(isNumbers(second)){
            res+="FILE_CHANGE";
            return res;
        }
    }
    res+="N";
    if(!isNumbers(second)){
        res+="M";
    }
    return res;
}
boolean isNumbers(char chars){
    return Character.getNumericValue(chars)<=57 && Character.getNumericValue(chars)>=48;
}

@CsvSource(value = {
        "*1",
        "#0",   // #0  throws org.junit.platform.commons.PreconditionViolationException: Line at index 1 contains invalid CSV: "#0"
        "*w",
        "#?",  // why # can't be the firstchar?
        "A1",
        "cc"
})
@ParameterizedTest(name = "nums {index}==>firstchar:{0},sencondchar:{1}")
void test1(ArgumentsAccessor accessor){
    System.out.println(charTest(accessor.getString(0).charAt(0),accessor.getString(0).charAt(1)));
}

}

This is the Exception stack for top 5

org.junit.platform.commons.PreconditionViolationException: Line at index 1 contains invalid CSV: "#0"

at org.junit.platform.commons.util.Preconditions.condition(Preconditions.java:296)
at org.junit.platform.commons.util.Preconditions.notNull(Preconditions.java:68)
at org.junit.jupiter.params.provider.CsvArgumentsProvider.parseLine(CsvArgumentsProvider.java:73)
at org.junit.jupiter.params.provider.CsvArgumentsProvider.lambda$provideArguments$0(CsvArgumentsProvider.jav

Upvotes: 3

Views: 1063

Answers (1)

andrewJames
andrewJames

Reputation: 21993

If you take a look at the documentation for CsvSource and the value() parameter here, you will see that the # is interpreted as a comment:

Any line beginning with a # symbol will be interpreted as a comment and will be ignored.

That explains why you do not get the expected result - although throwing an error is not the same as "will be ignored" - so perhaps there is a bug, also.

If you can, I would suggest avoiding the # entirely. Depending on the constraints you are dealing with, that may not be an option, of course.

Otherwise, you may have better success with something like the following approach:

import org.junit.jupiter.params.provider.ValueSource;

...

@ParameterizedTest
@ValueSource(strings = {"11", "#2", "33"})
public void test2(String input) {
    System.out.println(charTest(input.charAt(0), input.charAt(1)));
}

This will handle leading #s as expected:

first = #
second = 2

Upvotes: 1

Related Questions