O'Reilly logo

Mastering Software Testing with JUnit 5 by Boni García

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required