Chapter 4. Testing with JUnit

 

This chapter covers:

  • 4.1 What is testing, and why do it?
  • 4.2 Introducing our application
  • 4.3 How to test a program
  • 4.4 Introducing JUnit
  • 4.5 The JUnit task: <junit>
  • 4.6 Generating HTML test reports
  • 4.7 Advanced <junit> techniques
  • 4.8 Best practices
  • 4.9 Summary

“Any program feature without an automated test simply doesn’t exist.”

Kent Beck, Extreme Programming Explained

At this point we’ve learned how Ant can build and run an application. But does the application work? Does it do what we wanted? Sure, we can use Ant to run the program after the compile, and we can then check the output, but is that adequate?

You can write code, but unless you’re going to write tests or formal proofs of correctness, you have no way of knowing if it works. You can pretend it does, but your end users will discover the truth. Unless you like fielding support calls, you need to be testing your code. Ant and JUnit make this possible. JUnit provides the test framework to write your tests in Java, and Ant runs the tests. The two go hand in hand. JUnit makes it easy to write tests. Ant makes it easy to run those tests, capture the results, and halt the build if a test fails. It will even create HTML reports.

4.1. What is testing, and why do it?

4.2. Introducing our application

4.3. How to test a program

4.4. Introducing JUnit

4.5. The JUnit task: <junit>

4.6. Generating HTML test reports

4.7. Advanced <junit> techniques

4.8. Best practices

4.9. Summary