Using Comma or pipe in JunitParams

I am trying to use JunitParams to parameterize 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 | } } 

I have no problems with tilde. But with the pipe, I have an exception:

 java.lang.IllegalArgumentException: wrong number of arguments 

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

Is there a way to set another delimiter? Or is this a limitation of JunitParams?

+5
source share
3 answers

I did not find an option for setting separator characters, but these characters should not be escaped if you provide your data in Object[][] .

Consider the following example:

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

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

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

+2
source

You can really escape the double backslash pipe:

 @Parameters("AA\\|BBB") public void test(String value) 
0
source

you can check zohhak . it is similar to junit parameters, but gives you much more flexibility when analyzing parameters. it looks like it can help a lot with working with hard to read options. 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(); } 
-1
source

Source: https://habr.com/ru/post/1232282/


All Articles