JUnit Recipes
Practical Methods for Programmer Testing
J. B. Rainsberger with contributions by Scott Stirling
  • June 2004
  • ISBN 9781932394238
  • 752 pages
  • printed in black & white

No other unit testing book [offers] as much wisdom, knowledge, and practical advice .... a remarkable compendium ...

Robert C. Martin, from the Foreword

When testing becomes a developer's habit good things tend to happen—good productivity, good code, and good job satisfaction. If you want some of that, there's no better way to start your testing habit, nor to continue feeding it, than with JUnit Recipes. In this book you will find one hundred and thirty seven solutions to a range of problems, from simple to complex, selected for you by an experienced developer and master tester. Each recipe follows the same organization giving you the problem and its background before discussing your options in solving it.

JUnit—the unit testing framework for Java—is simple to use, but some code can be tricky to test. When you're facing such code you will be glad to have this book. It is a how-to reference full of practical advice on all issues of testing, from how to name your test case classes to how to test complicated J2EE applications. Its valuable advice includes side matters that can have a big payoff, like how to organize your test data or how to manage expensive test resources.

Table of Contents detailed table of contents




about this book

about the cover illustration

Part 1 The building blocks

1. Fundamentals

1.1. What is Programmer Testing?

1.2. Getting started with JUnit

1.3. A few good practices

1.4. Summary

2. Elementary tests

2.1. Test your equals method

2.2. Test a method that returns nothing

2.3. Test a constructor

2.4. Test a getter

2.5. Test a setter

2.6. Test an interface

2.7. Test a JavaBean

2.8. Test throwing the right exception

2.9. Let collections compare themselves

2.10. Test a big object for equality

2.11. Test an object that instantiates other objects

3. Organizing and building JUnit tests

3.1. Place test classes in the same package as production code

3.2. Create a separate source tree for test code

3.3. Separate test packages from production code packages

3.4. Factor out a test fixture

3.5. Factor out a test fixture hierarchy

3.6. Introduce a Base Test Case

3.7. Move special case tests to a separate test fixture

3.8. Build tests from the command line

3.9. Build tests using Ant

3.10. Build tests using Eclipse

4. Managing test suites

4.1. Let JUnit build your test suite

4.2. Collect a specific set of tests

4.3. Collect all the tests in a package

4.4. Collect all the tests for your entire system

4.5. Scan the file system for tests

4.6. Separate the different kinds of test suites

4.7. Control the order of some of your tests

4.8. Build a data-driven test suite

4.9. Define a test suite in XML

5. Working with test data

5.1. Use Java system properties

5.2. Use environment variables

5.3. Use an inline data file

5.4. Use a properties file

5.5. Use ResourceBundles

5.6. Use a file-based test data repository

5.7. Use XML to describe test data

5.8. Use Ant’s <sql> task to work with a database

5.9. Use JUnitPP

5.10. Set up your fixture once for the entire suite

5.11. Perform environment setup once for multiple test runs

5.12. Use DbUnit

6. Running JUnit tests

6.1. See the name of each test as it executes

6.2. See the name of each test as it executes with a text-based test runner

6.3. Execute a single test

6.4. Execute each test in its own JVM

6.5. Reload classes before each test

6.6. Ignore a test

7. Reporting JUnit results

7.1. Using a Base Test Case with a logger

7.2. Using Log4Unit

7.3. Getting plain text results with Ant

7.4. Reporting results in HTML with Ant’s <junitreport> task

7.5. Customizing <junit> XML reports with XSLT

7.6. Extending Ant’s JUnit results format

7.7. Implementing TestListener and extending TestRunner

7.8. Reporting a count of assertions

8. Troubleshooting JUnit

8.1. JUnit cannot find your tests

8.2. JUnit does not execute your custom test suite

8.3. JUnit does not set up your test fixture

8.4. Test setup fails after overriding runTest()

8.5. Your test stops after the first assertion fails

8.6. The graphical test runner does not load your classes properly

8.7. JUnit fails when your test case uses JAXP

8.8. JUnit fails when narrowing an EJB reference

Part 2 Testing J2EE


Designing J2EE applications for testability

The Coffee Shop application

9. Testing and XML

9.1. Verify the order of elements in a document

9.2. Ignore the order of elements in an XML document

9.3. Ignore certain differences in XML documents

9.4. Get a more detailed failure message from XMLUnit

9.5. Test the content of a static web page

9.6. Test an XSL stylesheet in isolation

9.7. Validate XML documents in your tests

