Why Unit Testing

Unit testing is when you (a programmer) write test code to verify units of code. The size of a unit isn’t precisely defined, so we’ll view a unit as a small bit of code that exhibits some useful behavior in your system. A unit on its own usually doesn’t represent complete end-to-end behavior. It instead represents some small subset of that end-to-end-behavior.

We’re coding in Java, so we write our unit tests in Java, too. We run these unit tests through JUnit, a tool that marks our tests as passing or failing.

Here are a few whens and whys for writing unit tests:

  • You just finished coding a feature and want to ensure that it works as you expect.

  • You want to document a change so that you and others later understand the choices ...

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.