| Annotation | Role |
|---|---|
@Test | A test method. Not required to be public on Jupiter. |
@BeforeEach / @AfterEach | Per-test setup/teardown. |
@BeforeAll / @AfterAll | Once per class. Methods must be static unless the class uses @TestInstance(Lifecycle.PER_CLASS). |
@DisplayName | Human title in reports. |
@Disabled | Skip — always attach a reason / ticket. |
@Nested | Inner class grouping. Non-static nested tests can see the outer instance. |
@Timeout | Fail if the test overruns. Prefer deterministic waits over generous sleeps. |
@Tag | Filter (fast, integration). |
assertEquals(expected, actual);
assertNotEquals(a, b);
assertTrue(cond);
assertFalse(cond);
assertNull(x);
assertNotNull(x);
assertSame(a, b); // ==
assertThrows(IAE.class, () -> money(-1));
assertDoesNotThrow(() -> money(0, "INR"));
assertAll(
() -> assertEquals(1, m.cents()),
() -> assertEquals("INR", m.currency())
);
Expected is the first argument of assertEquals. Use assertAll so one failure does not hide the next. Prefer AssertJ assertThat if the repo already uses it — do not mix styles in one class without a reason.
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void positive(int n) { assertTrue(n > 0); }
@ParameterizedTest
@CsvSource({"100,INR", "0,USD"})
void parses(long cents, String ccy) { new Money(cents, ccy); }
@ParameterizedTest
@MethodSource("invalid")
void rejects(long cents) {
assertThrows(IllegalArgumentException.class, () -> new Money(cents, "INR"));
}
static Stream<Long> invalid() { return Stream.of(-1L, -99L); }
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" ", "\t"})
void blankRejected(String name) { ... }
Need junit-jupiter-params. Method sources must be static unless PER_CLASS lifecycle.
var ex = assertThrows(IllegalArgumentException.class, () -> new Money(-1, "INR"));
assertTrue(ex.getMessage().contains("cents"));
Clock clock = Clock.fixed(Instant.parse("2024-01-15T00:00:00Z"), ZoneOffset.UTC);
Service svc = new Service(clock);
@Test
void readsFixture() throws Exception {
var url = getClass().getResource("/cases/sample.json");
assertNotNull(url);
String json = Files.readString(Path.of(url.toURI()));
}
Clock — do not call Instant.now() inside domain code you want to test.Files.readString (Java 11) over handwritten scanners.rejectsNegativeCents, not test1.@Tag("integration") and a slower suite. Keep them out of the default fast loop if they are expensive.@Disabled without an owner.@Before / junit.framework in new code.~ Good luck with your revision! ~