FIR[S]T: [S]elf-Validating

“I’ve been writing tests for years,” says Pat. “Every once in a while, I write a main() method that drives some of my code. It spews a bunch of output onto the console using System.out.println(). I take a look at each result and compare it with what I expect the right answer to be.”

“That’s great,” says Dale, “Most of us have done that sometime in our career, but it doesn’t hold up so well to larger numbers of tests. I remember having to add lots of comments in main() to explain what I was testing next.”

“I split them into smaller methods when they get out of hand,” says Pat.

“Hmm…,” says Dale, sporting a bemused look. “I also recall having a problem remembering what the expected output should look like. Sometimes ...

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

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