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 below 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 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

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

Keywords

JUnit, parametrized tests, unit testing, test framework

 [/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]