Bird
Raised Fist0
JUnittesting~20 mins

@BeforeAll method in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit @BeforeAll Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of @BeforeAll method execution order
Consider the following JUnit 5 test class. What will be the output when running this test suite?
JUnit
import org.junit.jupiter.api.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class SampleTest {

    @BeforeAll
    void setup() {
        System.out.println("Setup before all tests");
    }

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

    @Test
    void testTwo() {
        System.out.println("Test Two");
    }
}
A
Test One
Setup before all tests
Test Two
B
Test One
Test Two
Setup before all tests
C
Setup before all tests
Test One
Test Two
D
Setup before all tests
Test Two
Test One
Attempts:
2 left
💡 Hint
Remember that @BeforeAll runs once before any test methods.
❓ assertion
intermediate
2:00remaining
Correct assertion to verify @BeforeAll initialization
Given a JUnit 5 test class with a static variable initialized in a @BeforeAll method, which assertion correctly verifies the variable is initialized before tests run?
JUnit
import org.junit.jupiter.api.*;

public class InitTest {
    static String sharedResource;

    @BeforeAll
    static void init() {
        sharedResource = "Ready";
    }

    @Test
    void testResource() {
        // Which assertion is correct here?
    }
}
AAssertions.assertFalse(sharedResource.equals("Ready"));
BAssertions.assertEquals("Ready", sharedResource);
CAssertions.assertTrue(sharedResource.isEmpty());
DAssertions.assertNull(sharedResource);
Attempts:
2 left
💡 Hint
The @BeforeAll method sets sharedResource to "Ready" before tests run.
🔧 Debug
advanced
2:00remaining
Identify the error with @BeforeAll method
What error will occur when running this JUnit 5 test class?
JUnit
import org.junit.jupiter.api.*;

public class ErrorTest {

    @BeforeAll
    void setup() {
        System.out.println("Setup");
    }

    @Test
    void test() {
        System.out.println("Test");
    }
}
Ajava.lang.IllegalStateException: Test instance lifecycle not supported
BNo error, test runs successfully
Cjava.lang.NullPointerException at setup()
Djava.lang.Exception: Method setup() should be static
Attempts:
2 left
💡 Hint
By default, @BeforeAll methods must be static unless using PER_CLASS lifecycle.
🧠 Conceptual
advanced
2:00remaining
Purpose of @BeforeAll in test lifecycle
Which statement best describes the purpose of the @BeforeAll method in JUnit 5?
A@BeforeAll runs once before all tests to perform setup shared by all tests.
B@BeforeAll runs before each test method to reset test data.
C@BeforeAll runs after all tests to clean up resources.
D@BeforeAll runs only if a test fails to retry the test.
Attempts:
2 left
💡 Hint
Think about when shared setup should happen in a test class.
❓ framework
expert
2:00remaining
Effect of @TestInstance on @BeforeAll method signature
How does annotating a JUnit 5 test class with @TestInstance(TestInstance.Lifecycle.PER_CLASS) affect the @BeforeAll method requirements?
AAllows @BeforeAll methods to be non-static because the test instance is shared.
BRequires @BeforeAll methods to be static regardless of lifecycle.
CDisables @BeforeAll methods entirely.
DMakes @BeforeAll methods run after each test method.
Attempts:
2 left
💡 Hint
Consider how test instance lifecycle affects method context.

Practice

(1/5)
1. What is the main purpose of the @BeforeAll annotation in JUnit 5?
easy
A. To run a method once after all test methods
B. To run a method before each test method
C. To run a method after each test method
D. To run a method once before all test methods in the class

Solution

  1. Step 1: Understand the role of @BeforeAll

    The @BeforeAll annotation marks a method to run once before any test methods in the class.
  2. Step 2: Compare with other lifecycle annotations

    @BeforeEach runs before each test, @AfterEach runs after each test, and @AfterAll runs once after all tests.
  3. Final Answer:

    To run a method once before all test methods in the class -> Option D
  4. Quick Check:

    @BeforeAll = run once before all tests [OK]
Hint: Runs once before all tests, not before each [OK]
Common Mistakes:
  • Confusing @BeforeAll with @BeforeEach
  • Thinking it runs after tests
  • Assuming it runs multiple times
2. Which of the following is the correct way to declare a @BeforeAll method in JUnit 5?
easy
A. public void setup() { ... }
B. private static void setup() { ... }
C. public static void setup() { ... }
D. void setup() { ... }

