Testing

This chapter covers writing JUnit tests for an ADT, organizing them as one contract suite that runs against every implementation, and test-first development.

After reading this chapter, you should be able to:

  • Explain why correctness is measured against an ADT contract rather than against the code
  • Explain why passing tests are evidence of correctness rather than proof
  • Write focused JUnit tests that check one behavior each, are named by scenario, and start from a fresh @BeforeEach fixture
  • Test that a method throws the exception its contract names, using the try / fail / catch idiom
  • Write a contract test suite once, as an abstract class with a factory method, and run it against every implementation
  • Choose edge cases for a list from its contract
  • Explain how writing tests before the implementation turns the contract into an executable specification

You can download the accompanying code for this chapter: starter and solution.

Sections

  1. How Do We Know It’s Correct?
  2. JUnit, a Testing Library
  3. Your First Test
  4. Writing Good Tests
  5. Saying the Setup Once
  6. Testing That a Method Throws
  7. From Testing a Class to Testing a Contract
  8. One Suite, Many Implementations
  9. Covering the Edge Cases
  10. Test-First Development
  11. Kinds of Testing