0
0
JUnittesting~5 mins

TestInstance lifecycle per class in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
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>
What is the default lifecycle of test instances in JUnit 5?
APER_CLASS
BPER_TEST
CPER_SUITE
DPER_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
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
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.