Chapter 3. Mastering JUnit
Kent Beck, Test First Development
This chapter covers
- Implementing a sample application
- Testing the sample application with JUnit
- Following JUnit best practices
So far, we’ve made a JUnit survey and shown how to use it (chapter 1). We also looked at JUnit internals, what the core classes and methods are, and how they interact with each other (chapter 2).
We now dive deeper by introducing a real-life component and testing it. In this chapter, we implement a small application using the Controller design pattern. We then test every part of the application using JUnit. We also look at JUnit best practices when writing and organizing your tests.
Core Java EE Patterns describes a controller as a component that “interacts with a client, controlling and managing the handling of each request,” and tells us that it’s used in both presentation-tier and business-tier patterns.[1]
1 Deepak Alur, John Crupi, and Dan Malks, Core Java EE Patterns: Best Practices and Design Strategies (Upper Saddle River, NJ: Prentice Hall, 2001).
In general, a controller does the following:
- Accepts requests
- Performs any common computations on the request
- Selects an appropriate request handler
- Routes the request so that the handler can execute the relevant business logic
- May provide a top-level handler for errors and exceptions