JUnit Parametrized Tests shows that Junit allows us to create parametrized tests. Parametrized test class has to be annotated with the @RunWith(Parameterized.class). The parametrized test class also meets following requirements:

  • It is annotated with @RunWith(Parameterized.class).
  • It has a single constructor that contains the test data.
  • It has a static method which annotated with @parameters annotation and generates and returns test data. Each test data is used as a parameter for the test method.
  • It needs a test method that annotated with @test annotation.

Class Under Test: Addition.java


ParametrizedTestWithConstructor.java

It is also possible to inject data values directly into fields without needing a constructor using the @Parameter annotation.

ParametrizedTestWithParameterAnnotation.java

Output:

Summary of JUnit Parametrized Tests

  • You learned how to write parameterized tests with a constructor and without a constructor.
  • You practiced with parametrized test examples.

Keywords

Unit testing, test framework, JUnit Parametrized Tests