Overview - TestInstance lifecycle per class
What is it?
TestInstance lifecycle per class is a setting in JUnit that controls how test objects are created and reused during test execution. When set to per class, JUnit creates one instance of the test class and runs all test methods on that same instance. This differs from the default per-method lifecycle, where a new test object is created for each test method. It helps manage shared state and resource usage during testing.
Why it matters
Without controlling the test instance lifecycle, tests might run with unexpected side effects or inefficient resource use. If each test method creates a new object, setup can be slow and sharing data between tests is hard. Using per class lifecycle allows tests to share setup and state, making tests faster and enabling scenarios that require shared context. Without it, tests might be slower or harder to write correctly.
Where it fits
Before learning this, you should understand basic JUnit test structure and annotations like @Test and @BeforeEach. After this, you can learn about advanced test lifecycle annotations like @BeforeAll, @AfterAll, and how to manage test state safely. This concept fits into mastering JUnit test execution and writing efficient, maintainable tests.