Chapter 2. Exploring core JUnit

 

Mistakes are the portals of discovery.

James Joyce

This chapter covers

  • Using the core JUnit classes
  • Understanding JUnit mechanisms
  • Understanding the JUnit lifecycle

In chapter 1, we decided that we need a reliable and repeatable way to test our program. Our solution is to write or reuse a framework to drive test code that exercises our program’s API. As our program grows with new classes and new methods to existing classes, we need to grow our test code as well. Experience has taught us that sometimes classes interact in unexpected ways; we need to make sure that we can run all of our tests at any time, no matter what code changes took place. The question becomes, how do we run multiple test classes? And how do we find out which tests passed and which ones failed?

In this chapter, we look at how JUnit provides the functionality to answer those questions. We begin with an overview of the core JUnit concepts—the test class, test suite, and test runner. We take a close look at the core test runners and the test suite before we revisit our old friend the test class. We also examine how the core classes work together.

Then, in the next chapter, we use an example application to show you how to use these core JUnit concepts. We demonstrate best practices for writing and organizing test code.

2.1. Exploring core JUnit

The CalculatorTest program from chapter 1, shown in listing 2.1, defines a test class with a single test method testAdd.

2.2. Running parameterized tests

2.3. JUnit test runners

2.4. Composing tests with a suite

2.5. Summary