Sub topic 2
Checking Results with Assertions
An assertion is a line in a test that checks a result. If the check fails, JUnit marks the test as failed and shows you what went wrong.
Common Assertions
All of these come from org.junit.jupiter.api.Assertions. You usually bring them in with a static import.
| Assertion | What it checks |
|---|---|
| `assertEquals(expected, actual)` | The two values are equal |
| `assertTrue(condition)` | The condition is true |
| `assertFalse(condition)` | The condition is false |
| `assertNull(value)` | The value is null |
| `assertAll(...)` | Runs a group of checks and reports every failure |
Testing Exceptions
An exception is an error object that Java throws when something goes wrong. Sometimes throwing one is the correct behavior. assertThrows passes only when the code inside it throws the exception type you name. It also returns that exception, so you can check its message.
Structuring and Naming Tests
Arrange-Act-Assert (AAA) is a simple way to lay out a test:
- Arrange: set up the objects you need.
- Act: call the method you are testing.
- Assert: check the result.
Give each test a name that says what it checks, such as addReturnsSum. @DisplayName lets you write a plain sentence that appears in the test report instead of the method name.
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class CalculatorTest {
@Test
@DisplayName("add returns the sum of two numbers")
void addReturnsSum() {
// Arrange
Calculator calculator = new Calculator();
// Act
int result = calculator.add(2, 3);
// Assert
assertEquals(5, result);
}
@Test
void divideByZeroThrows() {
Calculator calculator = new Calculator();
IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> calculator.divide(10, 0));
assertEquals("divisor must not be zero", ex.getMessage());
}
}Setup and Teardown
A fixture is the starting data that a test needs. Lifecycle methods let you build that fixture once in the code and reuse it across tests:
@BeforeEachruns before every test, so each test gets fresh data.@AfterEachruns after every test to clean up.@BeforeAlland@AfterAllrun once for the whole class. These methods must bestatic.
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ShoppingListTest {
private List<String> items;
@BeforeEach
void setUp() {
items = new ArrayList<>();
items.add("milk");
}
@AfterEach
void tearDown() {
items.clear();
}
@Test
void addingAnItemGrowsTheList() {
items.add("bread");
assertAll(
() -> assertEquals(2, items.size()),
() -> assertTrue(items.contains("bread")));
}
}Exercise
Why does assertAll help when a test checks several things?
Show answer
assertAll runs every check in the group and reports all the failures together. Without it, the test stops at the first failed assertion, and you never learn whether the later checks would have passed.
Exercise
Which annotation would you use to create a new list before every single test?
Show answer
@BeforeEach. It runs before each test method, so every test starts with its own fresh list.