Table of Contents

Copyright

Brief Table of Contents

Table of Contents

Foreword

Preface

Acknowledgments

About this Book

About the Cover Illustration

1. The building blocks

Chapter 1. Fundamentals

1.1. What is Programmer Testing?

1.1.1. The goal of Object Testing

1.1.2. The rhythm of an Object Test

1.1.3. A framework for unit testing

1.1.4. Enter JUnit

1.1.5. Understanding Test-Driven Development

1.2. Getting started with JUnit

1.2.1. Downloading and installing JUnit

1.2.2. Writing a simple test

1.2.3. Understanding the TestCase class

1.2.4. Failure messages

1.2.5. How JUnit signals a failed assertion

1.2.6. The difference between failures and errors

1.3. A few good practices

1.3.1. Naming conventions for tests and test classes

1.3.2. Test behavior, not methods

1.4. Summary

Chapter 2. Elementary tests

2.1. Test your equals method

Problem

Background

Recipe

Discussion

Related

2.2. Test a method that returns nothing

Problem

Background

Recipe

Discussion

Related

2.3. Test a constructor

Problem

Background

Recipe

Discussion

Related

2.4. Test a getter

Problem

Background

Recipe

Discussion

Related

2.5. Test a setter

Problem

Background

Recipe

Discussion

Related

2.6. Test an interface

Problem

Background

Recipe

Discussion

Related

2.7. Test a JavaBean

Problem

Background

Recipe

Discussion

Related

2.8. Test throwing the right exception

Problem

Background

Recipe

Discussion

Related

2.9. Let collections compare themselves

Problem

Background

Recipe

Discussion

Related

2.10. Test a big object for equality

Problem

Background

Recipe

Discussion

Related

2.11. Test an object that instantiates other objects

Problem

Background

Recipe

Discussion

Related

Chapter 3. Organizing and building JUnit tests

A place to start

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

Problem

Background

Recipe

Discussion

Related

3.2. Create a separate source tree for test code

Problem

Background

Recipe

Discussion

Related

3.3. Separate test packages from production code packages

Problem

Background

Recipe

Discussion

Related

3.4. Factor out a test fixture

Problem

Background

Recipe

Discussion

Related

3.5. Factor out a test fixture hierarchy

Problem

Background

Recipe

Discussion

Related

3.6. Introduce a Base Test Case

Problem

Background

Recipe

Discussion

Related

3.7. Move special case tests to a separate test fixture

Problem

Background

Recipe

Discussion

Related

3.8. Build tests from the command line

Problem

Background

Recipe

Discussion

Related

3.9. Build tests using Ant

Problem

Background

Recipe

Discussion

Related

3.10. Build tests using Eclipse

Problem

Background

Recipe

Discussion

Related

Chapter 4. Managing test suites

4.1. Let JUnit build your test suite

Problem

Background

Recipe

Discussion

Related

4.2. Collect a specific set of tests

Problem

Background

Recipe

Discussion

Related

4.3. Collect all the tests in a package

Problem

Background

Recipe

Discussion

Related

4.4. Collect all the tests for your entire system

Problem

Background

Recipe

Discussion

Related

4.5. Scan the file system for tests

Problem

Background

Recipe

Discussion

Related

4.6. Separate the different kinds of test suites

Problem

Background

Recipe

Discussion

Related

4.7. Control the order of some of your tests

Problem

Background

Recipe

Discussion

Related

4.8. Build a data-driven test suite

Problem

Background

Recipe

Discussion

Related

4.9. Define a test suite in XML

Problem

Background

Recipe

Discussion

Postscript

Related

Chapter 5. Working with test data

5.1. Use Java system properties

Problem

Background

Recipe

Discussion

Related

5.2. Use environment variables

Problem

Background

Recipe

Discussion

Related

5.3. Use an inline data file

Problem

Background

Recipe

Discussion

Related

5.4. Use a properties file

Problem

Background

Recipe

Discussion

Related

5.5. Use ResourceBundles

Problem

Background

Recipe

Discussion

Related

5.6. Use a file-based test data repository

Problem

Background

Recipe

Discussion

Related

5.7. Use XML to describe test data

Problem

Background

Recipe

Discussion

Related

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

Problem

Background

Recipe

Discussion

Discussion

5.9. Use JUnitPP

Problem

Background

Recipe

Discussion

Related

5.10. Set up your fixture once for the entire suite

Problem

Background

Recipe

Discussion

Related

5.11. Perform environment setup once for multiple test runs

Problem

Background

Recipe

Discussion

Related

5.12. Use DbUnit

Problem

Background

Recipe

Discussion

Related

Chapter 6. Running JUnit tests

The basic test runners

Using Ant

JUnit-addons Test Runner

6.1. See the name of each test as it executes

Problem

Background

Recipe

Discussion

Related

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

Problem

Background

Recipe

Discussion

Related

6.3. Execute a single test

Problem

Background

Recipe

