Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 multiple parametrized tests with same parameters - migrate Parameterized

Previously in JUnit4 you could do something like this:

@RunWith(Parameterized.class)
public class MyTest
{
    private final int number;

    public MyTest(int number) {
        this.play = play;
    }

    @Test
    public void testIsEven() {
        assertEquals(true, number % 2 == 0);
    }

    @Test
    public void testIsNotOdd() {
        assertEquals(false, number % 2 != 0);
    }

    @Parameterized.Parameters
    public static int[] data() {
        return new int[] { 2, 4, 6 } 
    }
}

This would go trough the array, instantiate MyTest with each of the values and then run all of the tests on each of those instances. See the Parameterized docs for more details.

Now in JUnit5 things have changed, according to the new docs you'd have to write the same tests like this:

public class MyTest {
    @ParameterizedTest
    @MethodSource("data")
    public void testIsEven(int number) {
        assertEquals(true, number % 2 == 0);
    }

    @ParameterizedTest
    @MethodSource("data")
    public void testIsNotOdd(int number) {
        assertEquals(false, number % 2 != 0);
    }

    public static int[] data() {
        return new int[] { 2, 4, 6 } 
    }
}

You have to repeat the parameter and the data source for each individual test. Is there a way to do something similar as in JUnit4, where the parameterized tests work on instances of the class instantiated with the different parameters?

like image 516
Todd Sewell Avatar asked Jan 18 '19 20:01

Todd Sewell


People also ask

Does JUnit support parameterized tests where tests can be executed multiple times with different parameter values?

JUnit 5, the next generation of JUnit, facilitates writing developer tests with shiny new features. One such feature is parameterized tests. This feature enables us to execute a single test method multiple times with different parameters.

Which give annotation make it possible to run a test multiple times with different parameters?

Parameterized tests make it possible to run a test multiple times with different arguments. They are declared just like regular @Test methods but use the @ParameterizedTest annotation instead.

Is it possible to use JUnit 4 and JUnit 5 tests in the same test project or suite )?

JUnit 5 provides a way out of the box. Each one is a distinct project and using all of them allows to compile and execute JUnit 4 and JUnit 5 tests in a same project.

Which JUnit runner class should we use to run the parameterized test?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized.


3 Answers

(a summary of the comments)

Reusing the same parameters for all/multiple methods in a test class is currently (version 5.3.2 and 5.4.0-M1) not supported. But this is already a request the JUnit team is working on, see

  • Introduce extension API for container templates (#871)
  • Allow @ParameterizedTest declarations at type level for TCKs (#878)
like image 190
Roland Weisleder Avatar answered Oct 26 '22 18:10

Roland Weisleder


As of today (JUnit 5.3.2 or 5.4.0-M1) it seems not.

I tried to create an Extension to handle such a case, but test class instantiation happens before TestTemplateInvocationContextProvider extensions are taken into account.

So it seems not possible to have multiple instantiation contexts for a same test class.

You surely may ask the core team about this by opening an issue on the JUnit5 github repository.

like image 37
Loïc Le Doyen Avatar answered Oct 26 '22 19:10

Loïc Le Doyen


AFAIR junit5 supports meta annotations. You can define a custom annotation and put it on your tests instead:

@MethodSource("data")
@ParameterizedTest
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OldParameterizedTest {
}

public class MyTest {
    @OldParameterizedTest
    public void testIsEven(int number) {
        assertEquals(true, number % 2 == 0);
    }

    public static int[] data() {
        return new int[] { 2, 4, 6 } 
    }
}
like image 20
SimY4 Avatar answered Oct 26 '22 20:10

SimY4