Sub topic 7
Structuring and Naming Tests
A test that passes today will be read again months from now, often when it fails. A clear structure and a clear name tell the reader what went wrong without making them study the code. This chapter shows a simple shape for every test and a few ways to name tests well.
The Arrange-Act-Assert Pattern
Arrange-Act-Assert (AAA) is a pattern that splits each test into three parts:
- Arrange: set up the objects and data the test needs.
- Act: call the one method you want to test.
- Assert: check that the result is what you expected.
Each part has one job, so a reader can see at a glance what is being tested and how.
Here is a test for the Calculator class from earlier chapters, written in the AAA style:
import static org.junit.jupiter.api.Assertions.assertEquals;
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);
}
}The comments are optional. Once you are used to the pattern, a blank line between the parts works just as well.
Naming Test Methods
A test name should say what behavior is checked and what result is expected. A name like test1 or testAdd says almost nothing when it shows up in a failure report.
Here are some common naming styles:
| Style | Example |
|---|---|
| Behavior sentence | `addReturnsSum` |
| Method_Condition_Result | `divide_byZero_throwsException` |
| should style | `shouldThrowWhenDivisorIsZero` |
Any of these styles works. Pick one and use it across the whole project, because a consistent style is easier to scan.
Readable Names with @DisplayName
Java method names cannot contain spaces. @DisplayName is a JUnit annotation that gives a test a readable label. That label replaces the method name in your IDE and in test reports.
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Calculator")
class CalculatorNamingTest {
@Test
@DisplayName("divides 10 by 2 to get 5")
void divideReturnsQuotient() {
Calculator calculator = new Calculator();
int result = calculator.divide(10, 2);
assertEquals(5, result);
}
}When this test runs, the report shows "Calculator" with "divides 10 by 2 to get 5" under it. You can put @DisplayName on a class as well as on a method.
Where Test Files Go
By convention, tests live in src/test/java, in the same package as the class they test. The test class takes the name of that class plus Test, so Calculator gets CalculatorTest. Maven and Gradle find these files automatically.
Exercise
This test has a vague name and mixes its steps together: @Test void test2() { assertEquals(7, new Calculator().add(3, 4)); }. Rewrite it using Arrange-Act-Assert, a descriptive method name, and a @DisplayName.
Show answer
@Test @DisplayName("add returns 7 for 3 and 4") void addReturnsSumOfThreeAndFour() { Calculator calculator = new Calculator(); int result = calculator.add(3, 4); assertEquals(7, result); } The first line arranges, the second acts, and the third asserts. Both the method name and the display name describe the expected behavior.
Exercise
Why is it a good idea to keep only one Act step in each test?
Show answer
When a test checks just one behavior and that test fails, its name tells you exactly what broke. A test that runs many steps can fail in many places, so you have to dig through it to find the cause.