Solution

  1. Step 1: Recall method requirements for @BeforeAll

    In JUnit 5, @BeforeAll methods must be static and public (or package-private) to run before all tests.
  2. Step 2: Check each option's signature

    public void setup() { ... } is non-static, so invalid. private static void setup() { ... } is private, so invalid. void setup() { ... } is non-static and missing access modifier. public static void setup() { ... } is public static void, which is correct.
  3. Final Answer:

    public static void setup() { ... } -> Option C
  4. Quick Check:

    Static + public method = correct @BeforeAll signature [OK]
Hint: Must be static and public for @BeforeAll [OK]
Common Mistakes:
  • Omitting static keyword
  • Using private or protected access
  • Missing void return type
3. Consider the following JUnit 5 test class snippet:
public class SampleTest {
    static int counter = 0;

    @BeforeAll
    public static void init() {
        counter = 5;
    }

    @Test
    public void testOne() {
        counter += 1;
        assertEquals(6, counter);
    }

    @Test
    public void testTwo() {
        counter += 2;
        assertEquals(7, counter);
    }
}

What will happen when these tests run?
medium
A. testOne passes, testTwo fails
B. Both tests pass
C. testOne fails, testTwo passes
D. Both tests fail

Solution

  1. Step 1: Analyze @BeforeAll effect on counter

    The init() method sets counter = 5 once before all tests.
  2. Step 2: Trace testOne execution

    testOne adds 1 to counter (5 + 1 = 6) and asserts 6, so it passes.
  3. Step 3: Trace testTwo execution

    testTwo adds 2 to counter, but counter was modified by testOne and is now 6, so 6 + 2 = 8. It asserts 7, which fails.
  4. Final Answer:

    testOne passes, testTwo fails -> Option A
  5. Quick Check:

    Shared static state changes cause testTwo failure [OK]
Hint: Static state shared; second test sees changed value [OK]
Common Mistakes:
  • Assuming counter resets before each test
  • Ignoring static variable persistence
  • Expecting both tests to pass
4. Given this JUnit 5 test class:
public class DemoTest {

    @BeforeAll
    public void setup() {
        System.out.println("Setup");
    }

    @Test
    public void testA() {
        assertTrue(true);
    }
}

What is the problem with this code?
medium
A. The @Test method must be static
B. The @BeforeAll method must be static
C. The @BeforeAll method cannot print output
D. The @BeforeAll method must return a value

Solution

  1. Step 1: Check @BeforeAll method signature

    In JUnit 5, @BeforeAll methods must be static unless using a test instance lifecycle that allows non-static.
  2. Step 2: Identify error in code

    The setup() method is non-static, which causes a runtime error or test failure.
  3. Final Answer:

    The @BeforeAll method must be static -> Option B
  4. Quick Check:

    @BeforeAll requires static method [OK]
Hint: Remember: @BeforeAll methods must be static [OK]
Common Mistakes:
  • Making @BeforeAll method non-static
  • Thinking @BeforeAll can return values
  • Confusing @BeforeAll with @BeforeEach
5. You want to initialize a database connection once before all tests in a JUnit 5 test class. Which approach correctly uses @BeforeAll to achieve this, considering best practices?
hard
A. Use a static @BeforeAll method to open the connection and store it in a static variable
B. Use a non-static @BeforeAll method to open the connection
C. Open the connection inside each @Test method separately
D. Use @BeforeEach to open the connection before every test

Solution

  1. Step 1: Understand @BeforeAll usage for shared setup

    @BeforeAll runs once before all tests, ideal for expensive setup like database connections.
  2. Step 2: Apply static method and static variable for shared resource

    The method must be static, and the connection should be stored in a static variable to be accessible by all tests.
  3. Step 3: Evaluate other options

    Use a non-static @BeforeAll method to open the connection is invalid because @BeforeAll must be static. Open the connection inside each @Test method separately and D open connection multiple times, which is inefficient.
  4. Final Answer:

    Use a static @BeforeAll method to open the connection and store it in a static variable -> Option A
  5. Quick Check:

    Static @BeforeAll + static resource = efficient shared setup [OK]
Hint: Static method + static variable for shared setup [OK]
Common Mistakes:
  • Using non-static @BeforeAll method
  • Opening connection multiple times unnecessarily
  • Not storing connection in static variable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard