Core Test Annotations:
- @Test: Marks a method as a test method
- @ParameterizedTest: Indicates a parameterized test
- @RepeatedTest: Repeats test execution multiple times
- @TestFactory: Defines a method for dynamic test generation
Lifecycle Annotations:
- @BeforeEach: Executed before each test method
- @AfterEach: Executed after each test method
- @BeforeAll: Executed once before all test methods (static)
- @AfterAll: Executed once after all test methods (static)
Organization Annotations:
- @DisplayName: Provides custom display name for tests
- @Nested: Creates nested test classes for better organization
- @Tag: Tags tests for filtering and grouping
- @Disabled: Disables a test method or class
Example Usage:
@DisplayName("Calculator Tests")
class CalculatorTest {
@BeforeEach
void setUp() {
calculator = new Calculator();
}
@Test
@DisplayName("Should add two positive numbers correctly")
void testAddition() {
assertEquals(5, calculator.add(2, 3));
}
@Nested
@DisplayName("Division Tests")
class DivisionTests {
@Test
void testValidDivision() {
assertEquals(2, calculator.divide(10, 5));
}
@Test
@Disabled("Division by zero behavior under review")
void testDivisionByZero() {
// Test implementation
}
}
}
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
The model's verdict: “”
The interactive diagram is below the answer - jump to diagram ↓
This answer is explained by a shared concept diagram - open →