Pragmatists/JUnitParams

Parameter class must not be a List in 1.0.6

tbroberg opened this issue · 1 comments

When an argument to a test is a List, in 1.0.6, JUnitParams expands the list looking for parameters, and then fails when it finds that the expanded list doesn't match the expected parameter count.

This worked in 1.0.4.

@Test
@Parameters
public void test(ExtendsList transitions) throws Exception {
}

class ExtendsList extends ArrayList<Things> {
	// blah blah blah
}

public List<ExtendsList> parametersForTest() {
	List<ExtendsList> result = new ArrayList<ExtendsList>();
	result.add(new ExtendsList());
	return result;
}

InvokeParameterisedMethod.castParamsUsingConverters() checks the param count and throws an exception. I have no idea where the list gets expanded.

Apologies if this report is short on details as I'm short on time. My primary objective here is to make sure anybody else that steps in this same pot hole has a hint as to how to proceed.

Thanks for pointing this out. That's true - in version 1.0.6 we introduced change that if your Parameter class (here WithPrefixParamsList) is type of Iterable and method providing arguments returns List (here prefixedParams) then we expand arguments from Iterable to test method. Example is worth more than thousand words:

@RunWith(JUnitParamsRunner.class)
public class IterableTest {

    private static final String PREFIX = "M";

    @Test
    @Parameters(method = "prefixedParams")
    public void test(String param, String prefixedParam) throws Exception {
        assertThat(PREFIX + "_" + param).isEqualTo(prefixedParam);
    }

    class WithPrefixParamsList extends ArrayList<String> {

        public WithPrefixParamsList(String prefix, String paramToPrefix) {
            add(paramToPrefix);
            add(prefix + "_" + paramToPrefix);
        }
    }

    public List<WithPrefixParamsList> prefixedParams() {
        List<WithPrefixParamsList> params = new ArrayList<WithPrefixParamsList>();
        params.add(new WithPrefixParamsList(PREFIX, "some"));
        params.add(new WithPrefixParamsList(PREFIX, "more"));
        return params;
    }

}

If you want to achieve what @tbroberg is trying to do then you need to tweak a little bit method providing arguments.

@RunWith(JUnitParamsRunner.class)
public class IterableNotExpandedTest {

    @Test
    @Parameters
    public void test(ExtendsList transitions) throws Exception {
    }

    class ExtendsList extends ArrayList<Things> {
    }

    public Object[] parametersForTest() {
        Object[] result = new Object[]{new ExtendsList()};
        return result;
    }

    private class Things {
    }
}