10. Testing and JDBC

10.1. Test making domain objects from a ResultSet

10.2. Verify your SQL commands

10.3. Test your database schema

10.4. Verify your tests clean up JDBC resources

10.5. Verify your production code cleans up JDBC resources

10.6. Manage external data in your test fixture

10.7. Manage test data in a shared database

10.8. Test permissions when deploying schema objects

10.9. Test legacy JDBC code without the database

10.10. Test legacy JDBC code with the database

10.11. Use schema-qualified tables with DbUnit

10.12. Test stored procedures

11. Testing Enterprise JavaBeans

11.1. Test a session bean method outside the container

11.2. Test a legacy session bean

11.3. Test a session bean method in a real container

11.4. Test a CMP entity bean

11.5. Test CMP meta data outside the container

11.6. Test a BMP entity bean

11.7. Test a message-driven bean inside the container

11.8. Test a message-driven bean outside the container

11.9. Test a legacy message-driven bean

11.10. Test a JMS message consumer without the messaging server

11.11. Test JMS message-processing logic

11.12. Test a JMS message producer

11.13. Test the content of your JNDI directory

12. Testing web components

12.1. Test updating session data without a container

12.2. Test updating the HTTP session object

12.3. Test rendering a JavaServer Page

12.4. Test rendering a Velocity template

12.5. Test a JSP tag handler

12.6. Test your JSP tag library deployment

12.7. Test servlet initialization

12.8. Test the ServletContext

12.9. Test processing a request

12.10. Verify web page content without a web server

12.11. Verify web form attributes

12.12. Verify the data passed to a page template

12.13. Test a web resource filter

13. Testing J2EE applications

13.1. Test page flow

13.2. Test navigation rules in a Struts application

13.4. Test web resource security

13.5. Test EJB resource security

13.6. Test container-managed transactions

Part 3 More JUnit techniques

14. Testing design patterns

14.1. Test an Observer (Event Listener)

14.2. Test an Observable (Event Source)

14.3. Test a Singleton

14.4. Test a Singleton’s client

14.5. Test an object factory

14.6. Test a template method’s implementation

15. GSBase

15.1. Verify events with EventCatcher

15.2. Test serialization

15.3. Test object cloning

15.4. Compare JavaBeans using 'appears equal'

16. JUnit-addons

16.1. Test your class for compareTo()

16.2. Collect tests automatically from an archive

16.3. Organize test data using PropertyManager

16.4. Manage shared test resources

16.5. Ensure your shared test fixture tears itself down

16.6. Report the name of each test as it executes

17. Odds and ends

17.1. Clean up the file system between tests

17.2. Test your file-based application without the file system

17.3. Verify your test case class syntax

17.4. Extract a custom assertion

17.5. Test a legacy method with no return value

17.6. Test a private method if you must

Appendix A: Complete solutions

A.1. Define a test suite in XML

A.2. Parameterized Test Case overriding runTest()

A.3. Ignore the order of elements in an XML document

A.4. Test an XSL stylesheet in isolation

A.5. Validate XML documents in your tests

A.6. Aspect-based universal Spy

A.7. Test a BMP entity bean

Appendix B: Essays on testing

B.1. Too simple to break

B.2. Strangeness and transitivity

B.3. Isolate expensive tests

B.4. The mock objects landscape

Appendix C: Reading List



What's inside

  • Getting started with JUnit
  • Recipes for
    • servlets
    • JSPs
    • EJBs
    • Database code
    • much more
  • Difficult-to-test designs, and how to fix them
  • How testing saves time
  • Choose a JUnit extension:
    • HTMLUnit
    • XMLUnit
    • ServletUnit
    • EasyMock
    • and more!

About the authors

J. B. Rainsberger is a developer and consultant who has been a leader in the JUnit community since 2001. His popular online tutorial JUnit: A Starter Guide is read by thousands of new JUnit users each month. Joe lives in Toronto, Canada.

Scott Stirling is a Senior Software Engineer on the Platform and Tools team at Workscape, Inc. in Framingham, MA. He has been active in the JUnit community since 2000 and has contributed code to the Jakarta Ant <junit> task.

placing your order...

Don't refresh or navigate away from the page.
print book $24.99 $49.95 pBook + eBook + liveBook
Additional shipping charges may apply
JUnit Recipes (print book) added to cart
continue shopping
go to cart

eBook $39.99 3 formats + liveBook
JUnit Recipes (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.
customers also reading

This book

FREE domestic shipping on three or more pBooks