pragmatic_unit_testing_in_java_with_junit.pdf

(1266 KB) Pobierz
1154792562.006.png
What readers are saying about
Pragmatic Unit Testing . . .
This book starts out with a nice introduction discussing
what unit testing is as well as why we should do it. I like the
anecdotes peppered throughout the book illustrating the
point of why one should bother. . . . I also really liked the
analogies you use. It puts the code into a real-world context.
Sharee L. Johnson,
Project Lead, Applications Development
I wish I had a copy back when I started doing test-rst
development as part of Extreme Programming.
Al Koscielny, Software Developer
I'm not totally new to testing, but I've struggled with many
aspects of it. I think this book does a good job of bringing
those along who are completely new to unit testing, but still
has enough advanced material to assist those of us who have
dabbled in testing and oundered once we've hit obstacles.
Andrew Thompson,
Consultant, Greenbrier & Russel
When I'm on a project that needs to be doing unit testing
better (which is often the case), I'd like to have this book
available as a simple reference to suggest to the team.
Bobby Woolf, Consulting I/T Specialist,
IBM Software Services for Websphere
I am a rm believer in unit testing and I would want all
team members I work with to be religiously practicing the
techniques recommended in this book. I think there is a lot
of good, practical information in this book that any
professional software engineer should be incorporating into
their daily work.
James J. O'Connor III,
Lead System Design Engineer
1154792562.007.png 1154792562.008.png 1154792562.009.png 1154792562.001.png 1154792562.002.png
Pragmatic Unit Testing
in Java with JUnit
Andy Hunt
Dave Thomas
The Pragmatic Bookshelf
Raleigh, North Carolina
Dallas, Texas
1154792562.003.png 1154792562.004.png
Many of the designations used by manufacturers and sellers to distinguish
their products are claimed as trademarks. Where those designations appear
in this book, and The Pragmatic Programmers, LLC was aware of a trademark
claim, the designations have been printed in initial capital letters or in all
capitals.
Every precaution was taken in the preparation of this book. However, the
publisher assumes no responsibility for errors or omissions, or for damages
that may result from the use of information (including program listings) con-
tained herein.
For information on the latest Pragmatic titles, visit us online:
Copyright c
2003 The Pragmatic Programmers, LLC. All rights reserved. No
part of this publication may be reproduced, stored in a retrieval system, or
transmitted, in any form, or by any means, electronic, mechanical, photo-
copying, recording, or otherwise, without the prior consent of the publisher.
Printed in the United States of America.
ISBN 0-9745140-1-2
Text printed on acid-free paper.
First printing, September 2003
Version: 2004-1-4
Contents
viii
x
1
Introduction
1
1.1
Coding With Condence . . . . . . . . . . . . . .
2
1.2
What is Unit Testing? . . . . . . . . . . . . . . .
3
1.3
Why Should I Bother with Unit Testing?
. . . .
4
1.4
What Do I Want to Accomplish?
. . . . . . . . .
5
1.5
How Do I Do Unit Testing?
. . . . . . . . . . . .
7
1.6
Excuses For Not Testing . . . . . . . . . . . . . .
7
1.7
Roadmap
. . . . . . . . . . . . . . . . . . . . . .
12
2
Your First Unit Tests
13
2.1
Planning Tests
. . . . . . . . . . . . . . . . . . .
14
2.2
Testing a Simple Method
. . . . . . . . . . . . .
15
2.3
More Tests
. . . . . . . . . . . . . . . . . . . . .
20
3
Writing Tests in JUnit
21
3.1
Structuring Unit Tests . . . . . . . . . . . . . . .
21
3.2
JUnit Asserts . . . . . . . . . . . . . . . . . . . .
22
3.3
JUnit Framework . . . . . . . . . . . . . . . . . .
26
3.4
JUnit Test Composition . . . . . . . . . . . . . .
27
3.5
JUnit Custom Asserts . . . . . . . . . . . . . . .
32
3.6
JUnit and Exceptions . . . . . . . . . . . . . . .
33
3.7
More on Naming
. . . . . . . . . . . . . . . . . .
35
3.8
JUnit Test Skeleton
. . . . . . . . . . . . . . . .
35
1154792562.005.png
Zgłoś jeśli naruszono regulamin