8

I'm trying to use JunitParams in order to parametrized my tests. But my main problem is that the parameters are strings with special characters, tilde or pipe.

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

@RunWith(JUnitParamsRunner.class)
public class TheClassTest {

    @Rule
    public ExpectedException exception = ExpectedException.none();

     @Test
     @Parameters({"AA~BBB"})
     public void shouldTestThemethod(String value) throws Exception {
        exception.expect(TheException.class);

        TheClass.theMethod(value);     
        // Throw an exception if value like "A|B" or "A~B",
        // ie if value contain a ~ or a |
    }
}

With tilde, I have no problem. But with pipe, I have an exception:

java.lang.IllegalArgumentException: wrong number of arguments

The pipe, as the comma, is used as a separator for parameters.

Is there any way for me to set a different separator? Or is this a limitation of JunitParams?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
GaspardP
  • 880
  • 1
  • 12
  • 24
  • You could work around using [this syntax](https://github.com/Pragmatists/JUnitParams/blob/master/src/test/java/junitparams/usage/Samples_of_Usage_Test.java#L34) –  Sep 25 '15 at 14:59
  • I would also try escaping the pipe with "\\", see [this sample](https://github.com/Pragmatists/JUnitParams/blob/master/src/test/java/junitparams/usage/Samples_of_Usage_Test.java#L114) –  Sep 25 '15 at 15:00
  • I tried this syntax, still does not work. now I'm using Parameterized instead of JunitParams. it's working, but the argument are for all the JunitClass (so i had to create a new class). The "\\" works. But because my parameters are hard to read (full of | and ~ and other special characters), I'd prefered not to add a lot of escaping character in my input sets. But yes, it's working. – GaspardP Sep 25 '15 at 15:10

3 Answers3

6

You can indeed escape the pipe with double backslashes:

@Parameters("AA\\|BBB")
public void test(String value)
Per Huss
  • 4,755
  • 12
  • 29
5

I did not find an option to configure the separator chars, but these chars do not need to be escaped if you provide your data in an Object[][].

Consider the following example:

public static Object[][] provideParameters() {
   return new Object[][] {
        new Object[] {"A,B|C"}
    };
}

Both , and | can be directly used, which significantly improves the readability of your tests.

Annotate your test with @Parameters(method = "provideParameters") to make use of this test factory. You can even move the factory to another class (e.g., @Parameters(source = Other.class, method = "provideParameters")).

Sebastian P.
  • 818
  • 8
  • 20
1

you can check zohhak. it's similar to junit params but gives you much more flexibility around parsing parameters. looks like it may help significantly in dealing with 'hard to read' parameters. examples from the docs:

@TestWith(value="7 | 19, 23", separator="[\\|,]")
public void mixedSeparators(int i, int j, int k) {
    assertThat(i).isEqualTo(7);
    assertThat(j).isEqualTo(19);
    assertThat(k).isEqualTo(23);
}

@TestWith(value=" 7 = 7 > 5 => true", separator="=>")
public void multiCharSeparator(String string, boolean bool) {
    assertThat(string).isEqualTo("7 = 7 > 5");
    assertThat(bool).isTrue();
}
piotrek
  • 13,982
  • 13
  • 79
  • 165