0
0
JUnittesting~5 mins

@BeforeAll method in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AIt must be static.
BIt runs before each test method.
CIt can only be private.
DIt runs after all tests.
Which annotation allows @BeforeAll methods to be non-static?
A@TestInstance(Lifecycle.PER_CLASS)
B@BeforeEach
C@AfterAll
D@Test
How many times does a @BeforeAll method run in a test class?
ANever
BBefore each test
CAfter each test
DOnce before all tests
Where should you put code that needs to run before every test method?
A@BeforeAll method
B@AfterAll method
C@BeforeEach method
DInside the test method
Which of these is a good use case for @BeforeAll?
AChecking test results
BOpening a database connection once for all tests
CResetting test data before each test
DCleaning up after tests
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.