JUnit Recipes: Practical Methods for Programmer Testing
暫譯: JUnit 食譜:程式設計師測試的實用方法

J B Rainsberger

  • 出版商: Manning
  • 出版日期: 2004-07-15
  • 售價: $1,930
  • 貴賓價: 9.5$1,834
  • 語言: 英文
  • 頁數: 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

商品描述(中文翻譯)

描述:
當測試成為開發者的習慣時,好的事情往往會發生——良好的生產力、優質的程式碼和高滿意度的工作。如果你想要這些,沒有比《JUnit Recipes》更好的方式來開始你的測試習慣,或持續滋養它。在這本書中,你將找到一百三十七個針對各種問題的解決方案,從簡單到複雜,這些解決方案是由一位經驗豐富的開發者和測試大師為你精選的。每個食譜都遵循相同的組織結構,先給出問題及其背景,然後討論解決問題的選項。

JUnit——Java的單元測試框架——使用簡單,但某些程式碼可能難以測試。當你面對這樣的程式碼時,你會很高興擁有這本書。這是一本充滿實用建議的參考書,涵蓋了測試的所有問題,從如何命名你的測試案例類到如何測試複雜的J2EE應用程式。它的寶貴建議包括一些可能帶來巨大回報的附帶事項,例如如何組織你的測試數據或如何管理昂貴的測試資源。

內部內容:
- 開始使用JUnit
- 食譜包括:
- servlets
- JSPs
- EJBs
- 數據庫程式碼
- 更多內容
- 難以測試的設計及其修正方法
- 測試如何節省時間
- 選擇JUnit擴展:
- HTMLUnit
- XMLUnit
- ServletUnit
- EasyMock
- 更多!

