A.1. A basic JUnit test class

In short, JUnit test classes are plain old Java classes that have one or more test methods and zero or more setup and teardown methods. JUnit also defines a simple lifecycle for executing the tests it finds.

The following sections will walk you through these fundamental elements of JUnit test classes one by one, starting with declaring test methods.

A.1.1. Declaring test methods

JUnit tests in their most basic incarnation are regular instance methods marked with a certain annotation. These methods exercise the code they’re testing and make assertions about expected conditions and side effects, making use of APIs provided by JUnit. The following listing shows an example of what a simple JUnit test class might look ...

Get Effective Unit Testing 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.