[F]IRST: [F]ast!

The dividing line between fast and slow unit tests is somewhat arbitrary—as Justice Potter Stewart said, “I know it when I see it.” Fast tests deal solely in code and take a few milliseconds at most to execute. Slow tests interact with code that must handle external evil necessities such as databases, files, and network calls. They take dozens, hundreds, or thousands of milliseconds.

On a typical Java system, you’ll probably want a few thousand unit tests. If an average test takes 200 ms, you’ll wait over eight minutes each time to run 2,500 unit tests. Eight minutes might not seem terrible, but you’re not going to run an eight-minute set of tests too many times throughout your development day.

“So what?” Pat says. “I can run ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.