0
0
JUnittesting~5 mins

Execution order of lifecycle methods in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @BeforeAll annotation in JUnit?
The @BeforeAll method runs once before all tests in the class. It is used to set up shared resources needed for all tests.
Click to reveal answer
beginner
In what order do JUnit lifecycle methods execute around each test?
For each test, JUnit runs @BeforeEach, then the test method, then @AfterEach.
Click to reveal answer
beginner
When is the @AfterAll method executed in JUnit?
The @AfterAll method runs once after all tests in the class have finished. It is used to clean up shared resources.
Click to reveal answer
beginner
True or False: @BeforeEach methods run before every test method in JUnit.
True. @BeforeEach methods run before each test method to prepare the test environment.
Click to reveal answer
intermediate
What is the difference between @BeforeAll and @BeforeEach in JUnit?
@BeforeAll runs once before all tests; @BeforeEach runs before each individual test method.
Click to reveal answer
Which JUnit annotation runs a method once before all tests in a class?
A@AfterEach
B@BeforeEach
C@BeforeAll
D@AfterAll
What is the correct order of execution for lifecycle methods around a single test?
A@BeforeAll → Test → @AfterAll
B@AfterEach → Test → @BeforeEach
C@AfterAll → Test → @BeforeAll
D@BeforeEach → Test → @AfterEach
When does the @AfterAll method run in JUnit?
AOnce after all tests
BBefore each test
CAfter each test
DBefore all tests
Which annotation is used to run code before every test method?
A@BeforeEach
B@AfterEach
C@AfterAll
D@BeforeAll
What is the main difference between @BeforeAll and @BeforeEach?
A@BeforeAll runs after all tests; @BeforeEach runs before all tests
B@BeforeAll runs once before all tests; @BeforeEach runs before each test
C@BeforeAll runs before each test; @BeforeEach runs once before all tests
DNo difference
Explain the execution order of JUnit lifecycle methods when running multiple tests in a class.
Think about setup and teardown steps that happen once versus those that happen for every test.
You got /5 concepts.
    Describe the purpose of each JUnit lifecycle annotation: @BeforeAll, @BeforeEach, @AfterEach, and @AfterAll.
    Consider when each method runs relative to the tests.
    You got /4 concepts.