Recall & Review
beginner
What is the purpose of the
@BeforeAll method in JUnit?The
@BeforeAll method runs once before all test methods in a test class. It is used to set up shared resources or configurations needed for all tests.Click to reveal answer
beginner
When is the
@BeforeAll method executed in the test lifecycle?It is executed exactly one time before any test methods run in the test class.
Click to reveal answer
intermediate
What must be true about a
@BeforeAll method in JUnit 5 regarding its declaration?The <code>@BeforeAll</code> method must be static unless the test class is annotated with <code>@TestInstance(TestInstance.Lifecycle.PER_CLASS)</code>.Click to reveal answer
beginner
Example: How do you write a
@BeforeAll method to initialize a database connection?Example:<br>
@BeforeAll
static void setup() {
database.connect();
}Click to reveal answer
intermediate
Why should you avoid putting test assertions inside a
@BeforeAll method?Because
@BeforeAll is for setup only. Assertions belong in test methods to clearly show which test failed.Click to reveal answer
What is true about a
@BeforeAll method in JUnit 5 by default?✗ Incorrect
By default, @BeforeAll methods must be static because they run once before all tests.
Which annotation allows
@BeforeAll methods to be non-static?✗ Incorrect
Using @TestInstance(Lifecycle.PER_CLASS) lets @BeforeAll methods be instance methods (non-static).
How many times does a
@BeforeAll method run in a test class?✗ Incorrect
@BeforeAll runs exactly once before any tests in the class.
Where should you put code that needs to run before every test method?
✗ Incorrect
@BeforeEach runs before every test method, unlike @BeforeAll.
Which of these is a good use case for
@BeforeAll?✗ Incorrect
@BeforeAll is ideal for one-time setup like opening a database connection.
Explain the role and requirements of the
@BeforeAll method in JUnit testing.Think about when and how often setup code should run.
You got /4 concepts.
Describe a scenario where using
@BeforeAll is better than @BeforeEach.Consider efficiency and resource sharing.
You got /4 concepts.