1. Trang chủ
  2. » Công Nghệ Thông Tin

junit recipes, 2005

754 1,4K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 754
Dung lượng 7,7 MB

Nội dung

Internal Use Only – Do Not Distribute Stamp J. B.Rainsberger with contributions by Scott Stirling MANNING JUnit Recipes Practical Methods for Programmer Testing TEAMFLY Team-Fly ® JUnit Recipes JUnit Recipes Practical Methods for Programmer Testing J.B. RAINSBERGER with contributions by SCOTT STIRLING MANNING Greenwich (74° w. long.) Licensed to Wong Wing Kee <simonwg@gmail.com> 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: manning@manning.com ©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 12345678910–VHG–07 06 05 04 Licensed to Wong Wing Kee <simonwg@gmail.com> v brief contents PART 1 THE BUILDING BLOCKS 1 1 ■ Fundamentals 3 2 ■ Elementary tests 22 3 ■ Organizing and building JUnit tests 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 Licensed to Wong Wing Kee <simonwg@gmail.com> 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 Licensed to Wong Wing Kee <simonwg@gmail.com> vii contents foreword xv preface xvii acknowledgments xix about this book xxii about the cover illustration xxx PART 1THE 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 Licensed to Wong Wing Kee <simonwg@gmail.com> 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 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 Licensed to Wong Wing Kee <simonwg@gmail.com> [...]... Reporting JUnit results 7.1 7.2 7.3 7.4 7.5 136 177 188 Using a Base Test Case with a logger 190 Using Log4Unit 194 Getting plain text results with Ant 198 Reporting results in HTML with Ant’s task 202 Customizing XML reports with XSLT Licensed to Wong Wing Kee 205 ix CONTENTS 7.6 7.7 7.8 Troubleshooting JUnit 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 PART 2 233 JUnit cannot... tests 235 JUnit does not execute your custom test suite 237 JUnit does not set up your test fixture 239 Test setup fails after overriding runTest() 241 Your test stops after the first assertion fails 244 The graphical test runner does not load your classes properly 250 JUnit fails when your test case uses JAXP 252 JUnit fails when narrowing an EJB reference 253 AM FL Y 8 Extending Ant’s JUnit results... specialized JUnit testing packages in part 3, “More Testing Techniques.” Finally, no single volume can cover every conceivable way to use JUnit, so there is more you can do with JUnit than what is included here Kent Beck once said of JUnit that his goal was to create a framework that did what everyone would need it to do without piling on features that only some people would need He wanted us to think, JUnit. .. Ant, as well as hooking into the JUnit framework itself We conclude part 1 with chapter 8, “Troubleshooting JUnit. ” This chapter contains recipes for solving problems you may have using JUnit itself Many of these problems are common to first-time JUnit users, including configuration errors and typos Some are problems you encounter for the first time as you begin to use JUnit to test more complex systems,... in about 100 hours, including a suite of over 125 tests That was enough for me—I was hooked on JUnit Since that time I have involved myself in the JUnit community, not only as a practitioner of Test-Driven Development, but also by answering questions at the JUnit Yahoo! group Over the years I have refined my JUnit technique to the point where I am certain I could write that same component in closer to... everywhere with custom extensions to JUnit, and we provide some recipes that may help you start on your way to your own custom JUnit project The more, the merrier How this book is organized The first part of this book contains recipes devoted to the building blocks of writing tests with JUnit If we have done our job well as authors, then every test you write with JUnit will be reducible to some collection... answers I have provided to questions on the JUnit mailing lists Still more came from hard-won personal experience using JUnit on various projects I wanted this book to describe how I use JUnit; I did not want it to present some idealized view of how one ought to use it There’s already too much opinion and waving of hands out there—but not in here This book contains JUnit techniques that work, because they... chapters discussing two popular extensions to JUnit Chapter 15 provides recipes related to the open source project GSBase (http://gsbase sourceforge.net) This product includes some utilities to make it easier to write JUnit tests, as well as some test fixtures you can use directly in your projects Chapter 16 discusses another open source project, JUnit- addons (http:/ /junit- addons sourceforge.net), which includes... tests As Test-Driven Development practitioners, we have a tendency to write about JUnit exclusively as a tool for writing Object Tests Because much of the JUnit community intersects with the TDD community, this seems like a reasonable thing to do; however, you may not be a TDD practitioner Your current project may use JUnit, but only to write tests for existing code or to write tests at a higher-level... FOREWORD The book you are currently holding is a remarkable compendium of recipes written for those of us who use JUnit in our daily work This is not another book on TDD, nor is it a basic tutorial on JUnit Instead, this book is a suite of techniques— both simple and advanced—for using JUnit in a real, professional, environment Have you ever wondered how to test a servlet, or an XSLT script, or an . Organizing and building JUnit tests 71 4 ■ Managing test suites 102 5 ■ Working with test data 136 6 ■ Running JUnit tests 173 7 ■ Reporting JUnit results 188 8. 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

Ngày đăng: 20/03/2014, 15:40