Discussion

Related

6.4. Execute each test in its own JVM

Problem

Background

Recipe

Discussion

Related

6.5. Reload classes before each test

Problem

Background

Recipe

Discussion

Related

6.6. Ignore a test

Problem

Background

Recipe

Discussion

Related

Chapter 7. Reporting JUnit results

7.1. Using a Base Test Case with a logger

Problem

Background

Recipe

Discussion

Related

7.2. Using Log4Unit

Problem

Background

Recipe

Discussion

Related

7.3. Getting plain text results with Ant

Problem

Background

Recipe

Discussion

Related

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

Problem

Background

Recipe

Discussion

Related

7.5. Customizing <junit> XML reports with XSLT

Problem

Background

Recipe

Discussion

Related

7.6. Extending Ant’s JUnit results format

Problem

Background

Recipe

Discussion

Related

7.7. Implementing TestListener and extending TestRunner

Problem

Background

Recipe

Discussion

Related

7.8. Reporting a count of assertions

Problem

Background

Recipe

Discussion

Related

Chapter 8. Troubleshooting JUnit

The most common problem

A problem with the CLASSPATH environment variable

8.1. JUnit cannot find your tests

Problem

Background

Recipe

Discussion

Related

8.2. JUnit does not execute your custom test suite

Problem

Background

Recipe

Discussion

Related

8.3. JUnit does not set up your test fixture

Problem

Background

Recipe

Discussion

Related

8.4. Test setup fails after overriding runTest()

Problem

Background

Recipe

Discussion

Related

8.5. Your test stops after the first assertion fails

Problem

Background

Recipe

Discussion

Related

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

Problem

Background

Recipe

Discussion

Related

8.7. JUnit fails when your test case uses JAXP

Problem

Background

Recipe

Discussion

Related

8.8. JUnit fails when narrowing an EJB reference

Problem

Background

Recipe

Discussion

Related

2. Testing J2EE

Chapter 9. Testing and XML

9.1. Verify the order of elements in a document

Problem

Background

Recipe

Discussion

Related

9.2. Ignore the order of elements in an XML document

Problem

Background

Recipe

Discussion

Related

9.3. Ignore certain differences in XML documents

Problem

Background

Recipe

Discussion

Related

9.4. Get a more detailed failure message from XMLUnit

Problem

Background

Recipe

Discussion

Related

9.5. Test the content of a static web page

Problem

Background

Discussion

Related

9.6. Test an XSL stylesheet in isolation

Problem

Background

Recipe

Discussion

Related

9.7. Validate XML documents in your tests

Problem

Background

Recipe

Discussion

Related

Chapter 10. Testing and JDBC

10.1. Test making domain objects from a ResultSet

Problem

Background

Recipe

Discussion

Related

10.2. Verify your SQL commands

Problem

Background

Recipe

Discussion

Post script

Related

10.3. Test your database schema

Problem

Background

Recipe

Discussion

Related

10.4. Verify your tests clean up JDBC resources

Problem

Background

Recipe

Discussion

Related

10.5. Verify your production code cleans up JDBC resources

Problem

Background

Recipe

Discussion

Related

10.6. Manage external data in your test fixture

Problem

Background

Recipe

Discussion

Related

10.7. Manage test data in a shared database

Problem

Background

Recipe

Discussion

Related

10.8. Test permissions when deploying schema objects

Problem

Background

Recipe

Discussion

Related

10.9. Test legacy JDBC code without the database

Problem

Background

Recipe

Discussion

Related

10.10. Test legacy JDBC code with the database

Problem

Background

Recipe

Discussion

Related

10.11. Use schema-qualified tables with DbUnit

Problem

Background

Recipe

Discussion

Related

10.12. Test stored procedures

Problem

Background

Recipe

Discussion

Related

Chapter 11. Testing Enterprise JavaBeans

Stateless session beans

Stateful session beans

Entity beans

Message-driven beans

When all else fails

11.1. Test a session bean method outside the container

Problem

Background

Recipe

Discussion

Related

11.2. Test a legacy session bean

Problem

Background

Recipe

Discussion

Related

11.3. Test a session bean method in a real container

Problem

Background

Recipe

Discussion

Related

11.4. Test a CMP entity bean

Problem

Background

Recipe

Discussion

Related

11.5. Test CMP meta data outside the container

Problem

Background

Recipe

Discussion

Post script

Related

11.6. Test a BMP entity bean

Problem

Background

Recipe

Discussion

Related

11.7. Test a message-driven bean inside the container

Problem

Background

Recipe

Discussion

Related

11.8. Test a message-driven bean outside the container

Problem

Background

Recipe

Discussion

Related

11.9. Test a legacy message-driven bean

Problem

Background

Recipe

Discussion

Related

11.10. Test a JMS message consumer without the messaging server

Problem

Background

Recipe

Discussion

Related

11.11. Test JMS message-processing logic

Problem

Background

Recipe

