Junit 5 Test Instance Lifecycle

A method needs to be tested with multiple different sets of data, we can have different JUnit 5 test cases for the same method. all JUnit 5 test case of the same method is independent of other. To make sure isolation of the test case and to avoid unexpected side effects, at the end of the execution of the test case method, Junit 5 drops the test instance class and creates a new instance, and reinitializes all instance variables of the test class.
if required we can change this method. JUnit 5 provides us 2 ways to control the test instance life cycle.

  1. @TestInstance(Lifecycle.PER_CLASS)
  2. @TestInstance(Lifecycle.PER_METHOD)

@TestInstance(Lifecycle.PER_CLASS) is the default behaviour of junit 5 test class.

follow us on