What does the @TestInstance(Lifecycle.PER_CLASS) annotation do in JUnit?
It tells JUnit to create only one instance of the test class and reuse it for all test methods, instead of creating a new instance for each test method.
Click to reveal answer
intermediate
Why might you use @TestInstance(Lifecycle.PER_CLASS) instead of the default lifecycle?
To share state or expensive setup between test methods, reducing repeated initialization and improving test performance.
Click to reveal answer
intermediate
How does @BeforeAll behave differently when using @TestInstance(Lifecycle.PER_CLASS)?
With PER_CLASS lifecycle, <code>@BeforeAll</code> methods can be non-static because the same test instance is reused.
Click to reveal answer
beginner
What is the default test instance lifecycle in JUnit 5?
The default lifecycle is <code>PER_METHOD</code>, which creates a new test class instance for each test method.
Click to reveal answer
advanced
What is a potential risk of using @TestInstance(Lifecycle.PER_CLASS)?
Tests might become dependent on each other if shared state is modified, leading to flaky or unreliable tests.
Click to reveal answer
What happens when you annotate a JUnit 5 test class with @TestInstance(Lifecycle.PER_CLASS)?
AJUnit creates a new instance for each test method.
BJUnit creates one instance of the test class for all tests.
CJUnit disables all tests in the class.
DJUnit runs tests in parallel automatically.
✗ Incorrect
PER_CLASS lifecycle means one instance is reused for all test methods.
Which method can be non-static when using @TestInstance(Lifecycle.PER_CLASS)?
A<code>@BeforeAll</code>
B<code>@AfterEach</code>
C<code>@Test</code>
D<code>@BeforeEach</code>
✗ Incorrect
With PER_CLASS, @BeforeAll can be non-static because the same instance is used.
What is the default lifecycle of test instances in JUnit 5?
APER_CLASS
BPER_TEST
CPER_SUITE
DPER_METHOD
✗ Incorrect
JUnit 5 creates a new instance per test method by default (PER_METHOD).
What is a common reason to choose PER_CLASS lifecycle?
ATo isolate tests completely
BTo run tests faster by skipping assertions
CTo share expensive setup across tests
DTo disable test methods selectively
✗ Incorrect
PER_CLASS allows sharing setup or state to improve performance.
What risk does PER_CLASS lifecycle introduce?
ATests may become dependent on shared state
BTests run slower
CTests cannot use <code>@BeforeAll</code>
DJUnit throws errors on multiple test methods
✗ Incorrect
Shared state can cause tests to affect each other, leading to flaky tests.
Explain how the @TestInstance(Lifecycle.PER_CLASS) annotation changes the lifecycle of test instances in JUnit 5.
Think about how many times the test class is created.
You got /4 concepts.
Describe the benefits and risks of using the PER_CLASS lifecycle in JUnit testing.
Consider both positive and negative effects on test design.
You got /4 concepts.
Practice
(1/5)
1. What does the @TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation do in a JUnit test class?
easy
A. Creates a single test instance shared by all test methods in the class.
B. Creates a new test instance before each test method runs.
C. Disables test instance creation for the class.
D. Runs tests in parallel threads automatically.
Solution
Step 1: Understand the annotation purpose
The @TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation tells JUnit to create only one instance of the test class for all test methods.
Step 2: Compare with default behavior
By default, JUnit creates a new instance before each test method, but PER_CLASS changes this to reuse the same instance.
Final Answer:
Creates a single test instance shared by all test methods in the class. -> Option A
Quick Check:
PER_CLASS means one instance per class [OK]
Hint: PER_CLASS means one instance for all tests in class [OK]
Common Mistakes:
Confusing PER_CLASS with default PER_METHOD lifecycle
Thinking PER_CLASS disables instance creation
Assuming PER_CLASS runs tests in parallel
2. Which of the following is the correct syntax to set the test instance lifecycle to PER_CLASS in JUnit 5?
easy
A. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
B. @TestInstance(Lifecycle.PER_CLASS)
C. @TestInstance(PER_CLASS)
D. @TestInstance(Lifecycle.PER_METHOD)
Solution
Step 1: Recall the full annotation syntax
The correct syntax uses the full enum path: @TestInstance(TestInstance.Lifecycle.PER_CLASS).
Step 2: Check other options
@TestInstance(Lifecycle.PER_CLASS) misses the TestInstance. prefix, @TestInstance(PER_CLASS) is incomplete, and @TestInstance(Lifecycle.PER_METHOD) uses PER_METHOD which is default, not PER_CLASS.
Final Answer:
@TestInstance(TestInstance.Lifecycle.PER_CLASS) -> Option A
Quick Check:
Full enum path required in annotation [OK]
Hint: Use full enum path: TestInstance.Lifecycle.PER_CLASS [OK]
Common Mistakes:
Omitting TestInstance prefix in annotation
Using PER_METHOD instead of PER_CLASS
Using incorrect enum names or syntax
3. Consider the following JUnit 5 test class with @TestInstance(TestInstance.Lifecycle.PER_CLASS):
If the list.clear() call is removed from setup(), what problem will occur?
medium
A. Tests will pass normally without issues.
B. Tests will fail because list keeps growing between tests.
C. Tests will throw NullPointerException.
D. JUnit will create new instances despite PER_CLASS.
Solution
Step 1: Analyze list state without clearing
Without list.clear(), the list keeps accumulating elements from previous tests because the same instance is reused.
Step 2: Understand assertion impact
Each test expects list size 2, but after first test, list size grows beyond 2, causing assertions to fail in subsequent tests.
Final Answer:
Tests will fail because list keeps growing between tests. -> Option B
Quick Check:
Shared state must be reset to avoid test interference [OK]
Hint: Clear shared state in setup to avoid test interference [OK]
Common Mistakes:
Forgetting to reset mutable fields in PER_CLASS tests
Assuming new instance per test with PER_CLASS
Ignoring side effects between tests
5. You want to optimize a test class by sharing expensive setup across tests using @TestInstance(TestInstance.Lifecycle.PER_CLASS). Which of the following is the best practice to avoid flaky tests?
hard
A. Remove all assertions to speed up tests.
B. Avoid using @BeforeEach and rely on constructor initialization only.
C. Reset all mutable shared state in a @BeforeEach method.
D. Use static fields to hold shared state instead of instance fields.
Solution
Step 1: Understand shared state risks
With PER_CLASS lifecycle, the same instance is reused, so mutable fields keep their values across tests, risking interference.
Step 2: Apply best practice for test isolation
Resetting mutable shared state in @BeforeEach ensures each test starts fresh, preventing flaky or dependent tests.
Final Answer:
Reset all mutable shared state in a @BeforeEach method. -> Option C
Quick Check:
Reset shared state before each test to avoid flaky tests [OK]
Hint: Always reset mutable state in @BeforeEach with PER_CLASS [OK]
Common Mistakes:
Relying only on constructor for setup with PER_CLASS
Using static fields causing global state issues
Ignoring test isolation principles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.