Table Of ContentCovers JUnit 4.8
Petar Tahchiev
Felipe Leme
Vincent Massol
Gary Gregory
IN ACTION
SECOND EDITION
M A N N I N G
Praise for the First Edition
The definitive how-to manual for unit testing Java EE components. Pick up one of the
other books if you’re looking for something more motivational, but when you’re ready
to sit down and bang out some code, you’ll want this book at your side.
—JavaRanch.com
I would definitely recommend JUnit in Action for anyone interested in testing their
code.… It is a book that flows nicely, and offers a great mix of technology theory and
how to put it all into practice.
—TheServerSide.com
An essential guide for intermediate level Java programmers who want to learn how
to build Java EE applications properly...clear, simple and fun...the best I have seen
thus far…. The book actually goes into detail about using mock objects and stubs,
further expanding your understanding of basic software design…. I highly recom-
mend it.
—Killersites.com
Not a JUnit tutorial, it covers JUnit in depth. It also explains the importance of JUnit
in the context of software development process. This well-edited book is highly recom-
mended both for the beginner and advanced users of JUnit.
—Denver JUG
With a number of Manning books I can see myself start to think differently about
problems and so I end up being a better developer; JUnit in Action was like that for
me. At first it bothered me that I was changing my code in order to test it, but then I
started seeing that the changes made the code better overall. Now my code is littered
with factory methods and similar patterns. You guys are doing good stuff.
—Joshua Smith, a reader
The examples are clear and real-world. The authors address the complex issues of unit
testing EJBs and web apps head-on. They don’t shy away from the real issues that
come with testing these kinds of applications.
—Wade Matveyenko, a reader
JUnit in Action
SECOND EDITION
PETAR TAHCHIEV
FELIPE LEME
VINCENT MASSOL
GARY GREGORY
MANNING
Greenwich
(74° w. long.)
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.
180 Broad St.
Suite 1323
Stamford, CT 06901
Email: [email protected]
©2011 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 we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
Development Editor: Sebastian Stirling
Manning Publications Co. Copyeditor: Linda Recktenwald
180 Broad St. Proofreader: Katie Tennant
Suite 1323 Typesetters: Dennis Dalinnik
Stamford, CT 06901 Cover designer: Marija Tudor
ISBN 9781935182023
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 16 15 14 13 12 11 10
To my sister Hrissy; you showed me what real courage means
—P.T.
To the memory of my father, Leonidas de Almeida Leme
—F.L.
To my wife, Marie-Albane, and my children,
Paul, Jean, and Pierre-Olivier
—V.M.
To my loving family: my wife Lori, my son Alexander,
my mother Micheline, and to the memory of my father Greg
—G.G.
brief contents
PART 1 JUNIT ESSENTIALS........................................................ 1
1 ■ JUnit jump-start 3
2 ■ Exploring core JUnit 14
3 ■ Mastering JUnit 25
4 ■ Software testing principles 53
PART 2 DIFFERENT TESTING STRATEGIES ................................ 65
5 ■ Test coverage and development 67
6 ■ Coarse-grained testing with stubs 84
7 ■ Testing with mock objects 99
8 ■ In-container testing 126
PART 3 JUNIT AND THE BUILD PROCESS.................................135
9 ■ Running JUnit tests from Ant 137
10 ■ Running JUnit tests from Maven2 152
11 ■ Continuous integration tools 169
vii
viii BRIEF CONTENTS
PART 4 JUNIT EXTENSIONS................................................... 187
12 ■ Presentation-layer testing 189
13 ■ Ajax testing 224
14 ■ Server-side Java testing with Cactus 259
15 ■ Testing JSF applications 292
16 ■ Testing OSGi components 310
17 ■ Testing database access 326
18 ■ Testing JPA-based applications 360
19 ■ JUnit on steroids 389
contents
preface xix
preface to the first edition xxi
acknowledgments xxiii
about this book xxv
about the authors xxx
about the cover illustration xxxii
PART I JUNIT ESSENTIALS ............................................ 1
1 JUnit jump-start 3
1.1 Proving it works 4
1.2 Starting from scratch 6
1.3 Understanding unit testing frameworks 8
1.4 JUnit design goals 9
1.5 Setting up JUnit 9
1.6 Testing with JUnit 10
1.7 Summary 13
2 Exploring core JUnit 14
2.1 Exploring core JUnit 15
ix