Keeping Tests Consistent with AAA

When we wrote tests for the first iloveyouboss example, we visually organized our tests into three chunks: arrange, act, and assert, also known as triple-A (AAA).

 
@Test
 
public​ ​void​ answersArithmeticMeanOfTwoNumbers() {
 
ScoreCollection collection = ​new​ ScoreCollection();
 
collection.add(() -> 5);
 
collection.add(() -> 7);
 
 
int​ actualResult = collection.arithmeticMean();
 
 
assertThat(actualResult, equalTo(6));
 
}

Back then, we added comments to identify each of the chunks explicitly, but these comments add no value once you understand the AAA idiom.

AAA is a part of just about every test you’ll write. With AAA, you:

  • Arrange. Before we execute the code we’re trying to test, ensure that the system ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.