Chapter 1. JUnit jump-start

 

Never in the field of software development was so much owed by so many to so few lines of code.

Martin Fowler

This chapter covers

  • Exploring JUnit
  • Installing JUnit
  • Writing our first test
  • Running tests

All code is tested.

During development, the first thing we do is run our own programmer’s “acceptance test.” We code, compile, and run. When we run, we test. The test may just be clicking a button to see if it brings up the expected menu. Nevertheless, every day, we code, we compile, we run, and we test.

When we test, we often find issues—especially on the first run. Therefore, we code, compile, run, and test again.

Most of us quickly develop a pattern for our informal tests: we add a record, view a record, edit a record, and delete a record. Running a little test suite like this by hand is easy enough to do, so we do it—over and over again.

Some programmers like this type of repetitive testing. It can be a pleasant break from deep thought and hardcoding. When our little click-through tests finally succeed, there’s a feeling of accomplishment: Eureka! I found it!

Other programmers dislike this type of repetitive work. Rather than run the test by hand, they prefer to create a small program that runs the test automatically. Play-testing code is one thing; running automated tests is another.

If you’re a play-test developer, this book is for you. We’ll show you how creating automated tests can be easy, effective, and even fun.

1.1. Proving it works

1.2. Starting from scratch

1.3. Understanding unit testing frameworks

1.4. JUnit design goals

1.5. Setting up JUnit

1.6. Testing with JUnit

1.7. Summary