Test Instance Lifecycle

Let's understand how to use the test instance Lifecycle in JUnit 5.

We'll cover the following

Per-class test instance lifecycle

By default, JUnit creates a new instance of each test class before executing each test method. That’s why the @BeforeAll and @AfterAll annotations can only be added to static methods by default. This per-method test instance Lifecycle is appropriate for most cases because test methods in a test class should be independent and verify different aspects of the same target object.

Get hands-on with 1200+ tech skills courses.