JUnit 5 Annotations | Java Fresher Interview
Fast recall
Quick revision
Core answer
@TestInstance(PER_CLASS) permits instance methods. A disabled reason is optional in the API but valuable in reports.Interview-ready guide
Interview answer
The main Jupiter annotations begin with @Test, which marks a test method. @BeforeEach and @AfterEach run around each test invocation, so they are useful for per-test setup and cleanup. @BeforeAll and @AfterAll run once for the test container. Under the default PER_METHOD lifecycle those class-level methods are normally static; @TestInstance(PER_CLASS) allows instance methods and also means the same test object is reused.
@DisplayName gives a class or method readable report text. @Disabled skips a test or container; its reason is optional, though adding one helps explain the skip. @Nested groups scenarios in non-static inner classes. The important choice is scope: use an each-hook for state that must be rebuilt per invocation and an all-hook only for genuinely shared setup. A lifecycle hook does not by itself make a database, singleton, or other external state fresh.
JUnit 5 Annotations | Java Fresher Interview
Direct Answer
Core Jupiter annotations include @Test, @BeforeEach/@AfterEach, @BeforeAll/@AfterAll, @DisplayName, @Disabled, and @Nested. Before-all and after-all methods are normally static under the default per-method lifecycle; @TestInstance(PER_CLASS) permits instance methods. A disabled reason is optional in the API but valuable in reports.
Code Example
class OrderServiceTest {
private OrderService orderService;
@BeforeAll
static void initShared() { // static under default PER_METHOD lifecycle
System.out.println("start suite");
}
@BeforeEach
void setUp() { // before each test invocation
orderService = new OrderService(new InMemoryRepo());
}
@Test
@DisplayName("applies 10% discount for premium customers")
void appliesPremiumDiscount() {
assertEquals(90.0, orderService.priceFor(premium(), 100.0));
}
@Test
@Disabled("flaky until clock is injected — see TICKET-123")
void expiresAtMidnight() { }
@AfterEach
void tearDown() { orderService.close(); } // after each invocation
@AfterAll
static void cleanup() { System.out.println("end suite"); }
}
Common Mistakes & Pitfalls
@BeforeAllnot static under the default lifecycle. WithPER_METHOD, a once-per-class hook is normallystatic;@TestInstance(PER_CLASS)permits an instance method.- Confusing Each vs All.
@BeforeEachruns before each test invocation;@BeforeAllruns once for the container. Put setup at the scope where it belongs. @Disabledwith no explanation. The reason is optional, but a useful reason keeps the report actionable.- Mixing JUnit generations accidentally. Jupiter uses
@BeforeEach/@AfterEach; JUnit 4 uses@Before/@Afterand needs the Vintage engine when run on the Platform.
Check your understanding
Answer these without looking back at the article.
- Why must @BeforeAll be static in JUnit 5?
- What is @TestInstance(Lifecycle.PER_CLASS)?
- How do you group tests with @Nested?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.