Test instance lifecycle
In order to provide execution in isolation, the JUnit 5 framework creates a new test instance before executing the actual test (that is, the method annotated with @Test). This per-method test instance life cycle is the behavior in the Jupiter test and also in its antecessors (JUnit 3 and 4). As a novelty, this default behavior can be changed in JUnit 5, simply by annotating a test class with @TestInstance(Lifecycle.PER_CLASS). Using this mode, the test instance will be created once per class, instead of once per test method.
This per-class behavior implies that it is possible to declare the @BeforeAll and @AfterAll methods as non-static. This is beneficial to be used in conjunction with some advanced capabilities, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access