Gift Guide

JUnit Recipes: Practical Methods for Programmer Testing


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...

See more details below
Other sellers (Paperback)
  • All (18) from $3.65   
  • New (5) from $10.78   
  • Used (13) from $3.63   
Sending request ...


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.
What's Inside:
- Getting started with JUnit
- Recipes for:
Database code much more
- Difficult-to-test designs, and how to fix them
- How testing saves time
- Choose a JUnit extension:
EasyMock and more!

Read More Show Less

Product Details

  • ISBN-13: 9781932394238
  • Publisher: O'Reilly Media, Incorporated
  • Publication date: 9/28/2004
  • Pages: 721
  • Product dimensions: 7.36 (w) x 9.22 (h) x 1.69 (d)

Meet the Author

Rainsberger is a deeloper and consultant who has been a leader in JUnit community since 2001.

Stirling has worked as a technical support and QA engineer for JRun since Allaire acquired the JRun product.

Read More Show Less

Table of Contents

Foreword xv
Preface xvii
Acknowledgments xix
About this book xxii
About the cover illustration xxx
Part 1 The building blocks 1
1 Fundamentals 3
1.1 What is Programmer Testing? 4
1.2 Getting started with JUnit 10
1.3 A few good practices 17
1.4 Summary 20
2 Elementary tests 22
2.1 Test your equals method 26
2.2 Test a method that returns nothing 33
2.3 Test a constructor 37
2.4 Test a getter 41
2.5 Test a setter 44
2.6 Test an interface 48
2.7 Test a JavaBean 54
2.8 Test throwing the right exception 56
2.9 Let collections compare themselves 61
2.10 Test a big object for equality 63
2.11 Test an object that instantiates other objects 66
3 Organizing and building JUnit tests 71
3.1 Place test classes in the same package as production code 74
3.2 Create a separate source tree for test code 77
3.3 Separate test packages from production code packages 79
3.4 Factor out a test fixture 83
3.5 Factor out a test fixture hierarchy 87
3.6 Introduce a Base Test Case 90
3.7 Move special case tests to a separate test fixture 92
3.8 Build tests from the command line 94
3.9 Build tests using Ant 96
3.10 Build tests using Eclipse 99
4 Managing test suites 102
4.1 Let JUnit build your test suite 103
4.2 Collect a specific set of tests 107
4.3 Collect all the tests in a package 111
4.4 Collect all the tests for your entire system 114
4.5 Scan the file system for tests 116
4.6 Separate the different kinds of test suites 120
4.7 Control the order of some of your tests 123
4.8 Build a data-driven test suite 127
4.9 Define a test suite in XML 133
5 Working with test data 136
5.1 Use Java system properties 138
5.2 Use environment variables 142
5.3 Use an inline data file 145
5.4 Use a properties file 147
5.5 Use ResourceBundles 152
5.6 Use a file-based test data repository 154
5.7 Use XML to describe test data 156
5.8 Use Ant's [left angle bracket]sql[right angle bracket] task to work with a database 157
5.9 Use JUnitPP 159
5.10 Set up your fixture once for the entire suite 161
5.11 Perform environment setup once for multiple test runs 164
5.12 Use DbUnit 170
6 Running JUnit tests 173
6.1 See the name of each test as it executes 177
6.2 See the name of each test as it executes with a text-based test runner 178
6.3 Execute a single test 180
6.4 Execute each test in its own JVM 181
6.5 Reload classes before each test 182
6.6 Ignore a test 185
7 Reporting JUnit results 188
7.1 Using a Base Test Case with a logger 190
7.2 Using Log4Unit 194
7.3 Getting plain text results with Ant 198
7.4 Reporting results in HTML with Ant's [left angle bracket]junitreport[right angle bracket] task 202
7.5 Customizing [left angle bracket]junit[right angle bracket] XML reports with XSLT 205
7.6 Extending Ant's JUnit results format 208
7.7 Implementing TestListener and extending TestRunner 215
7.8 Reporting a count of assertions 224
8 Troubleshooting JUnit 233
8.1 JUnit cannot find your tests 235
8.2 JUnit does not execute your custom test suite 237
8.3 JUnit does not set up your test fixture 239
8.4 Test setup fails after overriding runTest() 241
8.5 Your test stops after the first assertion fails 244
8.6 The graphical test runner does not load your classes properly 250
8.7 JUnit fails when your test case uses JAXP 252
8.8 JUnit fails when narrowing an EJB reference 253
Part 2 Testing J2EE 257
Designing J2EE applications for testability 259
The Coffee Shop application 263
9 Testing and XML 265
9.1 Verify the order of elements in a document 273
9.2 Ignore the order of elements in an XML document 277
9.3 Ignore certain differences in XML documents 281
9.4 Get a more detailed failure message from XMLUnit 288
9.5 Test the content of a static web page 290
9.6 Test an XSL stylesheet in isolation 297
9.7 Validate XML documents in your tests 302
10 Testing and JDBC 308
10.1 Test making domain objects from a ResultSet 317
10.2 Verify your SQL commands 322
10.3 Test your database schema 327
10.4 Verify your tests clean up JDBC resources 335
10.5 Verify your production code cleans up JDBC resources 343
10.6 Manage external data in your test fixture 346
10.7 Manage test data in a shared database 349
10.8 Test permissions when deploying schema objects 352
10.9 Test legacy JDBC code without the database 357
10.10 Test legacy JDBC code with the database 360
10.11 Use schema-qualified tables with DbUnit 363
10.12 Test stored procedures 366
11 Testing Enterprise JavaBeans 370
11.1 Test a session bean method outside the container 378
11.2 Test a legacy session bean 387
11.3 Test a session bean method in a real container 394
11.4 Test a CMP entity bean 397
11.5 Test CMP meta data outside the container 400
11.6 Test a BMP entity bean 408
11.7 Test a message-driven bean inside the container 414
11.8 Test a message-driven bean outside the container 420
11.9 Test a legacy message-driven bean 422
11.10 Test a JMS message consumer without the messaging server 426
11.11 Test JMS message-processing logic 430
11.12 Test a JMS message producer 433
11.13 Test the content of your JNDI directory 439
12 Testing web components 443
12.1 Test updating session data without a container 446
12.2 Test updating the HTTP session object 452
12.3 Test rendering a JavaServer Page 456
12.4 Test rendering a Velocity template 465
12.5 Test a JSP tag handler 468
12.6 Test your JSP tag library deployment 474
12.7 Test servlet initialization 477
12.8 Test the ServletContext 480
12.9 Test processing a request 483
12.10 Verify web page content without a web server 491
12.11 Verify web form attributes 494
12.12 Verify the data passed to a page template 495
12.13 Test a web resource filter 500
13 Testing J2EE applications 508
13.1 Test page flow 510
13.2 Test navigation rules in a Struts application 519
13.3 Test your site for broken links 522
13.4 Test web resource security 525
13.5 Test EJB resource security 530
13.6 Test container-managed transactions 536
Part 3 More JUnit techniques 541
14 Testing design patterns 543
14.1 Test an Observer (Event Listener) 545
14.2 Test an Observable (Event Source) 550
14.3 Test a Singleton 556
14.4 Test a Singleton's client 559
14.5 Test an object factory 562
14.6 Test a template method's implementation 566
15 GSBase 572
15.1 Verify events with EventCatcher 574
15.2 Test serialization 577
15.3 Test object cloning 579
15.4 Compare JavaBeans using "appears equal" 581
16 JUnit-addons 585
16.1 Test your class for compareTo() 587
16.2 Collect tests automatically from an archive 590
16.3 Organize test data using PropertyManager 591
16.4 Manage shared test resources 593
16.5 Ensure your shared test fixture tears itself down 597
16.6 Report the name of each test as it executes 599
17 Odds and ends 603
17.1 Clean up the file system between tests 605
17.2 Test your file-based application without the file system 608
17.3 Verify your test case class syntax 614
17.4 Extract a custom assertion 617
17.5 Test a legacy method with no return value 620
17.6 Test a private method if you must 625
A Complete solutions 629
A.1 Define a test suite in XML 630
A.2 Parameterized Test Case overriding runTest() 634
A.3 Ignore the order of elements in an XML document 637
A.4 Test an XSL stylesheet in isolation 639
A.5 Validate XML documents in your tests 645
A.6 Aspect-based universal Spy 649
A.7 Test a BMP entity bean 653
B Essays on testing 673
B.1 Too simple to break 674
B.2 Strangeness and transitivity 677
B.3 Isolate expensive tests 681
B.4 The mock objects landscape 689
C Reading List 696
References 700
Index 705
Read More Show Less

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing 1 Customer Reviews
  • Anonymous

    Posted August 2, 2004

    A wonderful compendium of JUnit tips and tricks

    J. B. Rainsberger¿s JUnit Recipes is a wonderful compendium of tips and tricks that can quickly take anyone from novice to expert at JUnit. The organization of the book should make it appealing to unit-testing programmers of all levels. Early chapters are highly introductory, covering the installation and first uses of JUnit. Later chapters cover testing of JDBC, Enterprise Java Beans, XML, and more. JUnit Recipes includes the best discussions I¿ve read on how to test database applications and on the complicated art of managing test data. This is probably not a book you will read every chapter of. In my programming, for example, I don¿t use EJB so I only skimmed that chapter. But at over 700 pages is much more of an encyclopedia of wonderful testing techniques than a book that is meant to be read cover to cover. As its title implies, JUnit Recipes is a cookbook of ideas that will allow you to serve up better, and better-tested, applications.

    Was this review helpful? Yes  No   Report this review
Sort by: Showing 1 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)