A couple of years ago I wrote about JUnit Parameterized Tests. One of the things I didn't like about them was that JUnit named the invidividual test cases using numbers, so if they failed you had no idea which test parameters caused the failure. The following Eclipse screenshot will show you what I mean:
However, in JUnit 4.11, the @Parameters
annotation now takes a name
argument which can be used to display the parameters in the test name and hence, make them more descriptive. You can use the following placeholders in this argument and they will be replaced by actual values at runtime by JUnit:
{index}
: the current parameter index
{0}
, {1}
, ...: the first, second, and so on, parameter value
Here is an example:
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class StringSortTest {
@Parameters(name = "{index}: sort[{0}]={1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ "abc", "abc"},
{ "cba", "abc"},
{ "abcddcba", "aabbccdd"},
{ "a", "a"},
{ "aaa", "aaa"},
{ "", ""}
});
}
private final String input;
private final String expected;
public StringSortTest(final String input, final String expected){
this.input = input;
this.expected = expected;
}
@Test
public void testSort(){
assertEquals(expected, sort(input));
}
private static String sort(final String s) {
final char[] charArray = s.toCharArray();
Arrays.sort(charArray);
return new String(charArray);
}
}
When you run the test, you will see individual test cases named as shown in the Eclipse screenshot below, so it is easy to identify the parameters used in each test case.
Note that due to a bug in Eclipse, names containing brackets are truncated. That's why I had to use sort[{0}]
, instead of sort({0})
.