Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

About this Book

About this Book

Welcome to the second edition of JUnit in Action! If you’ve picked up this book, we suspect you’re a Java developer who cares about the quality of software you produce. Perhaps you’ve worked with the previous versions of the JUnit framework in the past, perhaps you’ve worked with other testing frameworks, or perhaps this is your first step into the testing world. Whichever path has led you here, you’re probably interested in improving your software process and the quality of the software you write. The goal of this book is to give the basic foundation you need—and much more. The world of software testing consists of many projects that solve specific tasks, of testing different components and layers of your application. The central player in this world is the JUnit framework. Written by Erich Gamma and Kent Beck about a decade ago, this framework has become the de facto standard in Java testing. The latest 4.x versions of the JUnit framework are much more than a revision of the old 3.x JUnit framework. If you haven’t heard anything about the JUnit framework yet, you might expect, based on the name, to find a new release of that old proven framework. But this is not the case. Unlike the old version of JUnit, the 4.x versions introduce a new approach and rewrite of the whole framework. Hence the need for an up-to-date copy of the first edition.

In this second edition of the book, we introduce the core concepts you need to know in order to start testing your projects with the JUnit framework. But that’s not the whole picture! This book will not only teach you how to write your test cases with the JUnit framework; it will also guide you through the process of writing your code, giving you suggestions of how to make it more testable. This book will also teach you about fundamental software development principles like test-driven development (TDD). It will also guide you step by step through the process of testing each and every layer of a typical Java EE application: the front layer, with external tools like Selenium and JSFUnit; the business layer, with tools like Cactus, mock objects, and stubs; and finally the database and JPA layer, with tools like DBUnit.

The book is organized into several parts, the goal being to walk you through JUnit in a sequence of increasing complexity. The first part contains the preliminary chapters that introduce the technological context of the framework, give a high-level overview of the architecture, and present a bare-bones HelloWorld sample application to get your environment up and running. After this brief introduction, we set off into a series of chapters that cover the core concepts and components of the framework one by one. We take time to explain the functionality of each component in depth.

The second part of the book deals with the different techniques of testing: the mock approach and the in-container approach. It introduces some new tools to create the fake objects we need. The third and fourth parts of the book look into detailed explanations of third-party tools/JUnit extensions that we use to test the different layers of our applications. In addition, the book has several appendixes that will help you to switch easily to the latest version of JUnit and integrate easily with your favorite IDE.


Chapter 1 gets you started right away. The gentle introduction defines what testing is, how to perform it efficiently, and how to write your first test cases. This chapter is a must to give you the confidence to realize that testing is something natural that should always happen during development.

Chapter 2 dives into the architecture of JUnit and shows how it’s organized. We introduce most of the common features of JUnit in this chapter.

In chapter 3 we start to build a sample real-life application. You get to know several design patterns and use them to build our application. Later in the chapter, we demonstrate how to test the application efficiently using the JUnit features introduced in chapter 2.

Chapter 4 looks at several important aspects: the need for unit testing, the various flavors of software tests that exist, and the difference between those kinds of tests. We also give handy advice on how to set up different development and testing environments.

In chapter 5 we discuss the quality of tests. We go on to answer several key questions, such as how to improve your tests, how to improve your test coverage, and how to design your application architecture in such a way that your application will be easily testable. The last point is a brief introduction to the test-driven development (TDD) approach.

Chapter 6 takes a closer look at stubbing as a technique for faking system resources that normally aren’t available. We use an example of stubbing a servlet container by using the Jetty embedded servlet container.

Chapter 7 demonstrates another technique for incorporating fake objects in your tests: mock objects. This technique is useful when you program against a closed API and you can’t modify or instantiate the available resources. In this chapter, we give an example of mocking a servlet and testing it by using two of the most popular frameworks, EasyMock and JMock.

Chapter 8 briefly introduces the final technique that we can use when we’re missing important system objects: in-container testing. We provide this introduction so that in chapters 14, 15, and 16 we can expand on it and discuss real-world examples of in-container testing. Chapter 8 also serves as a summary chapter for this part of the book, so it compares the previously discussed approaches: stubs, mocks, and in-container testing.

Chapter 9 is the opening chapter for the third part of the book. In this part, we focus on the integration of JUnit with various build frameworks; specifically in this chapter, we introduce the Ant build framework. We show you how to execute your tests automatically and how to produce efficient, great-looking reports with the results of the execution. We run some of the examples from the previous chapter using the Ant framework.

Chapter 10 continues the approach of introducing build frameworks and integrating JUnit with them. This time we take a closer look at Maven.

