JUnit 4.11 at last allows parametrized tests WITH names!

With JUnit 4.11 parametrized tests run via @Parameters have names.

public class AdderTest {

  private int first;
  private int second;
  private int result;
  private String message;
  private static final String OK = "Simple addition, should not be faulty.";

  @Parameters(name="Is {0}+{1} really {2}? Set # {index}")
  public static Collection data() {
    Object[][] data = new Object[][] { {1,1,2, OK}, {1,2,3, OK}, {1,3,4, OK}, {3,3,6, OK}, {4,5,9, OK} };
    return Arrays.asList(data);

  public AdderTest(int f, int s, int r, String msg) {
    this.first = f;
    this.second = s;
    this.result = r;
    this.message = msg;

  public void shouldAddAsExpected() {
  Adder a = new Adder();
  int expected = result;

  // when
  int actual = a.add(first, second);

  // then
  assertEquals(message, expected, actual);

And here’s how it looks in STS:

STS screenshot
Note names of the tests consistency with pattern in @Parameters annotation name

At last! This should have happen in 3.8!



I just realized, that I’m quite behind here. 😀  This was released in November 2011! 😀
I’ve even read about 4.11 before! On Tomek’s blog, yet I managed to forget about it. Well, perhaps because of the fact that the only features that sparkle there for me is naming parametrized tests and having skipped tests due to failed assumption finally reported as skipped.

For full release notes see JUnit Wiki page for 4.11 (note it’s moved from Kent’s GitHub!), or take a look at Misha’s post on hasCode.com – he dissected it fully, not like me, who just got into parametrized tests. 🙂

PS. Anyone who can suggest a better way to display Java code than default code tags is welcome. :-/


1 Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s