目錄:
前言 xv
序言 xvii
致謝 xix
關於本書 xxii
關於封面插圖 xxx
第一部分 基礎構件 1
1 基礎知識 3
1.1 什麼是程式設計師測試? 4
1.2 開始使用JUnit 10
1.3 一些良好的實踐 17
1.4 總結 20
2 基本測試 22
2.1 測試你的equals方法 26
2.2 測試一個不返回任何東西的方法 33
2.3 測試一個構造函數 37
2.4 測試一個getter 41
2.5 測試一個setter 44
2.6 測試一個介面 48
2.7 測試一個JavaBean 54
2.8 測試拋出正確的異常 56
2.9 讓集合自我比較 61
2.10 測試一個大對象的相等性 63
2.11 測試一個實例化其他對象的對象 66
3 組織和構建JUnit測試 71
3.1 將測試類放在與生產代碼相同的包中 74
3.2 為測試代碼創建一個單獨的源樹 77
3.3 將測試包與生產代碼包分開 79
3.4 提取測試設置 83
3.5 提取測試設置層次結構 87
3.6 引入基礎測試案例 90
3.7 將特殊案例測試移至單獨的測試設置 92
3.8 從命令行構建測試 94
3.9 使用Ant構建測試 96
3.10 使用Eclipse構建測試 99
4 管理測試套件 102
4.1 讓JUnit構建你的測試套件 103
4.2 收集特定的測試集 107
4.3 收集包中的所有測試 111
4.4 收集整個系統的所有測試 114
4.5 掃描文件系統以查找測試 116
4.6 分離不同類型的測試套件 120
4.7 控制某些測試的順序 123
4.8 構建數據驅動的測試套件 127
4.9 在XML中定義測試套件 133
5 使用測試數據 136
5.1 使用Java系統屬性 138
5.2 使用環境變量 142
5.3 使用內聯數據文件 145
5.4 使用屬性文件 147
5.5 使用ResourceBundles 152
5.6 使用基於文件的測試數據庫 154
5.7 使用XML描述測試數據 156
5.8 使用Ant的任務操作數據庫 157
5.9 使用JUnitPP 159
5.10 為整個套件設置一次測試設置 161
5.11 為多次測試運行執行一次環境設置 164
5.12 使用DbUnit 170
6 執行JUnit測試 173
6.1 在執行時查看每個測試的名稱 177
6.2 使用基於文本的測試運行器查看每個測試的名稱 178
6.3 執行單個測試 180
6.4 在自己的JVM中執行每個測試 181
6.5 在每個測試之前重新加載類 182
6.6 忽略一個測試 185
7 報告JUnit結果 188
7.1 使用帶有日誌記錄器的基礎測試案例 190
7.2 使用Log4Unit 194
7.3 使用Ant獲取純文本結果 198
7.4 使用Ant的任務報告結果 202
7.5 使用XSLT自定義 XML報告 205
7.6 擴展Ant的JUnit結果格式 208
7.7 實現TestListener並擴展TestRunner 215
7.8 報告斷言的計數 224
8 故障排除JUnit 233
8.1 JUnit無法找到你的測試 235
8.2 JUnit未執行你的自定義測試套件 237
8.3 JUnit未設置你的測試設置 239
8.4 在覆蓋runTest()後測試設置失敗 241
8.5 你的測試在第一次斷言失敗後停止 244
8.6 圖形測試運行器未正確加載你的類 250
8.7 當你的測試案例使用JAXP時,JUnit失敗 252
8.8 當縮小EJB引用時,JUnit失敗 253
第二部分 測試J2EE 257
介紹
設計可測試的J2EE應用程式 259
咖啡店應用程式 263
9 測試和XML 265
9.1 驗證文檔中元素的順序 273
9.2 忽略XML文檔中元素的順序 277
9.3 忽略XML文檔中的某些差異 281
9.4 從XMLUnit獲取更詳細的失敗消息 288
9.5 測試靜態網頁的內容 290
9.6 獨立測試XSL樣式表 297
9.7 在測試中驗證XML文檔 302
10 測試和JDBC 308
10.1 測試從ResultSet創建域對象 317
10.2 驗證你的SQL命令 322
10.3 測試你的數據庫架構 327
10.4 驗證你的測試清理JDBC資源 335
10.5 驗證你的生產代碼清理JDBC資源 343
10.6 在你的測試設置中管理外部數據 346
10.7 在共享數據庫中管理測試數據 349
10.8 測試部署架構對象時的權限 352
10.9 在沒有數據庫的情況下測試遺留JDBC代碼 357
10.10 在有數據庫的情況下測試遺留JDBC代碼 360
10.11 使用DbUnit測試架構限定的表 363
10.12 測試存儲過程 366
11 測試企業JavaBeans 370
11.1 在容器外測試會話Bean方法 378
11.2 測試遺留會話Bean 387
11.3 在真實容器中測試會話Bean方法 394
11.4 測試CMP實體Bean 397
11.5 在容器外測試CMP元數據 400
11.6 測試BMP實體Bean 408
11.7 在容器內測試消息驅動Bean 414
11.8 在容器外測試消息驅動Bean 420
11.9 測試遺留消息驅動Bean 422
11.10 在沒有消息伺服器的情況下測試JMS消息消費者 426
11.11 測試JMS消息處理邏輯 430
11.12 測試JMS消息生產者 433
11.13 測試你的JNDI目錄內容 439
12 測試Web組件 443
12.1 在沒有容器的情況下測試更新會話數據 446
12.2 測試更新HTTP會話對象 452
12.3 測試渲染JavaServer頁面 456
12.4 測試渲染Velocity模板 465
12.5 測試JSP標籤處理器 468
12.6 測試你的JSP標籤庫部署 474
12.7 測試servlet初始化 477
12.8 測試ServletContext 480
12.9 測試處理請求 483
12.10 在沒有Web伺服器的情況下驗證網頁內容 491
12.11 驗證網頁表單屬性 494
12.12 驗證傳遞給頁面模板的數據 495
12.13 測試Web資源過濾器 500
13 測試J2EE應用程式 508
13.1 測試頁面流程 510
13.2 測試Struts應用程式中的導航規則 519
13.3 測試你的網站是否有斷鏈 522
13.4 測試Web資源安全性 525
13.5 測試EJB資源安全性 530
13.6 測試容器管理的事務 536
第三部分 更多JUnit技術 541
14 測試設計模式 543
14.1 測試觀察者(事件監聽器) 545
14.2 測試可觀察者(事件源) 550
14.3 測試單例 556
14.4 測試單例的客戶端 559
14.5 測試對象工廠 562
14.6 測試模板方法的實現 566
15 GSBase 572
15.1 使用EventCatcher驗證事件 574
15.2 測試序列化 577
15.3 測試對象克隆 579
15.4 使用“看起來相等”比較JavaBeans 581
16 JUnit附加功能 585
16.1 測試你的類的compareTo() 587
16.2 從存檔自動收集測試 590
16.3 使用PropertyManager組織測試數據 591
16.4 管理共享測試資源 593
16.5 確保你的共享測試設置能夠自我清理 597
16.6 在執行時報告每個測試的名稱 599
17 雜項 603
17.1 在測試之間清理文件系統 605
17.2 在沒有文件系統的情況下測試你的基於文件的應用程式 608
17.3 驗證你的測試案例類語法 614
17.4 提取自定義斷言 617
17.5 測試沒有返回值的遺留方法 620
17.6 如果必須,測試私有方法 625
附錄A 完整解決方案 629
A.1 在XML中定義測試套件 630
A.2 參數化測試案例覆蓋runTest() 634
A.3 忽略XML文檔中元素的順序 637
A.4 獨立測試XSL樣式表 639
A.5 在測試中驗證XML文檔 645
A.6 基於方面的通用Spy 649
A.7 測試BMP實體Bean 653
附錄B 測試隨筆 673
B.1 太簡單而無法破壞 674
B.2 奇異性和傳遞性 677
B.3 隔離昂貴的測試 681
B.4 模擬對象的景觀 689
附錄C 讀書清單 696
參考文獻 700
索引 705