Chapter 11 is dedicated to the theory of continuous integration (CI)—building our project and executing our tests in a continuous manner in order to make sure none of our changes break the project. We take a closer look at two of the most popular software projects for practicing continuous integration: CruiseControl and Hudson. We also take the opportunity to import some of our previous examples into both of the tools, set them up, and execute them.

Chapter 12 opens the last part of the book. This part deals with various JUnit extensions, which enhance the testing framework to do specific tasks that normally aren’t possible. Also in this last part of the book, we walk through all the layers of a typical application and explain how to test those layers. Chapter 12 deals with the presentation layer of a web application. We introduce the HtmlUnit and Selenium tools and show exactly how to use them.

Chapter 13 continues with the presentation layer of a web application, but this time we focus on one of the hardest parts: Ajax. We detail what Ajax is and why it’s difficult to test, and we also describe various testing scenarios. Finally, we introduce the JsUnit project and give some special hints on testing a Google Web Toolkit (GWT) application.

Chapter 14 explores testing your presentation layer with a different approach: the in-container testing we introduced in chapter 8. For this purpose, we introduce the first in-container testing framework ever made: the Apache Cactus project.

Chapter 15 reveals techniques that are specifically applicable for testing JSF applications. This chapter explains how to use another recent tool called JSFUnit—a new, in-container testing framework that builds on Apache Cactus and is specifically designed to test JSF applications.

Chapter 16 is for those of you who are interested in OSGi applications. It starts with a brief introduction of what OSGi means. Then we introduce the JUnit4OSGi extension of JUnit and show several techniques for testing OSGi applications, using both mocking and in-container testing.

Chapter 17 is the first of the last three chapters, which deal with database testing. Here we tell you everything you need to know about a project called DBUnit. We demonstrate several techniques for testing your database, regardless of the persistence technology that you use.

Chapter 18 reveals all the secrets of JPA testing: testing multilayered applications and JPA persistence-layer applications.

Chapter 19 is the final chapter. Here we demonstrate techniques for making your tests more efficient. We introduce a new project that will help you to test your Spring applications: Unitils.

Code conventions

The following typographical conventions are used throughout the book:

  • Courier typeface is used in all code listings.

  • Courier typeface is used within text for certain code words.

  • Italics are used for emphasis and to introduce new terms.

  • Annotations are used in place of inline comments in the code. These highlight important concepts or areas of the code. Annotations appear with numbered bullets like this that are referenced later in the text.

In addition, in the code listings you might occasionally find

  • bold code—We use this for two purposes: to highlight some of the Java keywords (for your convenience) or to highlight the differences between two or more code listings.

Code downloads

You can download the sample code for this book via a link found on the book’s home page on the Manning website,, or This page contains a folder structure of all the submodules for the different chapters. Each of the subfolders contains a build script to compile and package, and you can execute the tests associated with it. Instructions on how to install the application are contained in a README file in that download.

We should make a couple of points about the source code. Initially we wanted to have a large-scale application demonstrating the various testing approaches in the application layers. Later, we realized the difficulties of having such a large-scale application, and instead we followed the folder-structure notation; each chapter has a source code example associated with it. Those are split into subfolders, clearly labeled with the name of the chapter. All of them contain a Maven build script, and some of them contain an Ant build script as well. In order to run the examples in the book, you will need to have Maven2 installed on your computer.

Author Online

The purchase of JUnit in Action, Second Edition, includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the authors and other users. You can access and subscribe to the forum at This page provides information on getting on the forum once you’re registered, what kind of help is available, and the rules of conduct in the forum.

Manning’s commitment to our readers is to provide a venue where a meaningful dialogue among individual readers and between readers and authors can take place. It’s not a commitment to any specific amount of participation on the part of the authors, whose contribution to the book’s forum remains voluntary (and unpaid). We suggest you try asking the authors some challenging questions, lest their interest stray!

The Author Online forum and the archives of previous discussions will be accessible from the publisher’s website as long as the book is in print.

About the title

By combining introductions, overviews, and how-to examples, the In Action books are designed to help with learning and remembering. According to research in cognitive science, the things people remember are things they discover during self-motivated exploration.

Although no one at Manning is a cognitive scientist, we’re convinced that for learning to become permanent, it must pass through stages of exploration, play, and, interestingly, retelling of what is being learned. People understand and remember new things, which is to say they master them, only after actively exploring them. Humans learn in action. An essential part of an In Action book is that it is example driven. It encourages the reader to try things out, to play with new code, and to explore new ideas.

There is another, more mundane reason for the title of this book: our readers are busy. They use books to do a job or solve a problem. They need books that allow them to jump in and jump out easily and learn just what they want just when they want it.

They need books that aid them in action. The books in this series are designed for such readers.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint