JUnitParams
JUnitParams copied to clipboard
Unable to pass newlines as a parameter (dupe of #30)
According to issue #30 the workaround for passing in newlines is to use a class provider. I'm using class providers nearly exclusively and my trailing newline is still trimmed.
@SuppressWarnings("unused")
public static Object[] provideInvalidValues() {
List<String> invalidValues = new ArrayList<>();
invalidValues.addAll(BlankStringProvider.BLANK_STRINGS);
invalidValues.add("1a"); // starts with a number
invalidValues.add("a-"); // contains a hyphen
invalidValues.add("a\n"); // contains a new line
invalidValues.add("a\r"); // contains a carriage return
invalidValues.add("a\nb"); // contains a new line
invalidValues.add("a\rb"); // contains a carriage return
invalidValues.add("a 1"); // contains a space
invalidValues.add("a∫ı"); // contains funky characters
invalidValues.add("aü"); // also contains funky characters
return invalidValues.toArray();
}
In this example a\n and a\r are provided in the test as just a. I don't really care about how the "test readability" looks, I just want to test the data I want to test.
I did some debugging, I've traced the issue down to the InvokedParameterisedMethod class, in the constructor if the value of params is a string, then it is passed through the Utils.splitAtCommaOrPipe method, which is trimming the string
To workaround this I am using a simple String wrapper:
public final class Text {
private final String text;
private Text(final String text) {
this.text = text;
}
public static Text of(final String text) {
return new Text(text);
}
@Override
public String toString() {
return text;
}
}
Then you can use it in your provider method:
public static Object[] provideInvalidValues() {
List<String> invalidValues = new ArrayList<>();
invalidValues.add(Text.of(" "));
return invalidValues.toArray();
}
@coopstah13 you should use 2-dimensional array as result of provideInvalidValues. Something like:
@SuppressWarnings("unused")
public static Object[][] provideInvalidValues() {
return new Object[]{new String[]{"a\n", "a\r"}};
}
should be enough for your case.
I'm using the wrapper class workaround. I get the 2d array works, my largest issue is that it's easy to do it wrong and you won't even know.