Pragmatists/JUnitParams

String values are trimmed for method and literal params

jolros opened this issue ยท 5 comments

Attempted to reopen #55 but didn't see activity, so opening a new bug for visibility.

Whitespace is trimmed in parameters, and the suggested fix of using methods doesn't seem to work:

import static com.google.common.truth.Truth.assertThat;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JUnitParamsRunner.class)
public class ParamsTest {

  public static String[] validStrings() {
    return new String[] {
      "    ", " f  ", "four",
    };
  }

  @Test
  @Parameters(method = "validStrings")
  public void testString(String value) throws Exception {
    assertThat(value.length()).isGreaterThan(3);
  }
}
[0] (testString)(com.google.corp.travel.shared.email.ParamsTest)
java.lang.AssertionError: Not true that <0> is greater than <3>

[1] f (testString)(com.google.corp.travel.shared.email.ParamsTest)
java.lang.AssertionError: Not true that <1> is greater than <3>

Hello,
We have already explained why we will not introduce this change #55 (@jrmichael comment).
Moreover, how many times you need this functionallity in your project? I guess it's usually some super corner case and it costs almost nothing to provide parameters by a method.

dimas commented

@woprzech, honestly, I do not understand why you refusing to accept that it is really a bug.
@jrmichael's comment does not really explain anything except stating that you won't fix it. Also, it suggest using method element and this bug says that method does not work too.
But the bug is absolutely real - the strings that are passed to the test method are NOT what developer put there. Here is my test case for example

    private static Object[] invalidMacs() {
        return new Object[] {
                // Too short
                "1122334455",
                // Too long
                "00112233445566",
                // Has spaces
                " 001122334455",
                "001122334455 ",
                "001122 334455",
                // Invalid characters
                "00x122334455",
        };
    }

I can hardly call it a super-corner case.
IMHO, it is a no-no for the test framework to modify parameters in any way that developer did not ask nor can control.

I've been affected by this issue as well and the only solution I've found is to box the strings into custom classes which are then passed to the test method. This feels clean if you are passing multiple test parameters as part of the same object, and quite icky otherwise.

I don't see how whitespace trimming could be considered a "super corner case" for a unit testing library. Isn't it one of the most common things to test for methods that take Strings?

boolean isValidAccountName(String name)

Using " admin " with this library would silently coalesce the input to "admin".

dlsxd commented

Hi guys,
There is a happy path on this issue, parameters should be passed as two-dimensional array: one row for each test invocation. This will preserve spaces.

       public static Object validStrings() {
	    return new Object[] {
	    	new Object[] {"    "},
	    	new Object[] {" f  "},
	    	new Object[] {"four"}
	    };
	}
	
	@Test
	@Parameters(method = "validStrings")
	public void testString(String value) throws Exception {
		assertThat(value.length()).isGreaterThan(3);
	}

or this one is also valid

public static Object validStrings() {	    
     return Arrays.asList("    "," f  ","four");	    
}