Sub topic 5
Checking Results with Assertions
A test needs a way to say "this result is right" or "this result is wrong." In JUnit you do that with assertions. An assertion is a method call that checks a value. If the check passes, nothing happens and the test keeps going. If the check fails, JUnit stops the test and marks it as failed.
JUnit's assertions are static methods in the class org.junit.jupiter.api.Assertions. A static import lets you call them by their short names, such as assertEquals, so you don't have to write Assertions.assertEquals every time.
How an Assertion Decides
The Most Common Assertions
| Assertion | What it checks |
|---|---|
| `assertEquals(expected, actual)` | The two values are equal |
| `assertNotEquals(unexpected, actual)` | The two values are different |
| `assertTrue(condition)` | The condition is `true` |
| `assertFalse(condition)` | The condition is `false` |
| `assertNull(value)` | The value is `null` |
| `assertNotNull(value)` | The value is not `null` |
| `assertAll(...)` | Runs a group of checks and reports every failure |
A Worked Example
This test checks some behavior of Java's built-in String and List classes, so you can run it without writing any other code first.
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class AssertionExamplesTest {
@Test
void upperCaseChangesLetters() {
String result = "hello".toUpperCase();
assertEquals("HELLO", result);
}
@Test
void listKnowsWhatItContains() {
List<String> fruits = List.of("apple", "pear");
assertTrue(fruits.contains("apple"));
assertFalse(fruits.isEmpty());
}
@Test
void mapReturnsNullForMissingKey() {
Map<String, Integer> ages = new HashMap<>();
ages.put("Ana", 30);
assertNotNull(ages.get("Ana"));
assertNull(ages.get("Ben"));
}
@Test
void checkSeveralThingsAtOnce() {
String name = "JUnit";
assertAll(
() -> assertEquals(5, name.length()),
() -> assertTrue(name.startsWith("J")),
() -> assertEquals("junit", name.toLowerCase()));
}
}Inside assertAll, each check is written as a lambda, which is a small piece of code written as () -> .... JUnit runs every lambda, even when an earlier one fails. Then it lists all the failures together. Without assertAll, the test stops at the first failed assertion, so you only find out about one problem per run.
Order Matters: Expected First
Adding a Failure Message
Every assertion can take an extra last argument, which is a message that JUnit prints when the check fails. Use it to explain what went wrong.
assertEquals(5, result, "2 + 3 should equal 5");Exercise
A test contains assertTrue(total == 10);. When it fails, the message does not show what total actually was. Rewrite the line so the failure message shows both the expected and the actual value.
Show answer
Use assertEquals(10, total);. Put the expected value first and the actual value second. On failure, JUnit reports something like "expected: <10> but was: <8>".
Exercise
You check three fields of an object with three separate assertEquals calls. The first one fails. How many problems does JUnit report, and how could you see all of them in one run?
Show answer
JUnit reports only the first failure, because the test stops there. Wrap the three checks in assertAll(() -> ..., () -> ..., () -> ...) so JUnit runs all of them and reports every failure together.