By Epaga

2009-03-16 15:25:21 8 Comments

Is there a way to set my own custom test case names when using parameterized tests in JUnit4?

I'd like to change the default — [Test class].runTest[n] — to something meaningful.


@Sina Madani 2017-06-06 17:00:04

Since the parameter accessed (e.g. with "{0}" always returns the toString() representation, one workaround would be to make an anonymous implementation and override toString() in each case. For example:

public static Iterable<? extends Object> data() {
    return Arrays.asList(
        new MyObject(myParams...) {public String toString(){return "my custom test name";}},
        new MyObject(myParams...) {public String toString(){return "my other custom test name";}},

@rescdsk 2012-04-13 15:34:58

This feature has made it into JUnit 4.11.

To use change the name of parameterized tests, you say:


namestring is a string, which can have the following special placeholders:

  • {index} - the index of this set of arguments. The default namestring is {index}.
  • {0} - the first parameter value from this invocation of the test.
  • {1} - the second parameter value
  • and so on

The final name of the test will be the name of the test method, followed by the namestring in brackets, as shown below.

For example (adapted from the unit test for the Parameterized annotation):

static public class FibonacciTest {

    @Parameters( name = "{index}: fib({0})={1}" )
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });

    private final int fInput;
    private final int fExpected;

    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;

    public void testFib() {
        assertEquals(fExpected, fib(fInput));

    private int fib(int x) {
        // TODO: actually calculate Fibonacci numbers
        return 0;

will give names like testFib[1: fib(1)=1] and testFib[4: fib(4)=3]. (The testFib part of the name is the method name of the @Test).

@Matthew Farwell 2012-06-27 09:32:04

There is no reason it would not be in 4.11, it's in master. Now when 4.11 will be available, that is a good question :-)

@rescdsk 2012-10-17 18:59:42

4.11 is now in beta, and can be downloaded from the same link as above :-)

@djangofan 2012-11-17 03:17:51

Yes, but there is a bug. If you put a parenthesis in the parameter "name" value like you are doing in this posting, it breaks the display of the unit test name in Eclipse.

@rescdsk 2012-12-21 19:46:42

@djangofan, are you saying that's an Eclipse bug? I'm not working with Eclipse on a daily basis anymore, so I can't confirm. Or is it a jUnit bug?

@djangofan 2012-12-21 21:39:38

@rescdsk - im not sure. the parameter name that is displayed in the JUNit tab in eclipse gets munged (when parenthesis exists in the string).

@dogbane 2013-03-26 11:12:28

great, but what if {0} and {1} are arrays? JUnit should ideally call Arrays.toString({0}), not {0}.toString(). For example, my data() method returns Arrays.asList(new Object[][] {{ new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 } }});.

@Pool 2013-08-29 08:06:46

@djangofan This is an 8 year old Eclipse bug:

@djangofan 2013-08-29 22:00:03

@Pool - that is not the same bug. the bug I speak of is for JUnit version 4.11 only.

@Pool 2013-08-30 08:27:11

@djangofan - I believe it will only show up with JUnit version 4.11 due to the possibility to add brackets. You wrote the parameter name that is displayed in the JUNit tab in eclipse gets munged (when parenthesis exists in the string). The bug report details that Eclipse removes detail in brackets and that it is done deliberately. Isn't this what you meant by munged?

@djangofan 2013-08-30 18:59:07

@Pool - yes, its a related bug but I doubt its the same.

@Pool 2013-08-31 16:52:16

@djangofan - OK, so what happens for the name for you? The characters are corrupted and display garbled? That sounds a more serious issue and it would be worth reporting the bug if it is still occurring.

@Valentine Zakharenko 2016-10-05 21:59:46

I use JUnit 4.9 and Parameters has no name().

@Bowi 2017-02-02 13:36:38

There is no way I can do anything with it, right? Like name = makeNameForObj(xy)?

@aelimill 2017-02-04 15:46:33

@Bowi you can make class NewClass extends YourTestObject and override its toString method to anything you like

@mmirwaldt 2013-06-05 17:09:57

A workaround would be to catch and nest all Throwables into a new Throwable with a custom message that contains all information about the parameters. The message would appear in the stack trace. This works whenever a test fails for all assertions, errors and exceptions as they are all subclasses of Throwable.

My code looks like this:

public class ParameterizedTest {

    int parameter;

    public ParameterizedTest(int parameter) {
        this.parameter = parameter;

    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] { {1}, {2} });

    public void test() throws Throwable {
        try {
        catch(Throwable thrown) {
            throw new Throwable("parameter="+parameter, thrown);


The stack trace of the failed test is:

java.lang.Throwable: parameter=1
    at sample.ParameterizedTest.test(
Caused by: java.lang.AssertionError
    at org.junit.Assert.assertTrue(
    at org.junit.Assert.assertTrue(
    at sample.ParameterizedTest.test(
    ... 31 more

@quarkonium 2012-03-27 16:19:44

Check out JUnitParams as dsaff mentioned, works using ant to build parameterized test method descriptions in the html report.

This was after trying LabelledParameterized and finding that it although it works with eclipse it does not work with ant as far as the html report is concerned.


@Yishai 2009-03-16 15:49:14

Looking at JUnit 4.5, its runner clearly doesn't support that, as that logic is buried inside a private class inside the Parameterized class. You could not use the JUnit Parameterized runner, and create your own instead which would understand the concept of names (which leads to the question of how you might set a name ...).

From a JUnit perspective, it would be nice if instead of (or in addition to) just passing an increment, they would pass the comma delimited arguments. TestNG does this. If the feature is important to you, you can comment on the yahoo mailing list referenced at

@guerda 2009-03-16 16:09:32

I would highly appreciate if there's a improvement for this in JUnit!

@reccles 2010-02-05 14:42:19

Instead of passing an argument perhaps just using the toString() on the test class. I imagine adding this feature to JUnit would be pretty straightforward.

@reccles 2010-02-05 14:47:21

Just checked, there is an outstanding feature request for this at: Please vote it up.

@rescdsk 2012-04-13 15:32:54

Looks like it made it in, and will be in the next release of JUnit :-)…

@Franz Ebner 2012-05-24 09:39:20

Whats the current answer regarding this issue?

@Yishai 2012-05-24 15:20:36

@Frank, I think that the release that addresses this issue is not yet released. It will be in JUnit 4.11. At that time (assuming the design remains the same) it will be about a textual way of specifying how you name the test, including taking parameters as names. Pretty nice, actually.

@rescdsk 2012-11-16 21:38:32

JUnit 4.11 has now been released :-)

@kldavis4 2013-03-26 13:53:39

Here is the updated link to the original issue for future reference

@dsaff 2011-06-08 14:30:53

You may also want to try JUnitParams:

@rockydgeekgod 2017-03-30 02:37:51

JUnit has moved to github. Here is the updated link:

@Christian 2011-05-24 17:11:09

None of it was working for me, so I got the source for Parameterized and modified it create a a new test runner. I didn't have to change much but IT WORKS!!!

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Assert;
import org.junit.internal.runners.ClassRoadie;
import org.junit.internal.runners.CompositeRunner;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.internal.runners.MethodValidator;
import org.junit.internal.runners.TestClass;
import org.junit.runner.notification.RunNotifier;

public class LabelledParameterized extends CompositeRunner {
static class TestClassRunnerForParameters extends JUnit4ClassRunner {
    private final Object[] fParameters;

    private final String fParameterFirstValue;

    private final Constructor<?> fConstructor;

    TestClassRunnerForParameters(TestClass testClass, Object[] parameters, int i) throws InitializationError {
        super(testClass.getJavaClass()); // todo
        fParameters = parameters;
        if (parameters != null) {
            fParameterFirstValue = Arrays.asList(parameters).toString();
        } else {
            fParameterFirstValue = String.valueOf(i);
        fConstructor = getOnlyConstructor();

    protected Object createTest() throws Exception {
        return fConstructor.newInstance(fParameters);

    protected String getName() {
        return String.format("%s", fParameterFirstValue);

    protected String testName(final Method method) {
        return String.format("%s%s", method.getName(), fParameterFirstValue);

    private Constructor<?> getOnlyConstructor() {
        Constructor<?>[] constructors = getTestClass().getJavaClass().getConstructors();
        Assert.assertEquals(1, constructors.length);
        return constructors[0];

    protected void validate() throws InitializationError {
        // do nothing: validated before.

    public void run(RunNotifier notifier) {

public static @interface Parameters {

private final TestClass fTestClass;

public LabelledParameterized(Class<?> klass) throws Exception {
    fTestClass = new TestClass(klass);

    MethodValidator methodValidator = new MethodValidator(fTestClass);

    int i = 0;
    for (final Object each : getParametersList()) {
        if (each instanceof Object[])
            add(new TestClassRunnerForParameters(fTestClass, (Object[]) each, i++));
            throw new Exception(String.format("%s.%s() must return a Collection of arrays.", fTestClass.getName(), getParametersMethod().getName()));

public void run(final RunNotifier notifier) {
    new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() {
        public void run() {

private Collection<?> getParametersList() throws IllegalAccessException, InvocationTargetException, Exception {
    return (Collection<?>) getParametersMethod().invoke(null);

private Method getParametersMethod() throws Exception {
    List<Method> methods = fTestClass.getAnnotatedMethods(Parameters.class);
    for (Method each : methods) {
        int modifiers = each.getModifiers();
        if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
            return each;

    throw new Exception("No public static parameters method on class " + getName());

public static Collection<Object[]> eachOne(Object... params) {
    List<Object[]> results = new ArrayList<Object[]>();
    for (Object param : params)
        results.add(new Object[] { param });
    return results;

@yliang 2011-01-04 21:34:06

from junit4.8.2, you can create your own MyParameterized class by simply copy Parameterized class. change the getName() and testName() methods in TestClassRunnerForParameters.

@java_enthu 2011-12-29 13:50:42

I tried this but doesn't help. While creating new class getParametersMethod fails.

@David Moles 2010-08-04 09:35:17

With Parameterized as a model, I wrote my own custom test runner / suite -- only took about half an hour. It's slightly different from darrenp's LabelledParameterized in that it lets you specify a name explicitly rather than relying on the first parameter's toString().

It also doesn't use arrays because I hate arrays. :)

public class PolySuite extends Suite {

  // //////////////////////////////
  // Public helper interfaces

   * Annotation for a method which returns a {@link Configuration}
   * to be injected into the test class constructor
  public static @interface Config {

  public static interface Configuration {
    int size();
    Object getTestValue(int index);
    String getTestName(int index);

  // //////////////////////////////
  // Fields

  private final List<Runner> runners;

  // //////////////////////////////
  // Constructor

   * Only called reflectively. Do not use programmatically.
   * @param c the test class
   * @throws Throwable if something bad happens
  public PolySuite(Class<?> c) throws Throwable {
    super(c, Collections.<Runner>emptyList());
    TestClass testClass = getTestClass();
    Class<?> jTestClass = testClass.getJavaClass();
    Configuration configuration = getConfiguration(testClass);
    List<Runner> runners = new ArrayList<Runner>();
    for (int i = 0, size = configuration.size(); i < size; i++) {
      SingleRunner runner = new SingleRunner(jTestClass, configuration.getTestValue(i), configuration.getTestName(i));
    this.runners = runners;

  // //////////////////////////////
  // Overrides

  protected List<Runner> getChildren() {
    return runners;

  // //////////////////////////////
  // Private

  private Configuration getConfiguration(TestClass testClass) throws Throwable {
    return (Configuration) getConfigMethod(testClass).invokeExplosively(null);

  private FrameworkMethod getConfigMethod(TestClass testClass) {
    List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Config.class);
    if (methods.isEmpty()) {
      throw new IllegalStateException("@" + Config.class.getSimpleName() + " method not found");
    if (methods.size() > 1) {
      throw new IllegalStateException("Too many @" + Config.class.getSimpleName() + " methods");
    FrameworkMethod method = methods.get(0);
    int modifiers = method.getMethod().getModifiers();
    if (!(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
      throw new IllegalStateException("@" + Config.class.getSimpleName() + " method \"" + method.getName() + "\" must be public static");
    return method;

  // //////////////////////////////
  // Helper classes

  private static class SingleRunner extends BlockJUnit4ClassRunner {

    private final Object testVal;
    private final String testName;

    SingleRunner(Class<?> testClass, Object testVal, String testName) throws InitializationError {
      this.testVal = testVal;
      this.testName = testName;

    protected Object createTest() throws Exception {
      return getTestClass().getOnlyConstructor().newInstance(testVal);

    protected String getName() {
      return testName;

    protected String testName(FrameworkMethod method) {
      return testName + ": " + method.getName();

    protected void validateConstructor(List<Throwable> errors) {

    protected Statement classBlock(RunNotifier notifier) {
      return childrenInvoker(notifier);

And an example:

public class PolySuiteExample {

  // //////////////////////////////
  // Fixture

  public static Configuration getConfig() {
    return new Configuration() {
      public int size() {
        return 10;

      public Integer getTestValue(int index) {
        return index * 2;

      public String getTestName(int index) {
        return "test" + index;

  // //////////////////////////////
  // Fields

  private final int testVal;

  // //////////////////////////////
  // Constructor

  public PolySuiteExample(int testVal) {
    this.testVal = testVal;

  // //////////////////////////////
  // Test

  public void odd() {
    assertFalse(testVal % 2 == 0);

  public void even() {
    assertTrue(testVal % 2 == 0);


@binkley 2010-01-14 17:19:14

I make extensive use of static import for Assert and friends, so it is easy for me to redefine assertion:

private <T> void assertThat(final T actual, final Matcher<T> expected) {
    Assert.assertThat(editThisToDisplaySomethingForYourDatum, actual, expected);

For example, you could add a "name" field to your test class, initialized in the constructor, and display that on test failure. Just pass it in as the first elements of your parameters array for each test. This also helps label the data:

public ExampleTest(final String testLabel, final int one, final int two) {
    this.testLabel = testLabel;
    // ...

public static Collection<Object[]> data() {
    return asList(new Object[][]{
        {"first test", 3, 4},
        {"second test", 5, 6}

@Yishai 2010-01-14 20:44:01

This is fine if the test fails an assert, but there are other cases, such as if an exception is thrown that fails the test, or if the test is expecting an exception to be thrown, that make thinking of the name overhead that should be handled by the framework.

@darrenp 2010-01-12 20:42:19

I recently came across the same problem when using JUnit 4.3.1. I implemented a new class which extends Parameterized called LabelledParameterized. It has been tested using JUnit 4.3.1, 4.4 and 4.5. It reconstructs the Description instance using the String representation of the first argument of each parameter array from the @Parameters method. You can see the code for this at:

and an example of its use at:

The test description formats nicely in Eclipse which is what I wanted since this makes failed tests a lot easier to find! I will probably further refine and document the classes over the next few days/weeks. Drop the '?' part of the URLs if you want the bleeding edge. :-)

To use it, all you have to do is copy that class (GPL v3), and change @RunWith(Parameterized.class) to @RunWith(LabelledParameterized.class) assuming the first element of your parameter list is a sensible label.

I don't know if any later releases of JUnit address this issue but even if they did, I can't update JUnit since all my co-developers would have to update too and we have higher priorities than re-tooling. Hence the work in the class to be compilable by multiple versions of JUnit.

Note: there is some reflection jiggery-pokery so that it runs across the different JUnit versions as listed above. The version specifically for JUnit 4.3.1 can be found here and, for JUnit 4.4 and 4.5, here.

@darrenp 2010-01-13 09:51:26

:-) One of my co-developers today had a problem with it since the version I point to in the above message uses JUnit 4.3.1 (not 4.4 as I originally said). He is using JUnit 4.5.0 and it caused problems. I'll be addressing these today.

@giraff 2011-08-06 14:13:35

I took some time to understand that you need to pass the test name in the constructor, but not memorize it. Thanks for the code!

@Henrik Aasted Sørensen 2012-10-10 13:18:24

Works great as long as I run the tests from Eclipse. Does anyone have experience with making it work with the JUnit Ant Task, though? The test reports are named execute[0], execute[1] ... execute[n] in the test reports generated.

@gia 2013-07-11 06:49:56

Very nice. Works like a charm. Would be nice, if you could add the info, that it is required to add "String label, ..." as first parameter to the invoked @Test-method.

@ShiDoiSi 2016-10-13 10:30:38 no longer exists/hosts SVN.

@Terra Caines 2009-08-10 20:23:50

You can create a method like

public void name() {
    Assert.assertEquals("", inboundFileName);

While I wouldn't use it all the time it would be useful to figure out exactly which test number 143 is.

Related Questions

Sponsored Content

36 Answered Questions

[SOLVED] Can't execute jar- file: "no main manifest attribute"

51 Answered Questions

33 Answered Questions

[SOLVED] How do you assert that a certain exception is thrown in JUnit 4 tests?

18 Answered Questions

[SOLVED] How to run test methods in specific order in JUnit4?

6 Answered Questions

[SOLVED] Populating Spring @Value during Unit Test

12 Answered Questions

[SOLVED] JUnit test for System.out.println()

  • 2009-07-13 13:18:13
  • Mike Minicki
  • 217391 View
  • 346 Score
  • 12 Answer
  • Tags:   java console junit

4 Answered Questions

[SOLVED] Conditionally ignoring tests in JUnit 4

3 Answered Questions

[SOLVED] Junit Parameterized tests together with Powermock - how?

4 Answered Questions

[SOLVED] Parameterized test suite

  • 2012-05-05 15:45:18
  • EA.
  • 1977 View
  • 1 Score
  • 4 Answer
  • Tags:   java junit junit4

1 Answered Questions

[SOLVED] How to run a generic parameterized JUnit test in Eclipse?

Sponsored Content