0
0
JUnittesting~20 mins

@BeforeEach method in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of @BeforeEach initialization
Consider the following JUnit 5 test class. What will be the output when the test method runs?
JUnit
import org.junit.jupiter.api.*;

public class SampleTest {
    private int counter = 0;

    @BeforeEach
    void setup() {
        counter += 5;
        System.out.println("Setup called. Counter = " + counter);
    }

    @Test
    void testCounter() {
        System.out.println("Test running. Counter = " + counter);
        Assertions.assertEquals(5, counter);
    }
}
A
Setup called. Counter = 0
Test running. Counter = 0
B
Setup called. Counter = 10
Test running. Counter = 10
C
Setup called. Counter = 5
Test running. Counter = 5
D
Setup called. Counter = 5
Test running. Counter = 10
Attempts:
2 left
💡 Hint
Remember that @BeforeEach runs before every test method and the instance is recreated for each test.
assertion
intermediate
1:30remaining
Correct assertion to verify @BeforeEach effect
Given a @BeforeEach method that sets a list to contain exactly 3 elements before each test, which assertion correctly verifies this in a test method?
JUnit
import org.junit.jupiter.api.*;
import java.util.*;

public class ListTest {
    private List<String> items;

    @BeforeEach
    void setup() {
        items = new ArrayList<>();
        items.add("A");
        items.add("B");
        items.add("C");
    }

    @Test
    void testListSize() {
        // Which assertion is correct here?
    }
}
AAssertions.assertEquals(3, items.size());
BAssertions.assertTrue(items.isEmpty());
CAssertions.assertNull(items);
DAssertions.assertEquals(0, items.size());
Attempts:
2 left
💡 Hint
Check the size of the list after @BeforeEach runs.
🔧 Debug
advanced
2:30remaining
Why does the test fail despite @BeforeEach initialization?
Examine the code below. The test fails with a NullPointerException. What is the most likely cause?
JUnit
import org.junit.jupiter.api.*;
import java.util.*;

public class NullTest {
    private List<String> items;

    @BeforeEach
    void setup() {
        List<String> items = new ArrayList<>();
        items.add("X");
    }

    @Test
    void testNotNull() {
        Assertions.assertEquals(1, items.size());
    }
}
AThe items list is cleared after setup, causing NullPointerException.
BThe @BeforeEach annotation is missing, so setup is not called.
CThe test method runs before the setup method.
DThe setup method declares a new local variable 'items' instead of initializing the instance variable.
Attempts:
2 left
💡 Hint
Check variable scope inside the setup method.
🧠 Conceptual
advanced
1:30remaining
Behavior of @BeforeEach with static variables
If a static variable is modified inside a @BeforeEach method, what is the expected behavior across multiple test methods in the same class?
AThe static variable causes tests to run in parallel automatically.
BThe static variable retains its modified value across all test methods.
CThe static variable is reinitialized by JUnit before each test method.
DThe static variable is reset to default before each test method automatically.
Attempts:
2 left
💡 Hint
Static variables belong to the class, not instances.
framework
expert
3:00remaining
Order of execution with multiple lifecycle annotations
Given the following JUnit 5 test class, what is the correct order of printed output when the single test runs?
JUnit
import org.junit.jupiter.api.*;

public class LifecycleTest {

    @BeforeAll
    static void beforeAll() {
        System.out.println("BeforeAll");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("BeforeEach");
    }

    @Test
    void test() {
        System.out.println("Test");
    }

    @AfterEach
    void afterEach() {
        System.out.println("AfterEach");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("AfterAll");
    }
}
A
BeforeAll
BeforeEach
Test
AfterEach
AfterAll
B
BeforeEach
BeforeAll
Test
AfterEach
AfterAll
C
BeforeAll
Test
BeforeEach
AfterEach
AfterAll
D
BeforeAll
BeforeEach
AfterEach
Test
AfterAll
Attempts:
2 left
💡 Hint
Remember the order: BeforeAll once, BeforeEach before each test, AfterEach after each test, AfterAll once.