0
0
JUnittesting~20 mins

@BeforeAll method in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 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.