What is the purpose of the @BeforeEach annotation in JUnit?
The @BeforeEach annotation marks a method to be run before each test method. It is used to set up common test data or state so each test starts fresh.
Click to reveal answer
beginner
When is a method annotated with @BeforeEach executed in the test lifecycle?
It runs before every individual test method in the test class, ensuring setup code is executed freshly for each test.
Click to reveal answer
intermediate
Can you have multiple @BeforeEach methods in one test class?
Yes, you can have multiple @BeforeEach methods. They will all run before each test method, but the order is not guaranteed unless explicitly controlled.
Click to reveal answer
beginner
Write a simple example of a @BeforeEach method in JUnit that initializes a list before each test.
import org.junit.jupiter.api.BeforeEach;
import java.util.ArrayList;
import java.util.List;
class MyTests {
List<String> items;
@BeforeEach
void setup() {
items = new ArrayList<>();
}
// test methods here
}
Click to reveal answer
beginner
Why is using @BeforeEach better than repeating setup code inside every test method?
Using @BeforeEach avoids duplication, makes tests cleaner, and ensures consistent setup. It helps tests stay independent and easier to maintain.
Click to reveal answer
What does the @BeforeEach annotation do in JUnit?
ARuns a method before each test method
BRuns a method after all tests
CMarks a test method to be ignored
DRuns a method only once before all tests
✗ Incorrect
@BeforeEach runs the annotated method before every test method to prepare test data or state.
Can you have more than one @BeforeEach method in a test class?
AYes, but order is not guaranteed
BNo, only one is allowed
CYes, and order is always alphabetical
DNo, it causes a compilation error
✗ Incorrect
You can have multiple @BeforeEach methods, but their execution order is not guaranteed unless controlled.
When is a @BeforeEach method executed?
AAfter each test method
BBefore all test methods once
COnly if a test fails
DBefore each test method
✗ Incorrect
@BeforeEach methods run before each test method to set up the test environment.
Which of these is a good use for @BeforeEach?
ACleaning up resources after tests
BInitializing objects needed for tests
CSkipping tests conditionally
DLogging test results
✗ Incorrect
@BeforeEach is used to initialize or reset objects before each test runs.
What happens if a @BeforeEach method throws an exception?
AJUnit ignores the exception
BThe test method runs anyway
CThe test method is skipped and marked failed
DAll tests in the class are skipped
✗ Incorrect
If a @BeforeEach method throws an exception, the related test is skipped and marked as failed.
Explain the role of the @BeforeEach method in JUnit testing and why it is useful.
Think about how you prepare for each test to run independently.
You got /4 concepts.
Describe a scenario where using multiple @BeforeEach methods might be helpful.
Imagine you have several independent setup steps before tests.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @BeforeEach annotation in JUnit?
easy
A. To clean up resources after all tests have run
B. To run a method only once before all tests in the class
C. To mark a method as a test case
D. To run a method before each test method to set up test conditions
Solution
Step 1: Understand the role of @BeforeEach
The @BeforeEach annotation marks a method to run before every test method, preparing the test environment.
Step 2: Compare with other annotations
@BeforeAll runs once before all tests, @Test marks test methods, and cleanup is done with @AfterAll or @AfterEach.
Final Answer:
To run a method before each test method to set up test conditions -> Option D
Quick Check:
@BeforeEach = setup before every test [OK]
Hint: Runs setup before each test method to avoid repetition [OK]
Common Mistakes:
Confusing @BeforeEach with @BeforeAll
Thinking it runs after tests
Using it to mark test methods
2. Which of the following is the correct way to declare a @BeforeEach method in JUnit 5?
easy
A. public void setup() { }
B. @BeforeEach public void setup() { }
C. void @BeforeEach setup() { }
D. public @BeforeEach void setup() { }
Solution
Step 1: Recall correct annotation syntax
Annotations like @BeforeEach must be placed before the method signature with proper access modifier and return type.
Step 2: Check each option
@BeforeEach public void setup() { } correctly places @BeforeEach before a public void method. public void setup() { } lacks the annotation. public @BeforeEach void setup() { } incorrectly places the access modifier before the annotation (syntax error). void @BeforeEach setup() { } misplaces the annotation after the return type.
Final Answer:
@BeforeEach public void setup() { } -> Option B
Quick Check:
Annotation before method signature = correct [OK]
Hint: Annotation goes immediately before method declaration [OK]
Common Mistakes:
Placing annotation after method name
Omitting public access modifier
Missing parentheses in method declaration
3. Given the following JUnit 5 test class, what will be the output when running the tests?
A. Setup count: 1
Test One count: 1
Setup count: 1
Test Two count: 1
B. Setup count: 1
Test One count: 1
Test Two count: 1
Setup count: 2
C. Setup count: 1
Test One count: 1
Setup count: 2
Test Two count: 2
D. Setup count: 0
Test One count: 0
Setup count: 0
Test Two count: 0
Solution
Step 1: Understand @BeforeEach execution
The setup() method runs before each test, incrementing count and printing it.
Step 2: Trace the output for each test
Before testOne(), count becomes 1 and prints "Setup count: 1" then "Test One count: 1". Before testTwo(), count increments to 2 and prints "Setup count: 2" then "Test Two count: 2".
Final Answer:
Setup count: 1
Test One count: 1
Setup count: 2
Test Two count: 2 -> Option C
Quick Check:
@BeforeEach runs before each test, incrementing count [OK]
Hint: Remember @BeforeEach runs before every test method [OK]
Common Mistakes:
Assuming count resets before each test
Thinking @BeforeEach runs only once
Mixing order of print statements
4. Identify the error in the following JUnit 5 test class using @BeforeEach:
import org.junit.jupiter.api.*;
class TestExample {
int value;
@BeforeEach
void initialize() {
value = 10;
}
@Test
void testValue() {
Assertions.assertEquals(10, value);
}
@BeforeEach
void reset() {
value = 20;
}
}
medium
A. Multiple @BeforeEach methods cause both to run before each test
B. Only the first @BeforeEach method runs, so value is 10 in test
C. JUnit throws an error because multiple @BeforeEach methods are not allowed
D. The test fails because value is never initialized