2

In Eclipse, while using the Parameterized runner in a junit test class, each run is noted by a number (0, 1, etc.)

Is there a way to replace this number with a proper label?

PS: I am using a JUNIT version 4.8 older than 4.11 so the @Parameters does not take any argument

Test Case:

@RunWith(value = Parameterized.class)
public class TestClass {

    @Parameters
    public static Collection<Object[]> getLabels() {
        List<Object[]> labels = new ArrayList<Object[]>();
        labels.add(new Object[] {"Toto"});
        labels.add(new Object[] {"Titi"});
        return labels;
    }

    private final String label;

    public TestClass(String label) {
        this.label = label;
    }

    @Test
    public void test1() {
        assertTrue(true);
    }
}

Result:

enter image description here

Adel Boutros
  • 10,205
  • 7
  • 55
  • 89
  • possible duplicate of [Changing names of parameterized tests](http://stackoverflow.com/questions/650894/changing-names-of-parameterized-tests) – Martin Schröder Jan 23 '14 at 13:27
  • @MartinSchröder Nope. I don't want to change the Test Class name or the test method. I just want to replace the numbered indices with a String. I have already checked the one you pointed at and it's not a duplicate! – Adel Boutros Jan 23 '14 at 14:41
  • @MartinSchröder Also, the solution is only available since junti 4.11 while I am using an older version of junit 4 – Adel Boutros Jan 23 '14 at 14:43

2 Answers2

3

There is an easy way to easily identify the individual test cases in a Parameterized test, you may provide a name using the @Parameters annotation.
This name is allowed to contain placeholders that are replaced at runtime:

{index}: the current parameter index
{0}, {1}, …: the first, second, and so on, parameter value

See example here: https://github.com/junit-team/junit/wiki/Parameterized-tests

Ittiel
  • 1,104
  • 9
  • 12
0

Parameterized test is calling toString() internally, which does not work for us because some our implementations do not allow toString() and throw an exception.

In this case, the test names will be

TestClass
    testMethod
        [1] Argument1.toString()
        [2] Argument2.toString()
        [3]

I created an object wrapper for my argument that holds the original object, and overrides toString method.

Here is the example.

private static List<LabelArgument> getLabels() {
    List<LabelArgument> labels = new ArrayList<>();
    labels.add(LabelArgument.of(new SimpleLabel("Hi there")));
    labels.add(LabelArgument.of(new LabelExtended2D("Good bye!")));

    // Label toString throws an exception
    labels.add(LabelArgument.of(new Label("Simple"))); 

    return labels;
}

@ParameterizedTest
@MethodSource("getLabels")
void testLabel(LabelArgument labelArgument ) {
    var label = labelArgument.getLabel();
    // Do the test
}

private static class LabelArgument {

    private Label label;

    private LabelArgument(Label label) {
        this.label = label;
    }

    public static LabelArgument of(Label label) {
        return new LabelArgument(label);
    }

    @Override
    public String toString() {
        return label.getClass().getSimpleName();
    }
}

It will produce

TestClass
    testAllLabels
        [1] SimpleLabel
        [2] LabelExtended2D
        [3] Label
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114