Each method should group code into these functional sections, separated by blank lines:
Arrange all necessary preconditions and inputs.
Act on the object or method under test.
Assert that the expected results have occurred.
Examples
@Test
public void test() {
String input = "abc";
String result = Util.reverse(input);
assertEquals("cba", result);
}
Benefits
- Clearly separates what is being tested from the setup and verification steps.
- Clarifies and focuses attention on a historically successful and generally necessary set of test steps.
- Makes some Test-Smells more obvious
- Assertions intermixed with “Act” code.
- Prevent Test Methods that try to test too many different things at once.