Copy-ready snippets and assertion lookup for writing JUnit 6 tests.
pom.xml — if IntelliJ shows the Fix button next to a missing import, click it to add it automatically.src/test/java/... (not src/main/java).com.yearup.hotel.Room in src/main/java → com.yearup.hotel.RoomTest in src/test/java.Test suffix: RoomTest, CalculatorTest, InventoryTest.@Test and is public void.| Import | What it gives you |
|---|---|
| import org.junit.jupiter.api.Test; | The @Test annotation that marks a method as a test. |
| import static org.junit.jupiter.api.Assertions.*; | All assertion methods (assertEquals, assertTrue, etc.) without the Assertions. prefix. |
static import matters. Without it you'd have to write Assertions.assertEquals(...) on every line. With it, you write just assertEquals(...).shouldX_whenY_thenZ). Pick one for your project — and stick with it. Consistency beats cleverness.| Method | When to use | Example |
|---|---|---|
| assertEquals(expected, actual) | Comparing two values for equality — numbers, strings, anything with .equals(). |
assertEquals(100.0, room.getRate()); |
| assertNotEquals(expected, actual) | Verifying two values are different. | assertNotEquals(0, list.size()); |
| assertTrue(boolean) | Verifying a condition is true — mostly boolean getters. | assertTrue(room.isOccupied()); |
| assertFalse(boolean) | Verifying a condition is false. | assertFalse(room.isDirty()); |
| assertArrayEquals(expected, actual) | Comparing two arrays element-by-element. | assertArrayEquals(new int[]{1,2,3}, result); |
| assertNull(actual) | Verifying a value is null. |
assertNull(repo.findById(999)); |
| assertNotNull(actual) | Verifying a value is NOT null. |
assertNotNull(repo.findById(1)); |
| fail(message) | Explicitly fail a test — useful in unreachable branches. | fail("Should have thrown an exception"); |
@Test method tests ONE thing. If you find yourself writing two // Act sections in the same method, split it into two tests with two clearly-named methods. A failing test should point you at exactly one bug.The AAA comments are PascalCase — capital first letter, no extra words.