JUnit Recipes: Practical Methods for Programmer Testing

J B Rainsberger

  • 出版商: Manning
  • 出版日期: 2004-07-15
  • 售價: $1,890
  • 貴賓價: 9.5$1,796
  • 語言: 英文
  • 頁數: 721
  • 裝訂: Paperback
  • ISBN: 1932394230
  • ISBN-13: 9781932394238
  • 相關分類: Java 相關技術JUnit
  • 已過版

買這商品的人也買了...

相關主題

商品描述

Description:

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
    • 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!


 

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 <sql> 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 <junitreport> task 202
7.5 Customizing <junit> 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
Introduction
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
Appendix 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
Appendix 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
Appendix C Reading List 696
 
references 700
index 705