O'Reilly logo

Pragmatic Unit Testing in Java 8 with JUnit by Jeff Langr, Andy Hunt, Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Arrange, Act, and Assert Your Way to a Test

In the prior section, we ran a test that does…nothing. Now it’s time to flesh it out with code that vets the ScoreCollection class.

We want to start with a scenario—a test case—that provides an example of expected behavior of the target code. To test a ScoreCollection object, we can add the numbers 5 and 7 to it and expect that the arithmeticMean method will return 6 (because (5 + 7) / 2 is equal to 6).

Naming is important. We call this test answersArithmeticMeanOfTwoNumbers—that nicely summarizes the scenario laid out in the test method. Here’s the code:

iloveyouboss/4/test/iloveyouboss/ScoreCollectionTest.java
Line 1 
package​ iloveyouboss;
import​ static org.junit.Assert.*;
import​ static ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required