ebook img

JUnit recipes : practical methods for programmer testing PDF

752 Pages·2005·14.496 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview JUnit recipes : practical methods for programmer testing

JUnit Recipes TEAM LinG - Live, Informative, Non-cost and Genuine! TEAM LinG - Live, Informative, Non-cost and Genuine! JUnit Recipes Practical Methods for Programmer Testing J.B. RAINSBERGER with contributions by SCOTT STIRLING MANNING Greenwich (74° w. long.) TEAM LinG - Live, Informative, Non-cost and Genuine! To my mother, Joan. I wish I had finished this in time. For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: [email protected] ©2005 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Mark Goodin 209 Bruce Park Avenue Typesetter: Martine Maguire-Weltecke Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1932394230 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 07 06 05 04 TEAM LinG - Live, Informative, Non-cost and Genuine! brief contents PART 1 THE BUILDING BLOCKS .......................................1 1 ■ Fundamentals 3 2 ■ Elementary tests 22 3 ■ Organizing and buildingJUnittests 71 4 ■ Managing test suites 102 5 ■ Working with test data 136 6 ■ Running JUnit tests 173 7 ■ Reporting JUnit results 188 8 ■ Troubleshooting JUnit 233 PART 2 TESTING J2EE ................................................. 257 9 ■ Testing and XML 265 10 ■ Testing and JDBC 308 11 ■ Testing Enterprise JavaBeans 370 12 ■ Testing web components 443 13 ■ Testing J2EE applications 508 v TEAM LinG - Live, Informative, Non-cost and Genuine! vi BRIEF CONTENTS PART 3 MORE JUNIT TECHNIQUES ............................ 541 14 ■ Testing design patterns 543 15 ■ GSBase 572 16 ■ JUnit-addons 585 17 ■ Odds and ends 603 APPENDICES ........................................................................ 629 A ■ Complete solutions 629 B ■ Essays on testing 673 C ■ Reading List 696 TEAM LinG - Live, Informative, Non-cost and Genuine! 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 ProgrammerTesting? 4 1.2 Getting started withJUnit 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 vii TEAM LinG - Live, Informative, Non-cost and Genuine! viii CONTENTS 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 packageasproductioncode 74 3.2 Create a separate sourcetreefortestcode 77 3.3 Separate test packages fromproductioncode 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 toaseparatetestfixture 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 TEAM LinG - Live, Informative, Non-cost and Genuine! CONTENTS ix 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 toworkwithadatabase 157 5.9 Use JUnitPP 159 5.10 Set up your fixture once fortheentiresuite 161 5.11 Perform environment setup once formultiple test runs 164 5.12 Use DbUnit 170 6 Running JUnit tests 173 6.1 See the name of each test asitexecutes 177 6.2 See the name of each test asitexecutes 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 withAnt’s<junitreport>task 202 7.5 Customizing <junit> XML reportswithXSLT 205 TEAM LinG - Live, Informative, Non-cost and Genuine!

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.