Discussion

Related

11.12. Test a JMS message producer

Problem

Background

Recipe

Discussion

Related

11.13. Test the content of your JNDI directory

Problem

Background

Recipe

Discussion

Related

Chapter 12. Testing web components

Test the components in a container

Simulate the container

Avoid the container

12.1. Test updating session data without a container

Problem

Background

Recipe

Discussion

Related

12.2. Test updating the HTTP session object

Recipe

Background

Recipe

Discussion

Related

12.3. Test rendering a JavaServer Page

Problem

Background

Recipe

Discussion

Related

12.4. Test rendering a Velocity template

Problem

Background

Recipe

Discussion

Related

12.5. Test a JSP tag handler

Problem

Background

Recipe

Discussion

Related

12.6. Test your JSP tag library deployment

Problem

Background

Recipe

Discussion

Related

12.7. Test servlet initialization

Problem

Background

Recipe

Discussion

Related

12.8. Test the ServletContext

Problem

Background

Recipe

Discussion

Related

12.9. Test processing a request

Problem

Background

Recipe

Discussion

Related

12.10. Verify web page content without a web server

Problem

Background

Recipe

Discussion

Related

12.11. Verify web form attributes

Problem

Background

Recipe

Discussion

Related

12.12. Verify the data passed to a page template

Problem

Background

Recipe

Discussion

Related

12.13. Test a web resource filter

Problem

Background

Recipe

Discussion

Related

Chapter 13. Testing J2EE applications

13.1. Test page flow

Problem

Background

Recipe

Discussion

Related

13.2. Test navigation rules in a Struts application

Problem

Background

Recipe

Discussion

Related

13.3. Test your site for broken links

Problem

Background

Recipe

Discussion

Related

13.4. Test web resource security

Problem

Background

Recipe

Discussion

Related

13.5. Test EJB resource security

Problem

Background

Recipe

Discussion

Related

13.6. Test container-managed transactions

Problem

Background

Recipe

Discussion

Related

3. More JUnit techniques

Chapter 14. Testing design patterns

14.1. Test an Observer (Event Listener)

Problem

Background

Recipe

Discussion

Related

14.2. Test an Observable (Event Source)

Problem

Background

Recipe

Discussion

Related

14.3. Test a Singleton

Problem

Background

Recipe

Discussion

Related

14.4. Test a Singleton’s client

Problem

Background

Recipe

Discussion

Related

14.5. Test an object factory

Problem

Background

Recipe

Discussion

Related

14.6. Test a template method’s implementation

Problem

Background

Recipe

Discussion

Related

Chapter 15. GSBase

15.1. Verify events with EventCatcher

Problem

Background

Recipe

Discussion

Related

15.2. Test serialization

Problem

Background

Recipe

Discussion

Related

15.3. Test object cloning

Problem

Background

Recipe

Discussion

Related

15.4. Compare JavaBeans using “appears equal”

Problem

Background

Recipe

Discussion

Related

Chapter 16. JUnit-addons

16.1. Test your class for compareTo()

Problem

Background

Recipe

Discussion

Related

16.2. Collect tests automatically from an archive

Problem

Background

Recipe

Discussion

Related

16.3. Organize test data using PropertyManager

Problem

Background

Recipe

Discussion

Related

16.4. Manage shared test resources

Problem

Background

Recipe

Discussion

Related

16.5. Ensure your shared test fixture tears itself down

Problem

Background

Recipe

Discussion

Related

16.6. Report the name of each test as it executes

Problem

Background

Recipe

Discussion

Related

Chapter 17. Odds and ends

17.1. Clean up the file system between tests

Problem

Background

Recipe

Discussion

Related

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

Problem

Background

Recipe

Discussion

Related

17.3. Verify your test case class syntax

Problem

Background

Recipe

Discussion

Related

17.4. Extract a custom assertion

Problem

Background

Recipe

Discussion

Related

17.5. Test a legacy method with no return value

Problem

Background

Recipe

Discussion

Related

17.6. Test a private method if you must

Problem

Background

Recipe

Discussion

Related

 Appendices

Appendix A. Complete solutions

A.1. Define a test suite in XML

Solution

A.2. Parameterized Test Case overriding runTest()

Solution

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

Solution

A.4. Test an XSL stylesheet in isolation

Solution

A.5. Validate XML documents in your tests

Solution

A.6. Aspect-based universal Spy

Solution

A.7. Test a BMP entity bean

Solution

Appendix B. Essays on testing

B.1. Too simple to break

The point

The details

Postscript

B.2. Strangeness and transitivity

The point

The details

B.3. Isolate expensive tests

The point

The details

B.4. The mock objects landscape

The point

The details

Appendix C. Reading List

Java Testing

General Testing

Java Programming

Enterprise Software

Agile Software Development and Extreme Programming

General Programming

Appendix References

Print Sources

Online Sources

Index

List of Figures

List of Tables

List of Listings

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset