Skip to content

References

Test lifecycle methods

JUnit allows to write tests by adding the reference @Test to a method. After adding several tests to one class, it is possible, that our test code will contain some repetitions, just like in this example.

In order to avoid such repetitions, JUnit allows defining the definition of so-called test lifecycle methods. These methods must have a certain reference over its signature:

  • @BeforeEach
  • @AfterEach
  • @BeforeAll
  • @AfterAll.

You have to remember, that all lifecycle cycle methods:

  • do not contain any object
  • can have any name
  • do not have arguments
  • do not have to have a defined access modificator
  • are optional and we can define them frequently (ie. one test class can not have a method with reference @AfterEach and have several methods with reference @BeforeEach)
  • can be plaecd in any part of the test class, but in practice, they are usually placed before the definition of the test.

@BeforeEach

A method with reference @BeforeEach will be executed before each test method. The purpose of this method is preparing the objects for each test. The use of this method can be found in this example.

@AfterEach

A method with reference @AfterEach will be executed after each test method. It is used mainly for removing data or restoring the initial state after each test.

@BeforeAll

A method of test lifecycle, described with the reference @BeforeAll has to be a static method. It is executed once, at the start of the test execution in the given class. It is used mainly for setting the values of objects, which will be used in tests, but will not be modified in them or are costly (e.g. creation time in time context), e.g. etablishing a connection with the database.

@AfterAll

Similar to @BeforeAll, a method with the reference @AfterAll has to be static. Will be executed once, after the test execution in the given class. It is used i.a. for deleting objects, which were common for all tests, like e.g. closing the connection with the database.

The use of all available assertions of test lifecycle cn be found in examples.

NOTE: If you are using JUnit 4, the test lifecycle references have slightly different names. They are: * @Before * @After * @AfterClass * @BeforeClass.

Additional references

JUnit library contains also references, that improve the developers' work. These are:

  • @DisplayName
  • @Disabled

@DisplayName

We can add our own name to a test class and a test method. This name will be displayed while test execution and in test reports.

@Disabled

It describes a method or class, that should not be executed while test execution. It has an argument, in which we can add an information, why this test is disabled. There can be various reason of using this reference. Such test could be created before implementing of the code or the current code contains a bug, which needs to be fixed before enabling the test

The use of additional references can be found in the next example.