What is the purpose of lifecycle hooks in JUnit testing?
Lifecycle hooks in JUnit manage setup and teardown tasks to prepare the test environment before tests run and clean up after tests finish. This ensures tests run independently and reliably.
Click to reveal answer
beginner
Name two common lifecycle hooks in JUnit and their roles.
Common lifecycle hooks are @BeforeEach, which runs setup code before each test, and @AfterEach, which runs teardown code after each test to reset the environment.
Click to reveal answer
beginner
Why is it important to use teardown methods after tests?
Teardown methods clean up resources like files or database connections to prevent side effects on other tests, keeping tests independent and reliable.
Click to reveal answer
intermediate
How do lifecycle hooks improve test reliability?
By running setup and teardown automatically, lifecycle hooks ensure each test starts fresh and ends clean, avoiding leftover data or states that could cause test failures.
Click to reveal answer
intermediate
Explain the difference between @BeforeAll and @BeforeEach in JUnit.
@BeforeAll runs once before all tests in a class, useful for expensive setup. @BeforeEach runs before every test method, preparing a clean state for each test.
Click to reveal answer
Which JUnit annotation is used to run setup code before each test method?
A@BeforeEach
B@AfterEach
C@BeforeAll
D@AfterAll
✗ Incorrect
@BeforeEach runs setup code before every test method to prepare the test environment.
What is the main reason to use teardown methods in tests?
ATo generate test reports
BTo speed up test execution
CTo write test assertions
DTo clean up resources and reset state after tests
✗ Incorrect
Teardown methods clean up resources and reset the environment to keep tests independent and avoid side effects.
Which lifecycle hook runs once before all tests in a JUnit test class?
A@AfterEach
B@BeforeAll
C@BeforeEach
D@AfterAll
✗ Incorrect
@BeforeAll runs once before all tests, useful for expensive setup shared by all tests.
What happens if you don't use teardown methods after tests?
ATests run faster
BTests become more readable
CTests may interfere with each other due to leftover state
DTests automatically clean up
✗ Incorrect
Without teardown, leftover data or open resources can cause tests to affect each other, leading to unreliable results.
Why is it better to use lifecycle hooks instead of manual setup and cleanup inside each test?
ALifecycle hooks reduce code duplication and errors
BManual setup is faster
CLifecycle hooks make tests run slower
DManual cleanup is more reliable
✗ Incorrect
Lifecycle hooks centralize setup and teardown, reducing repeated code and mistakes, making tests easier to maintain.
Describe how lifecycle hooks in JUnit help manage setup and teardown for tests.
Think about what happens before and after each test method.
You got /5 concepts.
Explain the difference between @BeforeEach and @BeforeAll annotations in JUnit and when to use each.
Consider how often the setup code needs to run.
You got /4 concepts.
Practice
(1/5)
1. Why do we use lifecycle hooks like @BeforeEach and @AfterEach in JUnit tests?
easy
A. To write test cases faster by skipping assertions
B. To run setup and cleanup code before and after each test automatically
C. To make tests run in parallel
D. To generate test reports automatically
Solution
Step 1: Understand lifecycle hooks purpose
Lifecycle hooks like @BeforeEach and @AfterEach run code before and after each test method to prepare and clean up the test environment.
Step 2: Identify correct use case
This avoids repeating setup and teardown code inside every test, keeping tests clean and consistent.
Final Answer:
To run setup and cleanup code before and after each test automatically -> Option B
Quick Check:
Lifecycle hooks = setup/teardown automation [OK]
Hint: Lifecycle hooks automate setup and cleanup around tests [OK]
Common Mistakes:
Thinking lifecycle hooks skip assertions
Confusing lifecycle hooks with parallel test execution
Assuming lifecycle hooks generate reports
2. Which of the following is the correct syntax to run a method once before all tests in a JUnit 5 test class?
easy
A. @BeforeEach public void setup() {}
B. @AfterAll public void finish() {}
C. @BeforeAll public static void init() {}
D. @AfterEach public void cleanup() {}
Solution
Step 1: Identify the annotation for running once before all tests
The @BeforeAll annotation runs a method once before any tests in the class run.
Step 2: Check method signature requirements
Methods with @BeforeAll must be static in JUnit 5, so public static void init() is correct.
Final Answer:
@BeforeAll public static void init() {} -> Option C
Quick Check:
@BeforeAll + static = run once before all tests [OK]
Hint: @BeforeAll must be static to run once before all tests [OK]
Common Mistakes:
Using @BeforeEach instead of @BeforeAll for once-only setup
Step 1: Analyze the lifecycle annotations and method signatures
The @AfterAll method must be static, which it is. However, counter is static and incremented in @BeforeEach which is non-static, so no issue there.
Step 2: Check for JUnit 5 rules on lifecycle methods
JUnit 5 requires @BeforeEach and @AfterEach methods to be non-static, which is correct here. But @AfterAll is static, so no problem.
Step 3: Identify the problem
However, the class is missing the @TestInstance(Lifecycle.PER_CLASS) annotation if we want to use static fields properly with lifecycle hooks. Without it, static fields are fine but the test runner may not allow static @AfterAll without this annotation in some setups.
Step 4: Confirm if code compiles
Actually, this code compiles and runs fine. The counter increments twice (once per test), so 2 is printed.
Final Answer:
2 -> Option A
Quick Check:
Counter increments twice = 2 printed [OK]
Hint: Each @BeforeEach runs before every test, incrementing counter [OK]