Vous êtes sur la page 1sur 857

don't be afraid of buying books

Next

Test-Driven Development: A Practical Guide


By David Astels
Publisher: Prentice Hall PTR
Pub Date: July 02, 2003
ISBN: 0-13-101649-0

Table of
Contents
Index

Pages: 592

Test-Driven Development: A Practical Guide presents TDD from the perspective of the
working programmer: real projects, real challenges, real solutions, ...real code. Dave Astels
explains TDD through a start-to-finish project written in Java and using JUnit. He introduces
powerful TDD tools and techniques; shows how to utilize refactoring, mock objects, and
"programming by intention"; even introduces TDD frameworks for C++, C#/.NET, Python,
VB6, Ruby, and Smalltalk. Invaluable for anyone who wants to write better code... and have
more fun doing it!

Amazon

Prev

don't be afraid of buying books

Test-Driven Development: A Practical Guide


By David Astels
Publisher: Prentice Hall PTR
Pub Date: July 02, 2003
ISBN: 0-13-101649-0

Table of
Contents
Index

Pages: 592

Copyright
The Coad Series
About the Series
About the Series Editor
FOREWORD
PREFACE
EXTREME PROGRAMMING
WHO SHOULD READ THIS BOOK?
THE STRUCTURE OF THIS BOOK
CONVENTIONS USED IN THIS BOOK
ACKNOWLEDGEMENTS
Part I. BACKGROUND
Chapter 1. TEST-DRIVEN DEVELOPMENT
WHAT IS TEST-DRIVEN DEVELOPMENT?
LET THE COMPUTER TELL YOU
A QUICK EXAMPLE
SUMMARY
Chapter 2. REFACTORING
WHAT IS REFACTORING?
WHEN TO REFACTOR
HOW TO REFACTOR
SOME IMPORTANT REFACTORINGS
REFACTORING TO PATTERNS
SUMMARY
Chapter 3. PROGRAMMING BY INTENTION
NAMES
SIMPLICITY
WARRANTED ASSUMPTIONS
HOW TO PROGRAM BY INTENTION
"NO COMMENT"
SUMMARY

Next

Part II. TOOLS AND TECHNIQUES


Chapter 4. JUNIT
ARCHITECTURAL OVERVIEW
THE ASSERTIONS
WRITING A TESTCASE
RUNNING YOUR TESTS
USING SETUP() AND TEARDOWN()
USING TESTSUITE
HOW DOES IT ALL FIT TOGETHER?
WHERE DO TESTS BELONG?
TIPS
SUMMARY
Chapter 5. JUNIT EXTENSIONS
STANDARD EXTENSIONS
ADDING MISSING ASSERTS WITH MockObjects
PERFORMANCE AND SCALABILITY WITH JUnitPerf
DAEDALOS JUNIT EXTENSIONS
WRITING XML-BASED TESTS WITH XMLUnit
GARGOYLE SOFTWARE JUNIT EXTENSIONS
Chapter 6. JUNIT-RELATED TOOLS
JESTER
NOUNIT
CLOVER
ECLIPSE
IDEA
Chapter 7. MOCK OBJECTS
MOCK OBJECTS
AN ILLUSTRATIVE EXAMPLE
USES FOR MOCKOBJECTS
WOULDN'T IT BE NICE?
ACOMMON EXAMPLE
THE MOCKOBJECTS FRAMEWORK
MOCKMAKER
EASYMOCK
EasyMock Summary
SUMMARY
Chapter 8. DEVELOPING A GUI TEST-FIRST
THE EXAMPLE
THE AWT ROBOT
BRUTE FORCE
JFCUNIT
JEMMY
ULTRA-THIN GUI
SUMMARY

Part III. A JAVA PROJECT: TEST-DRIVEN END TO END


Chapter 9. THE PROJECT
OVERVIEW
USER STORIES AND TASKS
Chapter 10. MOVIE LIST
MAKE A MOVIE CONTAINER
MAKE A MOVIE LIST GUI
ADD A MOVIE IN THE GUI
RETROSPECTIVE
Chapter 11. MOVIES CAN BE RENAMED
SUPPORT MOVIE NAME EDITING
MOVIE RENAME GUI
RETROSPECTIVE
Chapter 12. MOVIES ARE UNIQUE
MOVIES ARE UNIQUE
ERROR MESSAGE ON NON-UNIQUENESS
RETROSPECTIVE
Chapter 13. RATINGS
ADD A SINGLE RATING TO MOVIE
SHOW THE RATING IN THE GUI
EDIT THE RATING
RETROSPECTIVE
Chapter 14. CATEGORIES
ADD A CATEGORY
SHOW THE CATEGORY IN THE GUI
ADD A SELECTION OF CATEGORY
RETROSPECTIVE
Chapter 15. FILTER ON CATEGORY
GET A SUBLIST BASED ON CATEGORY
SUPPORT AN ALL CATEGORY
ADD A CATEGORY SELECTOR TO THE GUI
HANDLE CHANGING A MOVIE'S CATEGORY
INTERFACE CLEANUP
RETROSPECTIVE
Chapter 16. PERSISTENCE
WRITE TO A FLAT FILE
SAVE-AS IN GUI
SAVE IN GUI
READ FROM A FLAT FILE
LOAD IN GUI
RETROSPECTIVE
Chapter 17. SORTING
COMPARE MOVIES
SORT A MOVIELIST

ASK A MOVIELISTEDITOR FOR SORTED LISTS


ADD A WAY TO SORT TO THE GUI
RETROSPECTIVE
Chapter 18. MULTIPLE RATINGS
MULTIPLE RATINGS
RATING SOURCE
REVISED PERSISTENCE
SHOW MULTIPLE RATINGS IN THE GUI
ADD A RATING IN THE GUI
REMOVE THE SINGLE-RATING FIELD
RETROSPECTIVE
Chapter 19. REVIEWS
ADD A REVIEW TO RATINGS
SAVE REVIEW
LOAD REVIEW
DISPLAY REVIEW
ADD A REVIEW
RETROSPECTIVE
Chapter 20. PROJECT RETROSPECTIVE
THE DESIGN
TEST VS. APPLICATION
TEST QUALITY
OUR USE OF MOCKS
GENERAL COMMENTS
DEBUGGING
LIST OF TESTS
SUMMARY
Part IV. XUNIT FAMILY MEMBERS
Chapter 21. RUBYUNIT
THE FRAMEWORK
EXAMPLE
Chapter 22. SUNIT
THE FRAMEWORK
BOOLEAN ASSERTIONS
BLOCK ASSERTIONS
EXCEPTION ASSERTIONS
FAILURE
EXAMPLE
Chapter 23. CPPUNIT
THE FRAMEWORK
EXAMPLE
Chapter 24. NUNIT
THE FRAMEWORK
EXAMPLE

Chapter 25. PYUNIT


THE FRAMEWORK
EXAMPLE
Chapter 26. VBUNIT
THE FRAMEWORK
EXAMPLE
Part v. APPENDICES
Appendix A. EXTREME PROGRAMMING
THE AGILE REVOLUTION
EXTREME PROGRAMMING
THE FOUR VARIABLES
THE VALUES
THE PRACTICES
SUMMARY
Appendix B. AGILE MODELING
THE MYTHS SURROUNDING MODELING
AN INTRODUCTION TO AGILE MODELING (AM)
WHAT ARE AGILE MODELS?
Appendix C. ONLINE RESOURCES
FORUMS
INFORMATION ON AGILE PROCESSES
INFORMATION ON EXTREME PROGRAMMING
JUNIT-RELATED SOFTWARE
JUNIT-RELATED INFORMATION
TOOLS
OTHER XUNIT FAMILY MEMBERS
COMPANIES
MISCELLANEOUS
Appendix D. ANSWERS TO EXERCISES
BIBLIOGRAPHY
Index

Amazon

Prev

don't be afraid of buying books

Copyright
Library of Congress Cataloging-in-Publication Data
A CIP catalog record for this book can be obtained from the Library of Congress
Editorial/Production Supervision: Kathleen M. Caren

Cover Design Director: Jerry Votta

Cover Illustration: M. C. Escher

Art Director: Gail Cocker-Bogusz

Manufacturing Manager: Alexis R. Heydt-Long

Executive Editor: Paul Petralia

Editorial Assistant: Michelle Vincenti

Marketing Manager: Christopher Guzikowski

2003 Pearson Education, Inc.


Publishing as Prentice Hall Professional Technical Reference
Upper Saddle River, New Jersey 07458

Next

Prentice Hall PTR offers excellent discounts on this book when ordered in quantity for bulk
purchases or special sales. For more information, please contact: U.S. Corporate and
Government Sales, 1-800-382-3419, corpsales@pearsontechgroup.com. For sales outside of the
U.S., please contact: International Sales, 1-317-581-3793, international@pearsontechgroup.com.
Company and product names mentioned herein are the trademarks or registered trademarks of their
respective owners.
All rights reserved. No part of this book may be reproduced, in any form or by any means, without
permission in writing from the publisher.
Printed in the United States of America
First Printing
Pearson
Pearson
Pearson
Pearson
Pearson
Pearson
Pearson
Pearson

Education Ltd.
Education Australia Pty., Limited
Education Singapore, Pte. Ltd.
Education North Asia Ltd.
Education Canada, Ltd.
Educacin de Mexico, S.A. de C.V.
EducationJapan
Education Malaysia, Pte. Ltd.

Dedication
To my parents who, though they may not have always approved or understood, let me try.

Amazon

Prev

don't be afraid of buying books

The Coad Series


Peter Coad, Series Editor
David J. Anderson
Agile Management for Software Engineering: Applying the Theory of Constraints for Business
Results
David Astels
Test Driven Development: A Practical Guide
David Astels, Granville Miller, Miroslav Novak
A Practical Guide to eXtreme Programming
Andy Carmichael, Dan Haywood
Better Software Faster
Donald Kranz, Ronald J. Norman
A Practical Guide to Agile Unified Process
Jill Nicola, Mark Mayfield, Michael Abney
Streamlined Object Modeling: Patterns, Rules, and Implementation
Stephen R. Palmer, John M. Felsing
A Practical Guide to Feature-Driven Development

Next

Amazon

Prev

don't be afraid of buying books

Next

About the Series


The Coad Series is a growing collection of practical guides "from the trenches." The series focuses on
key "must be addressed" IT pain points, felt by leaders around the globe. The series is especially suited
for CIOs, IT department managers, technology leaders, and change agents. The Coad Series addresses
the four dimensions of successful IT : technology, process, people, and vision. For the series, Peter Coad
personally selects authors and topics and then mentors the authors at a strategic level along the way.

Amazon

Prev

don't be afraid of buying books

Next

About the Series Editor


Peter Coad (pronounced "code") is senior vice president and chief strategist of Borland
(http://www.borland.com) Software Corporation. Coad collaborates with fellow Borland execs in
formulating long-term business and product strategies. Peter also represents Borland worldwide as a
thought leader, industry luminary, and evangelist among CXOs, technical department managers, and
developers.
Peter is globally recognized as an accomplished business strategist, model builder, and thought leader.
As business strategist, Peter formulates long-term competitive strategies for Borland. Previously, as
Chairman, CEO, and President of Together Soft, he led in growing that company 11.6 times revenue in
the span of two years, overall profitably. As model builder, Peter has built hundreds of models for nearly
every business imaginable, fully focused on building-in strategic business advantage. As thought leader,
Peter writes books (six to date) on building better software faster; he speaks at industry events
worldwide; and he is the Editor-in-Chief of The Coad Series, published by Prentice Hall; in addition,
Peter was an invited speaker on business strategy at the 2003 "Future in Review" conference.
Coad attended the Stanford Executive Program for Growing Companies and received a Master of Science
in Computer Science (USC) and a Bachelor of Science with Honors in Electrical Engineering (OSU).

Amazon

Prev

don't be afraid of buying books

Next

FOREWORD
My responsibility in writing this foreword is to help you decide whether to read this book. If you are
interested in improving your programs and your programming skill, this book can help you.
Test-Driven Development is a practice that can make your programs better. If you're like me, using the
techniques in Dave's book, you will find that your programs are more clear, that they come into being
more easily, and that you'll have fewer defects than you used to.
I'm not saying that TDD is some kind of magic potion; quite the contrary. TDD isn't magic, it is
something that you yourself do. By focusing attention on tests first, you'll be designing your program
more from the viewpoint of the user. By doing the tests one at a time, you'll be creating a simple
design that's focused exactly on the problem. As you work up all these little tests, you'll drive out most
of the defects that otherwise slip into your code. Finally, by saving the tests, you make the program
easier to maintain and improve as time goes on.
Dave's book is full of examples of Test-Driven Development. There's an extended example to show you
how TDD works over a longer haul. There are small examples showing how to use most of he TDDrelated tools that are available. There are even examples in most of the languages where TDD is used,
though the book's main focus is on examples in Java. This is a book about practice, with real examples
rather than dry theory.
But wait! There's more! Dave also gives a good introduction to refactoring, and to programming by
intention. And he introduces Mock Objects, an advanced and powerful technique in testing and TestDriven Development. Plus, he has a section on one of the tricky areas in TDD, creating GUIs in test-first
fashion.
You'll also find a quick and valuable summary of eXtreme Programming, a look at Agile Modeling, and a
comprehensive list of online resources relating to all the book's topics.
All these things are good and serve as reasons to buy this book. The core value of Dave's book, the real
meat, is in the code. Test-Driven Development is a technique that we use as we program. No matter
what design or modeling we have done before we begin programming, TDD helps us make the code
better. I'm sure that it will help you, if you'll give this book, and what it teaches, a chance.
Test-Driven Development has made my programs better, and those of many other programmers as well.
It's a technique that is worth adding to your bag of tricks. This book will help you improve as a
programmer. That's why I'm recommending it.
Ron Jeffries
www.XProgramming.com
Pinckney, Michigan
18 December 2002

Amazon

Prev

don't be afraid of buying books

Next

PREFACE
This isn't a book about testing.
This is a book about a way of programming that leads to simple, clear, robust code. Code that is easy
to design, write, read, understand, extend, and maintain.
This is a book about thinking, designing, communicating, and programming. It's just a really nice side
effect that we end up with a comprehensive[1] suite of tests.
[1] How comprehensive depends on how good we become at it.

This book explores Test-Driven Development, Test-First Programming, call it what you will: programming
by writing the tests first, then writing the code needed to make the tests pass. Specifically, working in
the smallest steps possible: write just enough of a test to fail, write just enough code to make it pass,
refactor to clean up the mess you made getting the test to pass.
This book focuses on the Java programming language and uses Java examples throughout. It is assumed
that the reader has at least an intermediate understanding of Java (and a working Java system if you
want to try out the examples for yourself). Example code and other support material is available at my
website[URL 54].
Even though the focus is on Java, Part IV looks at other prominent members of the xUnit family for
several popular languages. This is done by taking the first task from Chapter 10 and recasting it in the
various languages. This provides a good comparison of the different frameworks.

Amazon

Prev

don't be afraid of buying books

Next

EXTREME PROGRAMMING
Test-Driven Development is a core part of the agile process formalized by Kent Beck called eXtreme
Programming (XP). XP is probably the most agile of the agile processes, being extremely low overhead,
and extremely low ceremony. However, it is extremely high discipline, very effective, and incredibly
resilient to change.
That being said, you do not need to adopt XP in order to practice TDD and gain the benefit from it. TDD
is worth doing on its own. The quality of your code will improve. Of course, if you are doing XP it's well
worth it to get really good at TDD.
TDD is one of the main design tools that we have in XP. [2] As I mentioned earlier, the fact that we end
up with a set of tests is a very pleasant by-product. Because we have those tests, we can have
confidence we haven't inadvertently broken anything if the tests ran successfully before the change and
after it. Conversely, if a test fails after we make a change we know exactly what broke and are in the
best position to find the problem and fix it. The only thing that could have caused the failure was the
change we made since the last time the tests ran clean.
[2] The other is refactoring.

All this means is that because the tests are there, we can safely use another of the XP practices:
refactoring. As we will see in Chapter 2, refactoring is the process of making changes to the structure of
code without changing its external behavior. The tests let you confirm that you haven't changed the
behavior. This gives you the courage necessary to make (sometimes drastic) changes to working code.
The result is that the code is cleaner, more extensible, more maintainable, and more understandable.
Appendix A talks a bit more about eXtreme Programming. For more exhaustive information, you can
browse the bibliography and explore the online XP resources listed in Appendix C.

Amazon

Prev

don't be afraid of buying books

Next

WHO SHOULD READ THIS BOOK?


Should you read this book? Helping you answer that question is why I wrote this preface. There was
once an informal survey on the XP Yahoo Group as to the purpose that a preface should serve. The
general opinion was that by reading the preface you should get a good idea of whether you should buy
and read the book. I hope I've done a good job of it!
Read this book if you want to adopt eXtreme Programming. As stated earlier, being able to do TDD well
is worth the time and effort it takes to get good at it. TDD is at the heart of XP, so doing TDD well
makes the entire process that much more effective.
Read this book if you want to write code that is clearer, more robust, easier to extend, and as slim (as
opposed to bloated) as possible.
Read this book if you know there must be a better way than spending weeks or months drawing
pictures before writing a line of code.
Finally, read this book if you want to know how to make programming fun again.
In terms of what you should know before reading this book, it would help if you had at least an
intermediate understanding of Java. Having a good background in another OO language or two (such as
Smalltalk, C++, Python, or Ruby) will, however, enable you to get even more out of this book.
As this book goes to print there is one other TDD book available[9] (although I'm sure many will follow).
I was aware of that book being written as I wrote much of this one, and it was always a partial goal to
be complementary to it. From it you will get the philosophy and metaphysics of TDD, mixed with enough
pragmatics to make it real. If you are so inclined, I encourage you to read it first. The book you hold in
your hands is, as the title says, a practical guide to doing TDD. It's focused on one language (not the
best language, but arguably one that is very popular and well supported for TDD), and presents not only
concepts and principles, but tools and techniques.

Amazon

Prev

don't be afraid of buying books

THE STRUCTURE OF THIS BOOK


This book is divided into four parts:
I Background In Part I we examine some topics that relate to the main body of material
in the book (i.e., TDD in Java). We start with an introduction to TDD. This is followed by
chapters on refactoring and programming by intention. These two techniques are also
prominent in XP and are required and enabled by TDD.

II Tools and Techniques In Part II we take an in-depth look at various tools that are
useful for practicing TDD with Java, and how to use them. We start with a tutorial
introduction to JUnit, the defacto standard Java TDD framework. We continue by
exploring some of the standard (i.e., included in the distribution) and nonstandard
extensions to JUnit. Next, we explore some tools that support the use of JUnit and other
tools that are completely independent of JUnit but work well with it. The final chapters in
this part examine specific techniques or issues and the related tools.

III A Java Project: Test-Driven End to End This is a practical hands-on book. To that
end, Part III (which makes up the bulk of the book) is built around the development of a
real system, not a toy example. We work through this project test-first. Along the way
we draw on material from the previous parts of the book.

IV xUnit Family Members JUnit is just one member of a large and growing family of
programmer test frameworks. In Part IV we have a look at some of the other members
of the family. We don't look at all of them, but we go over several for the more popular
languages. So that we get a good comparison, we go through the same set of stories
(i.e., requirements) for each. Specifically, these are the initial stories from the Java
project. This lets us compare the various members with JUnit as well.

There are also four appendices:


A eXtreme Programming This appendix provides a very brief introduction to XP.

B Agile Modeling This appendix provides an introduction to and overview of Agile


Modeling.

Next

C Online Resources Throughout the book I refer to Web sites where you can find
information as well as downloads. This appendix contains a categorized, annotated list of
these sites.

D Answers to Exercises Many of the chapters in this book contain exercises for the
reader. This appendix contains all exercises in the book, with answers.

Amazon

Prev

don't be afraid of buying books

Next

CONVENTIONS USED IN THIS BOOK


I've adopted a handful of visual conventions which I've used throughout this book to make it easier for
you, the reader, to differentiate between different sorts of information.

Source Code
This book contains a large amount of source code. When one of more complete lines of code is being
presented, it is indented and set in a sans-serif font, like this:
pub l ic in t g et Ave ra geRa ting () {
r e turn tot al Rat in g / numb erOf R a tin g s ;
}

When only part of a line is being presented, it is set in the same font, but kept in the body of the text.
This often includes class names (M ovie), methods (eq u a ls () ), and constants (t ru e ," a s tr i ng ",
42).
In general, when a method is referred to parameters are not included, but empty parentheses are, so
that it is obvious that it is a method as opposed to some other type of identifier, for example:
aMe t hod() .
In blocks of code, p ac kag e and imp ort statements are generally left out.

Filesystem and console I/O


Terms relating to the filesystem are set in a serif, monospaced font. This includes items like filenames
(fil t er.pr ope rt ies )and commands and their output:
$
>
>
>
>

j a va \
- c lassp ath b in: /u sr/l ocal /jav a / lib / M ock M a ke r. ja r \
m o ckmak er. Mo ckM ak er \
c o m.sao rsa .t ddb oo k.sa mple s.mo c k obj e c ts. I n tC al cu l at o r \
> s rc/co m/s ao rsa /t ddbo ok/s ampl e s /mo c k obj e c ts /M oc k In t Ca lc u la to r .j av a

Tips and Sidebars


I've used a couple of different callout mechanisms to highlight information that is important to take note
of, or is interesting but doesn't fit in the body of the text for some reason.
Throughout the book there are small bits of wisdom that you may find
especially useful.
These are set apart the way this paragraph is.

A Sample Sidebar
This is an example of a very short sidebar; most are a half to full page in length.

I've used sidebars to separate short passages that are not directly related to the main body of text.
Sidebars get placed as
somewhere as an example.

sees fit, usually at the top of a page. There's one around here

Terminology
I learned OO in the context of Smalltalk and I've used Smalltalk terminology from the beginning. If
you're not familiar with Smalltalk, I include a few terms that I use, and how they map to Java and
C++:
instance variable A variable whose scope is an object. Each object has a separate copy
of this variable. (Java: field or member variable, C++: data member.)

class variable A variable whose scope is a class. All instances of the class share a single
copy of the variable. (Java: static field, C++: static data member.)

method A functional member of a class. (Java: method, C++: member function.)

sending messages to an object A more abstract way to refer to calling an object's


methods.

senders Methods that send a specific message, that is, call a specific method (commonly
called references to a method).

Amazon

Prev

don't be afraid of buying books

Next

ACKNOWLEDGEMENTS
A preface is not complete without acknowledging the other people that make a book possible. Being an
author is like being at the peak of a pyramid. . . you are being supported (and your work made possible)
in various ways by a multitude of other people. This is my chance to acknowledge and thank them. . .
by name for the ones I'm aware of.
Kent Beck for making TDD and XP household wordsat least in my householdand for his support of
this book.
Miroslav Novak for first turning me on to this new way of programming that a bunch of smart people
were talking about on something called a Wiki. Miroslav may be my junior in terms of time spent
programming, but I've learned more from him than I sometimes care to admit.
Patrick Wilson-Welsh for several things: for always reminding me of the big picture when I got mired
down in the details of the moment; for being the best sounding board and copy editor that an author
could ask for; and for having the courage to leave an established life in Washington, D.C. to move to
small-town Canada to become my co-founder and first apprentice.

Dave Thomas of "The Pragmatic Programmers"[URL 55]for letting me use the


macros he
wrote for the book "The Pragmatic Programmer"[25]. That book was inspiring in its layout and
typesetting as well as catalytic in bringing about a turning point in my thoughts about programming.
Hand in hand with "The Pragmatic Programmer" went "Software Craftsmanship"[34] by Pete McBreen. I
mean that literally,.. . I read them back-to-back. Pete provides a wonderful introduction to and
discussion of software as a craft. A fabulous book, it was another contributing factor to my careershaking epiphany (the third being XP). Thanks, Pete.
Peter Coad, to whom I owe a great debt for taking me under his wing in many ways and helping me to
get this project off the ground. I have to thank him also for letting me charge ahead with a TDD edition
of The Coad Letter[URL 61].
Paul Petralia, my acquisitions editor at Prentice Hall, and the fine crew that works with him. Thanks for
letting us convince you that this book isn't about "Testing," and for believing in it whole heartedly once
we had accomplished that.
Craig Larman must be mentioned here for his encouragement, support, and advice. I still have great
memories of spending a day with Craig at his home outside Dallas, discussing UML and Together[URL
34]and drinking homemade Chai.
And a big thanks to Ron Jeffries for writing the foreword for me, as well as being generally supportive of
my XP-related endeavors, specifically (well, what comes to mind as I write this) this book, and the TDD
Coad Letter. Also, for doing so much to bring XP so far.
Special thanks and a hearty acknowledgement to members of the TDD Yahoo! group that sent me their
JUnit tips: Darren Hobbs, J. B. Rainsberger, and Derek Weber.
Very special thanks to those that contributed to the book by writing and letting me use material on
subjects that they are the experts in, specifically (in order of appearance):
Mike Clark for the section on JUnitPerf,

Jens Uwe Pipka for the section on the Daedalos extensions,

Tim Bacon for the section on xmlUnit,

Mike Bowler for the section on the Gargoyle extensions,

Bryan Dollery for the section on IDEA,

James Newkirk for the chapter on NUnit,

Bob Payne for stepping in at the last minute with the chapter on PyUnit,

Kay Pentecost for the chapter on vbUnit, and

Scott Ambler for the appendix on agile modeling.

Thanks to all the folks in the XP community who gave me feedback (in no particular order): Kay
Pentecost, Edmund Schweppe, Aldo Bergamini, Mike Clark, Francesco Cirillo, and my friends, colleagues,
and past co-authors: Randy Miller and Miroslav Novak. As with all authors, I'm sure I've missed
someone. Sorry about that.
I need to acknowledge and thank my reviewers as well: Alan Francis and William Wake.
And yes, as Kent Beck says in the preface of his TDD book[9], it is clich to thank our families, but they
heartily deserve it. To my wife, Kate, for saying "I'll clean up the kitchen. You go write." To my kids,
Tasha and Jason, for being understanding when I had to write, and for thinking that it's so cool to have
a Dad who writes books. Finally, to my youngest child, Leah, who is too young to notice what I'm doing
but simply smiles when she sees me and gives me a hug when I pick her up.
This book was produced using a variety of open source software. All my computers run Redhat Linux.
The manuscript was prepared using GNU Emacs, and typeset using
. Image manipulation was
done with Gimp. The xdvi previewer was used extensively. The PDF version was created using dvips, and
ps2pdf. Several packages were used with
, some off the shelf (lgrind, draft-copy, and fixme),
several courtesy of Dave Thomas (for exercises, extended cross reference support, and url references),
and several of my own (chapter heading quotes, story/task/test management, sidebars, and tips).

Amazon

Prev

don't be afraid of buying books

Part I: BACKGROUND
This part of the book provides an introduction to Test-Driven Development and associated
techniques and issues: refactoring and programming by intention.

Next

Amazon

Prev

don't be afraid of buying books

Next

Chapter 1. TEST-DRIVEN DEVELOPMENT


To vouch this, is no proof, Without more wider and more overt test
Othello, Act 1 Scene 3 William Shakespeare
From programmers to users, everyone involved in software development agrees: testing is good. So why
are so many systems so badly tested? There are several problems with the traditional approach to
testing:
If testing is not comprehensive enough, errors can make it into production and cause potentially
devastating problems.
Testing is often done after all the code is written, or at least after the programmer who wrote the
code has moved on to other things. When you are no longer living and breathing a particular
program, it takes some time and effort to back into the code enough to deal with the problems.
Often tests are written by programmers other than those who wrote the code. Since they may
not understand all of the details of the code, it is possible that they may miss important tests.
If the test writers base their tests on documentation or other artifacts other than the code, any
extent to which those artifacts are out of date will cause problems.
If tests are not automated, they most likely will not be performed frequently, regularly, or in
exactly the same way each time.
Finally, it is quite possible with traditional approaches to fix a problem in a way that creates
problems elsewhere. The existing test infrastructure may or may not find these new problems.
Test-Driven Development solves all of these problems, and others.
The programmer does the testing, working with tests while the code is freshly in mind. In fact,
the code is based on the tests, which guarantees testability, helps ensure exhaustive test
coverage, and keeps the code and tests in sync. All tests are automated. They are run quite
frequently and identically each time.
Exhaustive test coverage means that if a bug is introduced during debugging, the test scaffolding
finds it immediately and pinpoints its location. And the test-debug cycle is kept quite short: there
are no lengthy delays between the discovery of a bug and its repair.
Finally, when the system is delivered, the exhaustive test scaffolding is delivered with it, making
future changes and extensions to it easier.
So from a pure testing standpoint, before we begin to discuss non-testing benefits, TDD is superior to
traditional testing approaches. You get more thoroughly tested code, period.
But you do indeed get much more than that. You get simpler designs. You get systems that reveal
intent (describe themselves) clearly. The tests themselves help describe the system. You get extremely
low-defect systems that start out robust, are robust at the end, and stay robust all the time. At the end
of every day, the latest build is robust.
These are benefits for all project stakeholders. But perhaps the most immediate and most tangible
benefit is exclusively the programmer's: more fun. TDD gives you, the programmer, small, regular,

frequent doses of positive feedback while you work. You have tangible evidence that you are making
progress, and that your code works.
There is a potential problem with all this, of course. It is more addictive than caffeine. Once you're
hooked, you'll want to program this way, and only this way, from then on. And I for one certainly hope
you do.
This chapter will give you an overview of Test-Driven Development, including a short example of a
programming session.

Amazon

Prev

don't be afraid of buying books

Next

WHAT IS TEST-DRIVEN DEVELOPMENT?


Test-Driven Development (TDD) is a style of development where:
you maintain an exhaustive suite of Programmer Tests,
no code goes into production unless it has associated tests,
you write the tests first,
the tests determine what code you need to write.
Let's look at each of these in turn.

Maintain an exhaustive suite of Programmer Tests


You have Programmer Tests to test that your classes exhibit the proper behavior. Programmer Tests are
written by the developer who writes the code being tested. They're called Programmer Tests because
although they are similar to unit tests, they are written for a different reason. Unit tests are written to
test that the code you've written works. Programmer Tests are written to define what it means for the
code to work. Finally, they're called Programmer Tests to differentiate them from the tests that the
Customer writes (called, logically enough, Customer Tests) to test that the system behaves as required
form the point of view of a user.
Using Test-Driven Development implies, in theory, that you have an exhaustive test suite. This is
because there is no code unless there is a test that requires it in order to pass. You write the test, then
(and not until then) write the code that is tested by the test. There should be no code in the system
which was not written in response to a test. Hence, the test suite is, by definition, exhaustive.

No code goes into production unless it has associated tests


One of eXtreme Programming's tenets is that a feature does not exist until there is a suite of tests to go
with it. The reason for this is that everything in the system has to be testable as part of the safety net
that gives you confidence and courage. Confidence that all the code tests clean gives you the courage
(not to mention the simple ability) to refactor and integrate. How can you possibly make changes to the
code without some way to confidently tell whether you have broken the previous behavior? How can you
integrate if you don't have a suite of tests that will immediately (or at least in a short time) tell you if
you have inadvertently broken some other part of the code?

Write the tests first


Now we're getting eXtreme. What do I mean by write the tests first? I mean that when you have a task
to do (i.e., some bit of functionality to implement) you write code that will test that the functionality
works as required before you implement the functionality itself.
Furthermore, you write a little bit of test, followed by just enough code to make that test pass, then a
bit more test, and a bit more code, test, code, test, code, etc.

Tests determine what code you need to write

By writing only the code required to pass the latest test, you are putting a limit on the code you will
write. You write only enough to pass the test, no more. That means that you do the simplest thing that
could possibly work. I think an example is in order. Let's say you are working on a list class. The logical
place to start is with the behavior of an empty list (it makes sense to start with the basis, or simplest,
case). So you write the test:
pub l ic vo id te stE mp tyLi st() {
Movie Lis t emp ty List =ne wMov i e Lis t ( );
asser tEq ua ls( "E mpty lis t sh o u ld h a ve s i ze o f 0 ", 0, e m pt yL i st .s i ze () ) ;
}

To pass this test we need a Mov ieLi st class that has a si z e( ) method.
When you are working this way, you want to work in small increments. . . some-times increments that
seem ridiculously small. When you grasp the significance of this, you will be on your way to mastering
TDD. Later we'll explore the important and sometimes unexpected benefits and side effects of testing
and coding in tiny increments.

Amazon

Prev

don't be afraid of buying books

Next

LET THE COMPUTER TELL YOU


Write your tests (and your code for that matter) without worrying about what classes or methods you
will need to add. Don't even bother keeping a To Do list. Well, at least in terms of what classes,
methods, etc., you need to create. You will likely want a To Do list to keep track of tests you want to
write and other higher level items. Just write your test and compile.
If you need to add a class or method the compiler will tell you. It provides a better To Do list than you
could, and faster. In the previous example when I compile [1] after writing the test (with nothing else
written) I get the error:
[1] Modern Java programming environments will alert me to these missing items even before I
compile. Furthermore, they will offer solutions and do the work of creating the stubs for me.

Mov i eList ca nn ot be res olve d or i s n o t a t y pe .

This immediately tells me that I need to create a new Mov i eL i st class, so I do:
pub l ic cl ass M ovi eL ist {
}

I compile again and get another error:


The metho d s iz e() i s un defi ned f o r t h e ty p e M ov ie L is t

In response to this I add a stub s ize() method:


pub l ic in t s iz e() {
r e turn 0;
}

Now it will compile. Run the test, and it works. Due to Java requiring a return statement when a return
type is defined, we need to combine the steps of creating the method and adding the simplest return
statement. I have made a habit of always stubbing methods to return the simplest value possible (i.e.,
0, fal s e, or nul l).
What!?! Just re tu rn 0? That can't be right. Ah...but it is right. It is the simplest thing that could
possibly work to pass the test we just wrote. As we write more tests we will likely need to revisit the
siz e () method, generalizing and refactoring, but for now r et u rn 0 is all that is required.

Amazon

Prev

don't be afraid of buying books

Next

A QUICK EXAMPLE
Let's take a peek into the development of the project from later in the book. We have a Mo vi e class
which now needs to accept multiple ratings (e.g., 3 as in "3 stars out of 5") and give access to the
average.
As we go through the example, we will be alluding to a metaphor for the TDD flow originated by William
Wake: The TDD Traffic Light[URL 9][URL 61].
We start by writing a test, and we start the test by making an assertion that we want to be true:
pub l ic vo id te stR at ing( ) {
a s sertE qua ls ("B ad ave rage rat i n g." , 4 ,st a r Wa rs .g e tA v er ag e Ra ti n g( )) ;
}

Now we need to set the stage for that assertion to be true. To do that we'll add some rating to the
Mov i e:
pub l ic vo id te stR at ing( ) {
s t arWar s.a dd Rat in g(3) ;
s t arWar s.a dd Rat in g(5) ;
a s sertE qua ls ("B ad ave rage rat i n g." , 4 ,st a r Wa rs .g e tA v er ag e Ra ti n g( )) ;
}

Finally, we need to create the Mo vie instance we are working with:


pub l ic vo id te stR at ing( ) {
M o vie s tar Wa rs = new Movi e("S t a r W a r s") ;
s t arWar s.a dd Rat in g(3) ;
s t arWar s.a dd Rat in g(5) ;
a s sertE qua ls ("B ad ave rage rat i n g." , 4 ,st a r Wa rs .g e tA v er ag e Ra ti n g( )) ;
}

When we compile this, the compiler complains that a ddR a ti n g( in t ) and g et Av e ra ge R at in g () are
undefined. This is our yellow light. Now we make it compile by adding the following code to Mo vi e :
pub l ic vo id ad dRa ti ng(i nt n ewRa t i ng) {
}

pub l ic in t g et Ave ra geRa ting () {


r e turn 0;
}

Note that since we are using Java, we must provide a return value for g e tA ve r ag eR a ti ng ( ) since
we've said it returns an i nt .
Now it compiles, but the test fails. This is the red light (aka red bar). This term is derived by the JUnit
interfaces that present a progress bar that advances as tests are run. As long as all tests pass, the bar
is green. As soon as a test fails, the bar turns red and remains red. The message we get is:

Bad avera ge ra tin g. exp ecte d:<4 > but w as: < 0 >

Now we have to make the test pass. We add code to ge t Av e ra ge R at i ng () to make the test pass:
pub l ic in t g et Ave ra geRa ting () {
r e turn 4;
}

Recompile and rerun the test. Green light! Now we refactor to remove the duplication and other smells
that we introduced when we made the test pass.
You're probably thinking "Duplication.. . what duplication?" It's not always obvious at first. We'll start by
looking for constants that we used in making the test work. Sure enough, look at
get A verag eRa ti ng( ).It returns a constant. Remember that we set the test up to get the desired
result. How did we do that? In this case we gave the movie two ratings: 3 and 5. The average result is
the 4 that we are returning. So, that 4 is duplicated. We provide the information required to compute it,
as well as returning it as a constant. Returning a constant when we can compute its value is a form of
duplication. Let's get rid of it.
Our first step is to rewrite that constant into something related to the provided information:
pub l ic in t g et Ave ra geRa ting () {
r e turn (3 + 5) / 2;
}

Compile and run the tests. We're OK. We have the courage to continue. The 3 and 5 are duplicate with
the arguments to ad dRa ti ng() so let's capture them. Since we add the constants we can simply
accumulate the arguments. First we add a variable to accumulate them:
pri v ate i nt to tal Ra ting = 0 ;

Then we add some code to add Rati ng() :


pub l ic vo id ad dRa ti ng(i nt n ewRa t i ng) {
t o talRa tin g += ne wRat ing;
}

Now we use it in g et Ave ra geRa ting () :


pub l ic in t g et Ave ra geRa ting () {
r e turn tot al Rat in g / 2;
}

Compile, test, it works! We're not finished yet, though. While we were refactoring we introduced another
constant: the 2 in get Av erag eRat ing( ) .The duplication here is a little subtler. The 2 is the number
ratings we added, i.e., the number of times add R a tin g ( ) was called. We need to keep track of that in
order to get rid of the 2.

Like before, start by defining a place for it:


pri v ate i nt nu mbe rO fRat ings = 0 ;

Compile, run the tests, green. Now, increment it every time a d dR at i ng ( ) is called:
pub l ic vo id ad dRa ti ng(i nt n ewRa t i ng) {
t o talRa tin g += ne wRat ing;
n u mberO fRa ti ngs ++ ;
}

Compile, run the tests, green. OK, finally we replace the constant 2 with n um b er Of R at in g s:
pub l ic in t g et Ave ra geRa ting () {
r e turn tot al Rat in g / numb erOf R a tin g s ;
}

Compile, run the tests, green. OK, we're done. If we want to reinforce our confidence in what we did,
we can add more calls to a ddRa ting () and check against the appropriate expected average. For
example:
pub l ic vo id te stL ot sOfR atin gs()
{
M o viego dzi ll a = n ew M ovie ("Go d z ill a " );
g o dzill a.a dd Rat in g(1) ;
g o dzill a.a dd Rat in g(5) ;
g o dzill a.a dd Rat in g(1) ;
g o dzill a.a dd Rat in g(2) ;
a s sertE qua ls ("B ad ave rage rat i n g." , 2 ,go d z il la .g e tA v er ag e Ra ti n g( )) ;
}

I need to underline the fact that I recompiled and ran the tests after each little change above. This
cannot be stressed enough. Running tests after each small change gives us confidence and reassurance.
The result is that we have courage to continue, one little step at a time. If at any point a test failed we
know exactly what change caused the failure: the last one. We back it out and rerun the tests. The tests
should pass again. Now we can try again... with courage.
The above example shows one school of thought when it comes to cleaning up code. In it we worked to
get rid of the duplication that was embodied in the constant. Another school of thought would leave the
constant 4 in place and write another test that added different ratings, and a different number of them.
This second test would be designed to require a different returned average. This would force us to
refactor and generalize in order to get the test to pass.
Which approach should you take? It really depends on how comfortable you are with what you are
attempting. Remember that you do have the test to safeguard you. As long as the test runs, you know
that you haven't broken anything. In either case you will want to write that second test: either to drive
the generalization, or to verify it.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
We've explored what Test-Driven Development is:
an exhaustive suite of Programmer Tests,
no code without tests,
tests first,
tests determine the code.
We've seen how to leverage feedback from the computer to keep track of what we should do next: if we
need to create a class, method, variable, etc., the system will let us know.
We've even seen a quick example of TDD in action, step by step, building some code to maintain the
average rating of a movie.
However, before we can jump in and start practicing it in earnest, we need to make sure we have a few
basic techniques and skills that TDD builds on. The next few chapters will explore these.

Agile Modeling and TDD


A primary benefit of both modeling and TDD is that they promote a think before you act
approach to development. Just because they offer the same type of benefit doesn't mean
that they are incompatible with one another. Instead, experience shows that we can and
should model on a project taking a TDD approach.
Consider XP projects that clearly take a TDD approach. Modeling is definitely an important
part of XP. XP practitioners work with user stories, and user stories are clearly agile models.
XP practitioners also create CRC cards whenever they need to, also agile models. In
eXtreme Programming Explained [8], Kent Beck even includes sketches of class diagrams.
Heresy? No. Just common sense. If creating a model can help our software development
efforts then that's what we do. It's as simple as that.
Creating agile models can help our TDD efforts because they can reveal the need for some
tests. As an agile modeler sketches a diagram they will always be thinking, "How can I test
this?" in the back of their minds because they will be following the practice consider
testability. This will lead to new test cases. Furthermore, we are likely to find that some of
our project stakeholders or even other developers simply don't think in terms of tests;
instead, they are visual thinkers. There's nothing wrong with this as long as we recognize it
as an issue and act accordinglyuse visual modeling techniques with visual thinkers and
test-driven techniques with people that have a testing mindset.
TDD can also improve our agile modeling efforts. Following a test-first approach, agile
developers quickly discover whether their ideas actually work or notthe tests will either
validate their models or notproviding rapid feedback regarding the ideas captured within
the models. This fits in perfectly with AM's practice of Prove it With Code.
Agile Modeling (AM) and Test-Driven Development (TDD) go hand in hand. The most
effective developers have a wide range of techniques in their intellectual toolboxes, and AM
and TDD should be among those techniques.
See Appendix B for more information on Agile Modeling.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 2. REFACTORING
Urge the necessity and state of times, And be not peevish-fond in great designs.
Richard III, Act 4 Scene 4 William Shakespeare
This chapter is here because refactoring is so important to TDD. We'll have a quick look at some of the
ideas and techniquesjust enough to get a firm enough grasp of it to tackle the project later in the
book. For everything we ever wanted to know about refactoring check out [16], [29], [41], [URL 4],
and [URL 10].

Amazon

Prev

don't be afraid of buying books

Next

WHAT IS REFACTORING?
Refactoring is the process of making changes to existing, working code without changing its external
behavior. In other words, changing how it does it, but not what it does. The goal is to improve the
internal structure.
Refactoring is closely related to TDD in two ways:
After doing the simplest thing possible to make a test pass (breaking any/all rules in the
process), we refactor to clean up, mostly removing duplication we introduced getting the test to
pass.
If we are practicing TDD, then we have the safety net of tests in place that allows us to refactor
with confidence.

Amazon

Prev

don't be afraid of buying books

Next

WHEN TO REFACTOR
Generally speaking, we refactor whenever we need to. However, there are three situations when we
must refactor:
1. when there is duplication
2. when we perceive that the code and/or its intent isn't clear
3. when we detect code smells, that is, subtle (or not so subtle) indications that there is a problem.

Duplication
Duplicated code in its various forms is the death of good code. We absolutely must get rid of it.
Duplication is such a problem that several people have warned against it at length, including Ron Jeffries
et al. ("Say Everything Once and Only Once"[27]) and Dave Thomas & Andrew Hunt ("Don't Repeat
Yourself"[25]).
If we see duplicated code, we need to look closely to see what it's doing. It's likely that there is
something useful or important being done. We need to put it in one place. We can do this by extracting
duplicate code into separate methods that can then be called from multiple locations. If the duplication is
in an inheritance hierarchy, we may be able to push the duplicated code up the hierarchy. If the
structure of some code is duplicated but not the details, we can extract the differing parts and make a
template method of the common structure.
Some cases of duplication will be simple, others won't be. Some will be obvious, but some will be very
subtle.
An example of a simple case of duplication is an expression (or sequence of expressions) that appears in
multiple methods. In this case, it is simply a matter of moving the duplicated expression(s) into a
separate method and replacing the original occurrences with calls to the new method.
Here's an example of simple duplication. The majority of these two methods are identical. The only
difference is the first line of s aveA s():
p ub l ic b ool e an s a ve() thro ws I O E x c e p t i o n {
i f ( ou tpu t File == n u ll) {
re tu rn f alse ;
}

F i le Wr ite r wri t er = new File W r i t e r ( o u t p u t F i l e ) ;


m o vi es .wr i teTo ( writ e r);
w r it er .cl o se() ;
r e tu rn tr u e;

p ub l ic b ool e an s a veAs ( ) th rows I O E x c e p t i o n {


o u tp ut Fil e = v i ew.g e tFil e();
i f ( ou tpu t File == n u ll) {
re tu rn f alse ;
}

F i le Wr ite r wri t er = new File W r i t e r ( o u t p u t F i l e ) ;


m o vi es .wr i teTo ( writ e r);
w r it er .cl o se() ;
r e tu rn tr u e;

We can remove the duplication by chaining to s a v e ( ) after getting the file in s a v e A s ( ) , like so:
p ub l ic b ool e an s a veAs ( ) th rows I O E x c e p t i o n {
o u tp ut Fil e = v i ew.g e tFil e();
s a ve () ;
}

A more subtle form of duplication was shown in the example on page 9. That case deals with duplication
between a constant and the information supplied to calculate it. Notice how, in that example, refactoring
to remove duplication drove the code toward a more general solution.
Another form of duplication occurs at runtime when the same information is maintained redundantly. In
the following example, we've moved from keeping track of how many times add has been called
(satisfying initial tests that verify the list size) to storing the added movies. To satisfy a test that verifies
that the list contains the movies that were added and not ones that weren't added we needed to add
storage for the added movies. We properly just added the required code to get the new test passing,
without much regard for the code that is there already.
When you are practicing TDD, you have two hats[16]:
coding: When you wear this hat you are adding new
functionality to the system. You are not refactoring.

refactoring: When you wear this hat you are


refactoring. You are not adding new functionality.

When you are getting a test to pass, you wear your coding hat. Once
the test is passing, you switch hats and refactor to clean up.

Now we need to refactor the old code out since it is now maintaining redundant information. Here's the
code before refactoring:
p ub l ic c las s Mov i eLis t {
p r iv at e i n t nu m berO f Movi es = 0 ;
p r iv at e C o llec t ion m ovie s = n e w A r r a y L i s t ( ) ;

p u bl ic in t siz e () {
re tu rn m ovie s .siz e ();

p u bl ic vo i d ad d (Mov i e mo vieT o A d d ) {
mo vi es. a dd(m o vieT o Add) ;
nu mb erO f Movi e s++;
}

After removing the maintenance of the redundant information, the code looks like this:

p ub l ic c las s Mov i eLis t {


p r iv at e C o llec t ion m ovie s = n e w A r r a y L i s t ( ) ;

}
}

p u bl ic in t siz e () {
re tu rn m ovie s .siz e ();
p u bl ic vo i d ad d (Mov i e mo vieT o A d d ) {
mo vi es. a dd(m o vieT o Add) ;

Unclear Intent
The code is the most important deliverable. Because of that, it must be as clear and understandable as
possible. It's not always possible to write it this way at the beginning, so we refactor the code to make
it more understandable. One powerful and simple way to clarify intent is to choose better names for
things. We can often pick a name out of the air that is close but not exactly what we want. One trick is
to keep a thesaurus on hand and use it when we are having trouble coming up with a name that
communicates our exact intent.
Using TDD helps make our intent clear when writing code by forcing us to think about the interface of a
class rather than its implementation. We have a chance to decide what makes the most sense from the
point of view of a user of the class, without getting mired in implementation details. Chapter 3 goes into
detail on how to make intent clear when coding.

Code Smells
The concept of code smells [1] is widely used by the eXtreme Programming community to refer to
characteristics of code that indicate less than acceptable quality. In this section we will briefly overview
a few of the more important smells.
[1] The term code smells was first used by Fowler and Beck in [ 16].

When we find smells in our code, we refactor to get rid of them. In fact, one very useful feature of [16]
is a cross reference of smells to indicated refactorings.
One thing to remember is that many of these smells don't always indicate a problem, but they do
indicate something that we should have a closer look at to see if there is a problem.

Comments
Most comments are written either because they are required by the process being used or to
compensate for poorly written code. If we see a comment or feel compelled to write a comment,
consider refactoring or rewriting the code first. See Chapter 3 for details on how to make code more
understandable.
Here's some code that has comments that serve to break the method into separate functional units.
Here, the comments indicate that those functional units should be extracted into separate methods. Look
on page 29 to see this done.
p ub l ic v oid init ( ) {
/ / s et th e lay o ut
g e tC on ten t Pane ( ).se t Layo ut(n e w F l o w L a y o u t ( ) ) ;
/ / c re ate the l ist
m o vi eL ist = ne w JLi s t(my Edit o r . g e t M o v i e s ( ) ) ;
J S cr ol lPa n e sc r olle r = n ew J S c r o l l P a n e ( m o v i e L i s t ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;
/ / c re ate the f ield
m o vi eF iel d = n e w JT e xtFi eld( 1 6 ) ;
g e tC on ten t Pane ( ).ad d (mov ieFi e l d ) ;

/ / c re ate thea d d bu t ton


a d dB ut ton = ne w JBu t ton( "Add " ) ;
a d dB ut ton . addA c tion L iste ner( n e w A c t i o n L i s t e n e r ( ) {
pu bl ic v oid a ctio n Perf orme d ( A c t i o n E v e n t e ) {
my Edi t or.a d d(mo v ieFi eld. g e t T e x t ( ) ) ;
mo vie L ist. s etLi s tDat a(my E d i t o r . g e t M o v i e s ( ) ) ;
}
});
g e tC on ten t Pane ( ).ad d (add Butt o n ) ;

Data Class
This is a class that is essentially an evolved r e c o r d or s t r u c t . It simply contains data and has little
behavior. Don't be fooled by accessor methods that are only there to support the data. A real stink is
the existence of public instance variables (fields/attributes).
If we look around the code, we may find other classes that operate on instances of the data class. We
need to merge the two either by moving data into the other class or moving behavior into the data
class.
Here's a simple example:
p ub l ic c las s Poi n t {
p r iv at e i n t x;
p r iv at e i n t y;
p u bl ic Po i nt() {
t h is (0 , 0 ) ;
}

p u bl ic Po i nt(i n t in i tial X, i n t i n i t i a l Y ) {
x= in iti a lX;
y= in iti a lY;
}
p u bl ic in t get X () {
re tu rn x ;
}
p u bl ic in t get Y () {
re tu rn y ;
}
p u bl ic vo i d se t X(in t new X) {
x= ne wX;
}

p u bl ic vo i d se t Y(in t new Y) {
y= ne wY;
}

Here's an even stinkier version of the same thing:


p ub l ic c las s Poi n t {
p u bl ic in t x;
p u bl ic in t y;
p u bl ic Po i nt() {
th is (0, 0);
}

p u bl ic Po i nt(i n t in i tial X, i n t i n i t i a l Y ) {
x= in iti a lX;
y= in iti a lY;
}

This version should, initially, be refactored to encapsulate the instance variables. That would result in the
first version of Poi n t. Data classes are often manipulated by code that lives in other classes. The next
step in this example would be to look at the code that uses P o i n t to see if there is code that would be
better placed in Po i nt. For example:
p ub l ic c las s Sha p e {
p r iv at e P o int c ente r ;
p u bl ic Sh a pe() {
ce nt er = new P oint ( );
}

p u bl ic vo i d tr a nsla t e(in t dX , i n t d Y ) {
ce nt er. s etX( c ente r .get X() + d X ) ;
ce nt er. s etY( c ente r .get Y() + d Y ) ;
}

The code in t rans l ate () should be moved into P o i n t :


p ub l ic c las s Poi n t {
//. . .

p u bl ic vo i d tr a nsla t e(in t dX , i n t d Y ) {
x += dX ;
y += dY ;
}

p ub l ic c las s Sha p e
{
//. . .

p u bl ic vo i d tr a nsla t e(in t dX , i n t d Y ) {
ce nt er. t rans l ate( d X, d Y);
}

Duplicated Code
This is one of the worst smells. We discussed duplication earlier in this chapter.

Inappropriate Intimacy
This is the case where a class knows too much about another's internal details. To deal with this,
methods should be moved so that the pieces that need to know about each other are together.
For example, consider this method in a class M o v i e L i s t which writes the list to a W r i t e r :

p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {


I t er at or m ovie I tera t or = mov i e s . i t e r a t o r ( ) ;
w h il e (mo v ieIt e rato r .has Next ( ) ) {
Mo vi e m o vieT o Writ e = ( Movi e ) m o v i e I t e r a t o r . n e x t ( ) ; ;
de st ina t ion. w rite ( movi eToW r i t e . g e t N a m e ( ) ) ;
de st ina t ion. w rite ( '|') ;
de st ina t ion. w rite ( movi eToW r i t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
de st ina t ion. w rite ( '|') ;

tr y {
de sti n atio n .wri t e(In tege r . t o S t r i n g ( m o v i e T o W r i t e . g e t R a t i n g ( ) ) ) ;
} ca tch (Unr a tedE x cept ion e x ) {
de sti n atio n .wri t e("- 1");
}
de st ina t ion. w rite ( '\n' );

This method has and uses full knowledge of the structure of the class M o v i e . If the structure of M o v i e
changes (e.g., multiple ratings are supported), then this method, in a different class, must change to
match. In short, this method uses too much knowledge about M o v i e . The solution is to extract the
portion that deals with writing a single M o v i e and move it to the M o v i e class. The final result is:
p ub l ic c las s Mov i eLis t {
/ / .. .
p u bl ic vo i d wr i teTo ( Writ er d e s t i n a t i o n ) t h r o w s I O E x c e p t i o n {
It er ato r mov i eIte r ator = m o v i e s . i t e r a t o r ( ) ;
wh il e ( m ovie I tera t or.h asNe x t ( ) ) {
Mo vie movi e ToWr i te = (Mov i e ) m o v i e I t e r a t o r . n e x t ( ) ; ;
mo vie T oWri t e.wr i teTo (des t i n a t i o n ) ;
}
}
p ub l ic c las s Movi e {
/ / .. .
p u bl ic vo i d wr i teTo ( Writ er d e s t i n a t i o n ) {
de st ina t ion. w rite ( getN ame( ) ) ;
de st ina t ion. w rite ( '|') ;
de st ina t ion. w rite ( getC ateg o r y ( ) . t o S t r i n g ( ) ) ;
de st ina t ion. w rite ( '|') ;c
tr y {
de sti n atio n .wri t e(In tege r . t o S t r i n g ( g e t R a t i n g ( ) ) ) ;
} ca tch (Unr a tedE x cept ion e x ) {
de sti n atio n .wri t e("- 1");
}

de st ina t ion. w rite ( '\n' );

This can also be a common problem with inheritance, where subclasses know more than they should
about the implementation details of their ancestors. To deal with this we can decouple the relationship
by replacing the inheritance with delegation or by making the details of the ancestors private.

Large Class

If we find a class that is disproportionately larger than most of the other classes in the system, look
carefully at it. Why is it so large? Does it try to do too much? Does it know too much? Is much of the
behavior conditional? If so we might be able to extract subclasses and use polymorphism to see that the
appropriate code is executed. If there are definite sets of subfunctionality they may be able to be
extracted into classes of their own.
There are two ways to quickly find classes that might be too large:
1. Run a lines-of-code metric and look for inordinately large results. See Figure 2.1.

Figure 2.1. Finding a large class with a LOC metric.

2. Visualize the code as UML and look for large classes. See Figure 2.2.

Figure 2.2. Finding a large class by visualizing in UML.

Lazy Class
This is the opposite of Large Class. A Lazy Class doesn't pull its weight and should be merged with
another class as appropriate. You can use the same techniques as you do when looking for large classes,
but look for the other extreme, that is, unusually small classes.

Long Method
Just as an overly large class is a problem, so is an overly large method. Extracting functionally cohesive
blocks of code into their own methods reduces the size of the method and makes the code more
understandable. What's long? If you look at line count, then anything more than 10 lines, plus or minus.
Another thing to consider is cognitive lengthhow many things is the method trying to do? In this case,
long is anything more than one. Consider the following example of a method that isn't really long in
terms of line count (it's borderline), but it's doing four different things. See page 29 to see how to
refactor it.
p ub l ic v oid init ( ) {
g e tC on ten t Pane ( ).se t Layo ut(n e w F l o w L a y o u t ( ) ) ;
m o vi eL ist = ne w JLi s t(my Edit o r . g e t M o v i e s ( ) ) ;
J S cr ol lPa n e sc r olle r = n ew J S c r o l l P a n e ( m o v i e L i s t ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;
m o vi eF iel d = n e w JT e xtFi eld( 1 6 ) ;
g e tC on ten t Pane ( ).ad d (mov ieFi e l d ) ;
a d dB ut ton = ne w JBu t ton( "Add " ) ;
a d dB ut ton . addA c tion L iste ner( n e w A c t i o n L i s t e n e r ( ) {
pu bl ic v oid a ctio n Perf orme d ( A c t i o n E v e n t e ) {

my Edi t or.a d d(mo v ieFi eld. g e t T e x t ( ) ) ;


mo vie L ist. s etLi s tDat a(my E d i t o r . g e t M o v i e s ( ) ) ;

}
});
g e tC on ten t Pane ( ).ad d (add Butt o n ) ;

Switch Statements
The free use of switch statements (aka case statements) indicate a lack of deep understanding of objectoriented principles. Switch statements often result in Shotgun Surgery. We can often use polymorphism
to accomplish the same goal in a much better, cleaner way.
Here's an example that we'll see refactored on page 32:
p ub l ic c las s Emp l oyee {
/ / 0 - en g inee r , 1 - sal esma n , 2 - m a n a g e r
p r iv at e i n t em p loye e Type ;

p u bl ic St r ing d epar t ment Name ( ) {


sw it ch ( empl o yeeT y pe) {
ca se 0 :
ret u rn " E ngin e erin g";
ca se 1 :
re tur n "Sa l es";
ca se 2:
re tur n "Ma n agem e nt";
de fa ult :
re tur n "Un k nown " ;
}
}

Shotgun Surgery
This isn't so much a smell from the code, but more from how we work with the code. The problem is
evidenced when we need to make a single functional change and find that we need to make changes to
code in several places. An example is having to add a clause in switch statements in several methods.
This is a disaster waiting to happen. The solution is to move things that change together to the same
place. This can often be done by using the Replace Conditional with Polymorphism refactoring (see page
32).

Amazon

Prev

don't be afraid of buying books

Next

HOW TO REFACTOR
First of all, we really need to have automated tests in place that will give us feedback as to whether
we've broken anything as we are refactoring. The key to refactoring is that we don't want to change
behavior, and having tests that verify that behavior will let us know as soon as it has changed.
Refactoring is done in small steps, running the tests after each one. That way we know as soon as we
have broken something. By taking small steps, we will know exactly what caused the break: the last
step we took. We must back it out and try again.
It really helps if we can use a good refactoring tool. The original refactoring tool was the refactoring
browser for Smalltalk[URL 38]. There are several tools available for Java that provide refactoring
support. Some are plugins for IDEs (such as jFactor[URL 39] and RefactorIt[URL 40]). Some IDEs are
starting to include refactoring support as core features. These include Eclipse[URL 32] (which has
included refactoring support from even early builds), IDEA[URL 33], Together[URL 34], and jBuilder[URL
35].
Refactoring has been done almost exclusively in source code, but it doesn't need to be limited to that.
Working with code using a UML tool (specifically class and sequence diagrams) has advantages in some
cases. Some smells are easy or easier to find when we can see the system graphically. Likewise, some
refactorings can be easily performed on a class diagram. See [5], [11] for more information.

Amazon

Prev

don't be afraid of buying books

Next

SOME IMPORTANT REFACTORINGS


OK, time for Dave's ten favorite refactorings. Refactorings are like design patterns: the more we know,
and the better we know them, the more benefit we will get from them. The following are, in my
experience, some of the most useful and beneficial refactorings. Consider them a place to start, because,
as with patterns, more are being codified regularly.

Extract Class
When a class gets too big, or its behavior is unfocused, we need to split it into pieces that have
cohesive behavior, creating new classes as required. Extract Class deals with extracting one of these
sets of behavior into a new class. Another reason why we might want to use this refactoring is if we will
need multiple implementations of some behavior. In this case, we can extract the variable code into a
separate class. Once there, we can extract an interface and then go about writing the required
implementations.
In the following example, the code to write a list of movies is in the M o v i e L i s t and M o v i e classes:
p ub l ic c las s Mov i eLis t {
/ / .. .
p u bl ic vo i d wr i teTo ( Writ er d e s t i n a t i o n ) t h r o w s I O E x c e p t i o n {
It er ato r mov i eIte r ator = m o v i e s . i t e r a t o r ( ) ;
wh il e ( m ovie I tera t or.h asNe x t ( ) ) {
Mo vie movi e ToWr i te = (Mov i e ) m o v i e I t e r a t o r . n e x t ( ) ; ;
mo vie T oWri t e.wr i teTo (des t i n a t i o n ) ;
}
}
p ub l ic c las s Movi e {
/ / .. .
p u bl ic vo i d wr i teTo ( Writ er d e s t i n a t i o n ) {
de st ina t ion. w rite ( getN ame( ) ) ;
de st ina t ion. w rite ( '|') ;
de st ina t ion. w rite ( getC ateg o r y ( ) . t o S t r i n g ( ) ) ;
de st ina t ion. w rite ( '|') ;
tr y {
de sti n atio n .wri t e(In tege r . t o S t r i n g ( g e t R a t i n g ( ) ) ) ;

} ca tch (Unr a tedE x cept ion e x ) {


de sti n atio n .wri t e("- 1");
}
de st ina t ion. w rite ( '\n' );

We'd like to extract the writing functionality to a separate class so that it is easier to replace or
enhance. Rather than have it spread over two classes, we'd like it in one place:
p ub l ic c las s Mov i eLis t Writ er
{
W r it er de s tina t ion = nul l;
p u bl ic Mo v ieLi s tWri t er(W rite r a W r i t e r ) {
de st ina t ion = aWr i ter;
}

p u bl ic vo i d wr i teMo v ieLi st(M o v i e L i s t a L i s t ) t h r o w s I O E x c e p t i o n {


It er ato r mov i eIte r ator = a L i s t . g e t M o v i e s ( ) . i t e r a t o r ( ) ;
wh il e ( m ovie I tera t or.h asNe x t ( ) ) {
Mo vie movi e ToWr i te = (Mo v i e ) m o v i e I t e r a t o r . n e x t ( ) ; ;
wr ite M ovie ( movi e ToWr ite) ;
}

p r iv at e v o id w r iteM o vie( Movi e a M o v i e )


de st ina t ion. w rite ( aMov ie.g e t N a m e ( ) ) ;
de st ina t ion. w rite ( '|') ;
de st ina t ion. w rite ( aMov ie.g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
de st ina t ion. w rite ( '|') ;
tr y {
de sti n atio n .wri t e(In tege r . t o S t r i n g ( a M o v i e . g e t R a t i n g ( ) ) ) ;

} ca tch (Unr a tedE x cept ion e x ) {


de sti n atio n .wri t e("- 1");
}
de st ina t ion. w rite ( '\n' );

Now, if we need to change the format of the output, it's all in one place. Also, notice that we've
simplified the public interface in the process. Now only the top level call (i.e., w r i t e M o v i e L i s t ( ) ) is
exposed. The details are now hidden.

Extract Interface
We might want to extract an interface for several reasons. We may want to abstract away from a
concrete implementation so that we can use a technique like Mock Objects more easily. It is often
advantageous to have interfaces defining the important behavior groups in a system.
Suppose we had a class that implements the management of a list of movies called M o v i e L i s t .
Asweare going about our development we get to a point where we are developing a class that is
responsible for being a bridge between a user interface and the movie list. To better isolate that new
class we may want to mock (see Chapter 7) the movie list. Our existing class (M o v i e L i s t ) is concrete,
but to make a mock we should have an interface (and sometimes we need one, e.g., if we want to use
EasyMock). So, we need to extract an interface from M o v i e L i s t . It's generally a good idea to keep
interfaces small and focused, so only extract what you need and/or what makes sense.
Here's the class; it's still simple, but we'd like to be able to mock it:
p ub l ic c las s Mov i eLis t {
p r iv at e C o llec t ion m ovie s = n e w A r r a y L i s t ( ) ;
p u bl ic in t siz e () {
re tu rn m ovie s .siz e ();
}
p u bl ic vo i d ad d (Mov i e mo vieT o A d d ) {
mo vi es. a dd(m o vieT o Add) ;
}

p u bl ic bo o lean c onta i ns(M ovie m o v i e T o C h e c k F o r ) {


re tu rn m ovie s .con t ains (mov i e T o C h e c k F o r ) ;
}

To extract an interface, we create an interface with abstract methods corresponding to those in the
concrete class that are of interest. In this case, we're interested in all the methods in M o v i e L i s t :
p ub l ic i nte r face IMov i eLis t {
i n t si ze( ) ;

v o id a dd( M ovie movi e ToAd d);


b o ol ea n c o ntai n s(Mo v ie m ovie T o C h e c k F o r ) ;

Now we change Mo v ieL is t to implement our new interface:


p ub l ic c las s Mov i eLis t imp leme n t s I M o v i e L i s t {
/ / .. .
}

The final step is to change references to M o v i e L i s t to be references to I M o v i e L i s t . We can now


easily create other implementations of the I M o v i e L i s t interface, including a mock.
Choosing names for interfaces can be difficult and controversial. A well
accepted best- practice is to use an adjective to name interfaces
whenever possible and meaningful. So, we have interfaces like
Ser i aliz a ble, R un n a b l e , and R e m o t e . Note that interfaces will
often be named with adjectives ending in able or ible. Sometimes
that doesn't make sense. A second best strategy is to prefix the
interface name with I or Int. In our example, M o v i e L i s t a b l e
doesn't make much sense, so I went with I M o v i e L i s t . This is often
typical when you are extracting an interface to make mocking easier
and/or possible.

Extract Method
When a method gets too long or the logic is too complex to be easily understood, part of it can be
pulled out into a method of its own. Here's another example from the code we will see in Chapter 8.
Here's some code that builds a user interface:
p ub l ic v oid init ( ) {
g e tC on ten t Pane ( ).se t Layo ut(n e w F l o w L a y o u t ( ) ) ;
m o vi eL ist = ne w JLi s t(my Edit o r . g e t M o v i e s ( ) ) ;
J S cr ol lPa n e sc r olle r = n ew J S c r o l l P a n e ( m o v i e L i s t ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;
m o vi eF iel d = n e w JT e xtFi eld( 1 6 ) ;
g e tC on ten t Pane ( ).ad d (mov ieFi e l d ) ;
a d dB ut ton = ne w JBu t ton( "Add " ) ;
a d dB ut ton . addA c tion L iste ner( n e w A c t i o n L i s t e n e r ( ) {
pu bl ic v oid a ctio n Perf orme d ( A c t i o n E v e n t e ) {
my Edi t or.a d d(mo v ieFi eld. g e t T e x t ( ) ) ;
mo vie L ist. s etLi s tDat a(my E d i t o r . g e t M o v i e s ( ) ) ;
}
});
g e tC on ten t Pane ( ).ad d (add Butt o n ) ;
}

This method does several different things. It sets a layout and it creates and adds three components: a
list, a field, and a button. That's rather busy. To make the situation more extreme, I've made sure there
are no blank lines to split up the different bits of functionality. One approach would be to add blank lines
and explanatory comments, like this:
p ub l ic v oid init ( ) {
/ / s et th e lay o ut
g e tC on ten t Pane ( ).se t Layo ut(n e w F l o w L a y o u t ( ) ) ;

/ / c re ate the l ist


m o vi eL ist = ne w JLi s t(my Edit o r . g e t M o v i e s ( ) ) ;
J S cr ol lPa n e sc r olle r = n ew J S c r o l l P a n e ( m o v i e L i s t ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;
/ / c re ate the f ield
m o vi eF iel d = n e w JT e xtFi eld( 1 6 ) ;
g e tC on ten t Pane ( ).ad d (mov ieFi e l d ) ;

/ / c re ate the a dd b u tton


a d dB ut ton = ne w JBu t ton( "Add " ) ;
a d dB ut ton . addA c tion L iste ner( n e w A c t i o n L i s t e n e r ( ) {
pu bl ic v oid a ctio n Perf orme d ( A c t i o n E v e n t e ) {
my Edi t or.a d d(mo v ieFi eld. g e t T e x t ( ) ) ;
mo vie L ist. s etLi s tDat a(my E d i t o r . g e t M o v i e s ( ) ) ;
}
});
g e tC on ten t Pane ( ).ad d (add Butt o n ) ;

Is that any better? Maybe a little, but the method is even longer! All we've really done is try to mask
the smell with some deodorant comments. A better solution is to use Extract Method to pull each of
the different bits of functionality out into its own method:
p ub l ic v oid init ( ) {
s e tL ay out ( );
i n it Mo vie L ist( ) ;
i n it Mo vie F ield ( );
i n it Ad dBu t ton( ) ;
}
p ri v at e voi d set L ayou t () {
g e tC on ten t Pane ( ).se t Layo ut(n e w F l o w L a y o u t ( ) ) ;
}
p ri v at e voi d ini t Movi e List () {
m o vi eL ist = ne w JLi s t(ge tMov i e s ( ) ) ;
J S cr ol lPa n e sc r olle r = n ew J S c r o l l P a n e ( m o v i e L i s t ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;
}
p ri v at e voi d ini t Movi e Fiel d() {
m o vi eF iel d = n e w JT e xtFi eld( 1 6 ) ;
g e tC on ten t Pane ( ).ad d (mov ieFi e l d ) ;
}

p ri v at e voi d ini t AddB u tton () {


a d dB ut ton = ne w JBu t ton( "Add " ) ;
a d dB ut ton . addA c tion L iste ner( n e w A c t i o n L i s t e n e r ( ) {
pu bl ic v oid a ctio n Perf orme d ( A c t i o n E v e n t e ) {
my Edi t or.a d d(mo v ieFi eld. g e t T e x t ( ) ) ;
mo vie L ist. s etLi s tDat a(ge t M o v i e s ( ) ) ;
}
});
g e tC on ten t Pane ( ).ad d (add Butt o n ) ;
}

Notice how much clearer this is. Each functional unit is in its own method. The i n i t ( ) method is not
extremely clear, but from the decomposition and naming it is self-evident what is happening. For details

on any one aspect, you can simply look at the appropriate method.

Replace Type Code with Subclasses


We can use this refactoring when we have a class that indicates subtypes using a type-code (e.g., an
employee is either an Engineer or a Salesman). We make a subclass for each alternative. Doing this will
often help break up complex conditionals and switch statements that decide based on the type code.
Here's a simple example:
p ub l ic c las s Emp l oyee {
/ / 0 - en g inee r , 1 - sal esma n , 2 - m a n a g e r
p r iv at e i n t em p loye e Type ;
/ / ..
}

This isn't very object-oriented. Replacing e m p l o y e e T y p e with subclasses we get the following code,
which is also shown as UML in Figure 2.3:
a bs t ra ct pu b lic c lass Empl oyee {
//. . .
}
p ub l ic c las s Eng i neer exte nds E m p l o y e e {
//. . .
}
p ub l ic c las s Sal e sman exte nds E m p l o y e e
{
//. . .
}
p ub l ic c las s Man a ger e xten ds E m p l o y e e
{
//. . .
}

Figure 2.3. Replacing a type code with subclasses.

Replace Conditional with Polymorphism


When we find switch statements, consider creating subclasses to handle the different cases and get rid
of the switch. If there are subclasses already, maybe the conditional behavior can be pushed into them?
We can revisit the previous example for an illustration. Here's more of the original code:
p ub l ic c las s Emp l oyee {
/ / 0 - en g inee r , 1 - sal esma n , 2 - m a n a g e r
p r iv at e i n t em p loye e Type ;

p u bl ic St r ing d epar t ment Name ( ) {


sw it ch ( empl o yeeT y pe) {
ca se 0 :
ret u rn " E ngin e erin g";
ca se 1 :
ret u rn " S ales " ;
ca se 2 :
ret u rn " M anag e ment ";
de fau l t:
ret u rn " U nkno w n";
}
}

After extracting subclasses for each type of employee (as in the previous example) we can use
polymorphism to replace the switch statement in d e p a r t m e n t N a m e ( ) :
a bs t ra ct pu b lic c lass Empl oyee {
p u bl ic ab s trac t Str i ng d epar t m e n t N a m e ( ) ;
}
p ub l ic c las s Eng i neer exte nds E m p l o y e e {
p u bl ic St r ing d epar t ment Name ( ) {
re tu rn " Engi n eeri n g";
}
}
p ub l ic c las s Sal e sman exte nds E m p l o y e e {
p u bl ic St r ing d epar t ment Name ( ) {
re tu rn " Sale s ";
}
}
p ub l ic c las s Man a ger e xten ds E m p l o y e e {
p u bl ic St r ing d epar t ment Name ( ) {
re tu rn " Mana g emen t ";
}
}

Form Template Method


We can use this when we have a similar method in multiple classes that have a common structure but
different details. We want to end up with a method having the common structure in the superclass
(which may have to be created) and the extracted detail methods in subclasses. Polymorphism takes
care of calling the proper detail methods.
We'll reuse our employment example again, with a different twist. Here are methods that compute an
XML representation of employees:

p ub l ic c las s Eng i neer exte nds E m p l o y e e {


p u bl ic St r ing a sXML ( ) {
St ri ngB u ffer buf = new Str i n g B u f f e r ( ) ;
bu f. app e nd(" < empl o yee name = \ " " ) ;
bu f. app e nd(g e tNam e ());
bu f. app e nd(" \ " de p artm ent= \ " E n g i n e e r i n g \ " > " ) ;
// .. .
re tu rn b uf.t o Stri n g();
}
//. . .
}

p ub l ic c las s Sal e sman exte nds E m p l o y e e {


p u bl ic St r ing a sXML ( ) {
St ri ngB u fer b uf = new Stri n g B u f f e r ( ) ;
bu f. app e nd(" < empl o yee name = \ " " ) ;
bu f. app e nd(g e tNam e ());
bu f. app e nd(" \ " de p artm ent= \ " S a l e s \ " > " ) ;
// . . .
re tu rn b uf.t o Stri n g();
}
/ / .. .
}
p ub l ic c las s Man a ger e xten ds E m p l o y e e {
p u bl ic St r ing a sXML ( ) {
St ri ngB u fer b uf = new Stri n g B u f f e r ( ) ;
bu f. app e nd(" < empl o yee name = \ " " ) ;
bu f. app e nd(g e tNam e ());
bu f. app e nd(" \ " de p artm ent= \ " M a n a g e m e n t \ " > " ) ;
// . . .
re tu rn b uf.t o Stri n g();
}
/ / .. .
}

Notice how all three methods are almost identical. This is a sure indication that refactoring is required.
To refactor to a template method, we first push one of the methods up to the superclass, in this case
that is E mpl o yee:
p ub l ic c las s Emp l oyee {
p u bl ic St r ing a sXML ( ) {
St ri ngB u fer b uf = new Stri n g B u f e r ( ) ;
bu f. app e nd(" < empl o yee name = \ " " ) ;
bu f. app e nd(g e tNam e ());
bu f. app e nd(" \ " de p artm ent= \ " E n g i n e e r i n g \ " > " ) ;
// . . .
re tu rn b uf.t o Stri n g();
}
/ / .. .
}

Next, we extract the differences into separate methods. In this case that's the department name. See
the description of Replace Conditional with Polymorphism on page 32 on how this is done. Now, given
that we have d epar t ment N ame( ) methods in place, the next step is to call that polymorphic method
from the template method in E mplo yee :
p ub l ic c las s Emp l oyee {
p u bl ic St r ing a sXML ( ) {
St ri ngB u ffer buf = new Str i n g B u f f e r ( ) ;
bu f. app e nd(" < empl o yee name = \ " " ) ;
bu f. app e nd(g e tNam e ());
bu f. app e nd(" \ " de p artm ent= \ " " ) ;
bu f. app e nd(d e part m entN ame( ) ) ;
bu f. app e nd(" \ ">") ;
// .. .

// .. .
re tu rn b uf.t o Stri n g();

}
//. . .

Finally, delete the a sXM L () methods in each of the E m p l o y e e subclasses. All the tests should pass as
before.

Introduce Explaining Variable


When we have a complex expression that is difficult to understand, we can extract parts of it and store
the intermediate results in well-named temporary variables. This breaks the expression into easy-tounderstand pieces, as well as making the overall expression clearer.
Consider the following:
p ub l ic M one y cal c ulat e Tota l() {
r e tu rn ge t Subt o tal( ) .plu s((g e t T a x a b l e S u b t o t a l ( ) . t i m e s ( 0 . 1 5 ) ) )
.min us(( g e t S u b t o t a l ( ) . a s D o u b l e ( )
> 1 0 0. 0)
?(getSubtotal().times(0.10))
:0);

The above code is quite simple, but it could certainly be clearer. There's also a potential performance
issue since get S ubto t al() is called repeatedly. We can refactor this, breaking the complex expression
up into simpler pieces and using well-named variables to better communicate the intent:
p ub l ic M one y cal c ulat e Tota l() {
M o ne y sub t otal = ge t Subt otal ( ) ;
M o ne y tax = ge t Taxa b leSu btot a l ( ) . t i m e s ( 0 . 1 5 ) ;
M o ne y tot a l =s u btot a l.pl us(t a x ) ;
b o ol ea n q u alif i esFo r Disc ount = g e t S u b t o t a l ( ) . a s D o u b l e ( )
> 1 0 0. 0;
M o ne y dis c ount = qu a lifi esFo r D i s c o u n t
? subt o t a l . t i m e s ( 0 . 1 0 )
: newM o n e y ( 0 . 0 ) ;
r e tu rn to t al.m i nus( d isco unt) ;

Replace Constructor with Factory Method


If we have several constructors that create different flavors of the class, it can be confusing since, in
Java and C++ (as opposed to Smalltalk), all constructors have the same name. Instead of using hardcoded constructors, we can use static factory methods. This lets us give each one a meaningful name.
We'll use the following as an example:
p ub l ic c las s Rati n g {
p r iv at e i n t va l ue = 0;
p r iv at e S t ring sour c e = null ;
p r iv at e S t ring revi e w = null ;
p u bl ic Ra t ing( i nt a R atin g) {
th is (aR a ting , "An o nymo us", " " ) ;
}

p u bl ic Ra t ing( i nt a R atin g, S t r i n g a R a t i n g S o u r c e ) {
th is (aR a ting , aRa t ingS ourc e , " " ) ;
}
p u bl ic Ra t ing( i nt a R atin g, S t r i n g a R a t i n g S o u r c e , S t r i n g a R e v i e w ) {
va lu e = aRat i ng;
so ur ce = aRa t ingS o urce ;
re vi ew = aRe v iew;
}
}

/ / .. .

This isn't too bad. We're chaining constructors to provide default arguments to the next more specific
one. In this way the functionality of the constructor is implemented only in the most specific one.
However, we can communicate the intent more clearly by refactoring to convert these to factory
methods:
p ub l ic s tat i c Ra t ing n ewAn onym o u s R a t i n g ( i n t v a l u e ) {
r e tu rn ne w Rat i ng(v a lue, "An o n y m o u s " , " " ) ;
}
p ub l ic s tat i c Ra t ing n ewRa ting ( i n t v a l u e , S t r i n g s o u r c e ) {
r e tu rn ne w Rat i ng(v a lue, sou r c e , " " ) ;
}
p ub l ic s tat i c Ra t ing n ewRe view ( i n t v a l u e , S t r i n g s o u r c e , S t r i n g r e v i e w ) {
r e tu rn ne w Rat i ng(v a lue, sou r c e , r e v i e w ) ;
}
p ri v at e Rat i ng(i n t aR a ting , St r i n g a R a t i n g S o u r c e , S t r i n g a R e v i e w ) {
v a lu e = a R atin g ;
s o ur ce = a Rati n gSou r ce;
r e vi ew = a Revi e w;
}

Part of refactoring this is changing all references to use the factory methods. When testsall pass again
we can delete the unnecessary constructors and mark the ones that remain as p r i v a t e . For example,
constructor calls such as:
s ta r Wa rs .ad d Rati n g(ne w Rat ing( 2 ) ) ;
s ta r Wa rs .ad d Rati n g(ne w Rat ing( 4 , " N Y T i m e s " ) ) ;
s ta r Wa rs .ad d Rati n g(ne w Rat ing( 5 , " J a s o n " , " A r e a l l y f u n m o v i e ! " ) ) ;

get changed to calls to a factory method:


s ta r Wa rs .ad d Rati n g(Ra t ing. newA n o n y m o u s R a t i n g ( 2 ) ) ;
s ta r Wa rs .ad d Rati n g(Ra t ing. newR a t i n g ( 4 , " N Y T i m e s " ) ) ;
s ta r Wa rs .ad d Rati n g(Ra t ing. newR e v i e w ( 5 , " J a s o n " , " A r e a l l y f u n m o v i e ! " ) ) ;

Making the constructors private also gives us more control over where and how instances are created.

Replace Inheritance with Delegation


Inheritance should only be used when the subclasses are special kinds of the superclass, or they extend
the superclass and not just override parts of it[12]. If inheritance is being used just to reuse some of the
capabilities of the superclass (e.g., subclassing V e c t o r to be able to store objects sequentially), it
should be replaced with delegation by making the V e c t o r an instance variable and using it to store the
objects.
For example, consider this code for a class representing a company department that contains a
collection of employees:
p ub l ic c las s Depa r tmen t ext ends V e c t o r {
}

The idea here is to get for free the collection maintenance support from V e c t o r . Big mistake. There is
no type-safety at all. Anything can be added to the department, and anything coming out of it has to be
cast to Em pl oyee . Consider the following code:
p ub l ic c las s Com p any {
p r iv at e H a shMa p dep a rtme nts = n e w H a s h M a p ( ) ;
p ub l ic C omp a ny() {
d e pa rt men t s.pu t ("En g inee ring " , n e w D e p a r t m e n t ( ) ) ;
d e pa rt men t s.pu t ("Sa l es", new D e p a r t m e n t ( ) ) ;
}
p ub l ic D epa r tmen t get D epar tmen t N a m e d ( S t r i n g d e p a r t m e n t N a m e ) {
r e tu rn (D e part m ent) d epar tmen t s . g e t ( d e p a r t m e n t N a m e ) ;
}

p ub l ic i nt r ollC a ll() {
i n t ab sen t ees = 0;
C o ll ec tio n all D epar t ment s = d e p a r t m e n t s . v a l u e s ( ) ;
I t er at or d eptI t erat o r = allD e p a r t m e n t s . i t e r a t o r ( ) ;
w h il e (de p tIte r ator . hasN ext( ) ) {
De pa rtm e nt a D epar t ment = ( D e p a r t m e n t ) d e p t I t e r a t o r . n e x t ( ) ;
De pa rtm e nt.D e part m entI tera t o r e m p l o y e e s =
aD ep art m ent. i tera t or() ;

wh il e ( e mplo y ees. h asNe xt() )


Em plo y ee s o mebo d y = empl o y e e s . n e x t ( ) ;
if (! s omeb o dy.a t Work ()) {
abs e ntee s ++;
}

}
}
r e tu rn ab s ente e s;
}

Not a very good design. Any code can get a department object and add anything it wants to it. By
extending Ve ctor to reuse the collection implementation we've totally discarded any control over what
goes into Dep a rtme n t. Talk about equal-opportunity hiring!
What can we do? Well, the first step would be to refactor out the collection management and provide a
type-safe iterator:
p ub l ic c las s Dep a rtme n t {
p r iv at e V e ctor empl o yees = n e w V e c t o r ( ) ;
p u bl ic vo i d hi r e(Em p loye e ne w H i r e ) {
em pl oye e s.ad d (new H ire) ;

em pl oye e s.ad d (new H ire) ;

p u bl ic De p artm e ntIt e rato r it e r a t o r ( ) {


re tu rn n ew D e part m entI tera t o r ( ) ;
}
p u bl ic cl a ss D e part m entI tera t o r {
It er ato r und e rlyi n g = empl o y e e s . i t e r a t o r ( ) ;
pu bl ic b oole a n ha s Next () {
re tur n und e rlyi n g.ha sNex t ( ) ;
}

pu bl ic E mplo y ee n e xt() {
re tur n (Em p loye e )und erly i n g . n e x t ( ) ;
}

Now the only way to add to the collection is to use the h i r e ( ) method. This provides type-safety.
Since we now have that, we can create the customer iterator to encapsulate the cast.

Replace Magic Number with Symbolic Constant


Having hard-coded literal values embedded in code is a very bad thing. They are harder to see,
changing them is shotgun surgery, and it's blatant duplication. We can use a well-named symbolic
constant instead. Then when it has to change, it's only in one place. This is really more general, and
applies to any literal values, such as strings.
Consider this code:
p ub l ic v oid test A dd() {
V e ct or mo v ieNa m esWi t hAdd itio n = n e w V e c t o r ( m o v i e N a m e s ) ;
m o vi eN ame s With A ddit i on.a dd(" L o s t I n S p a c e " ) ;
M o ck Co ntr o l co n trol = Ea syMo c k . c o n t r o l F o r ( M o v i e L i s t E d i t o r . c l a s s ) ;
M o vi eL ist E dito r moc k Edit or = ( M o v i e L i s t E d i t o r ) c o n t r o l . g e t M o c k ( ) ;
m o ck Ed ito r .get M ovie s ();
c o nt ro l.s e tRet u rnVa l ue(m ovie N a m e s , 1 ) ;
m o ck Ed ito r .add ( "Los t In Spac e " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Ed ito r .get M ovie s ();
c o nt ro l.s e tRet u rnVa l ue(m ovie N a m e s W i t h A d d i t i o n , 1 ) ;
c o nt ro l.a c tiva t e();
M o vi eL ist W indo w win d ow = new M o v i e L i s t W i n d o w ( m o c k E d i t o r ) ;
w i nd ow .in i t();
w i nd ow .sh o w();
J T ex tF iel d mov i eFie l d = wind o w . g e t M o v i e F i e l d ( ) ;
m o vi eF iel d .set T ext( " Lost In S p a c e " ) ;
J B ut to n a d dBut t on = wind ow.g e t A d d B u t t o n ( ) ;
a d dB ut ton . doCl i ck() ;
J L is t mov i eLis t = w i ndow .get M o v i e L i s t ( ) ;

L i st Mo del movi e List M odel = m o v i e L i s t . g e t M o d e l ( ) ;


a s se rt Equ a ls(" M ovie list is t h e w r o n g s i z e a f t e r a d d " ,
m o vieN a mesW ithA d d i t i o n . s i z e ( ) ,
m o vieL i stMo del. g e t S i z e ( ) ) ;
a s se rt Equ a ls(" M ovie list doe s n ' t c o n t a i n n e w n a m e " ,
" L ost I n Sp ace" ,
m o vieL i stMo del. g e t E l e m e n t A t ( m o v i e N a m e s . s i z e ( ) ) ) ;
}

c o nt ro l.v e rify ( );

The smell here is the duplication of the literal string" L o s t I n S p a c e " . We start by introducing a
constant to hold the value:
p ri v at e sta t ic f i nal S trin g LO S T I N S P A C E = " L o s t I n S p a c e " ;

Then we replace every occurrence of the literal string with L O S T I N S P A C E . The resulting code is:
p ub l ic v oid test A dd() {
V e ct or mo v ieNa m esWi t hAdd itio n = n e w V e c t o r ( m o v i e N a m e s ) ;
m o vi eN ame s With A ddit i on.a dd(L O S T I N S P A C E ) ;
M o ck Co ntr o l co n trol = Ea syMo c k . c o n t r o l F o r ( M o v i e L i s t E d i t o r . c l a s s ) ;
M o vi eL ist E dito r moc k Edit or = ( M o v i e L i s t E d i t o r ) c o n t r o l . g e t M o c k ( ) ;
m o ck Ed ito r .get M ovie s ();
c o nt ro l.s e tRet u rnVa l ue(m ovie N a m e s , 1 ) ;
m o ck Ed ito r .add ( LOST IN S PACE ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Ed ito r .get M ovie s ();
c o nt ro l.s e tRet u rnVa l ue(m ovie N a m e s W i t h A d d i t i o n , 1 ) ;
c o nt ro l.a c tiva t e();
M o vi eL ist W indo w win d ow = new M o v i e L i s t W i n d o w ( m o c k E d i t o r ) ;
w i nd ow .in i t();
w i nd ow .sh o w();
J T ex tF iel d mov i eFie l d = wind o w . g e t M o v i e F i e l d ( ) ;
m o vi eF iel d .set T ext( L OST IN S P A C E ) ;
J B ut to n a d dBut t on = wind ow.g e t A d d B u t t o n ( ) ;
a d dB ut ton . doCl i ck() ;
J L is t mov i eLis t = w i ndow .get M o v i e L i s t ( ) ;
L i st Mo del movi e List M odel = m o v i e L i s t . g e t M o d e l ( ) ;
a s se rt Equ a ls(" M ovie list is t h e w r o n g s i z e a f t e r a d d " ,
m o vieN a mesW ithA d d i t i o n . s i z e ( ) ,
m o vieL i stMo del. g e t S i z e ( ) ) ;
a s se rt Equ a ls(" M ovie list doe s n ' t c o n t a i n n e w n a m e " ,
L O ST I N SPA CE,
m o vieL i stMo del. g e t E l e m e n t A t ( m o v i e N a m e s . s i z e ( ) ) ) ;
}

c o nt ro l.v e rify ( );

This provides several benefits, including:

the literal value appears only once, so only one change is needed if the value needs to change.
all uses of the string are synchronized, so we no longer need to worry about references being
the same.
Replace Nested Conditional with Guard Clauses I know I said this was my ten favorite refactorings, but I
thought I'd throw in a bonus.
Many people have been taught that a method should have only a single exit point (i.e., r e t u r n
statement). There is no valid reason for this, certainly not at the expense of clarity. In a method that
should exit under multiple conditions, this leads to complex, nested conditional statements. A better, and
much clearer, alternative is to use guard clauses to return under those conditions.
Here's a simple example:
p ub l ic i nt f ib(i n t i) {
i n t re sul t ;
i f ( i == 0 ) {
re su lt = 0;
} el se if (i
< =2 ) {
re su lt = 1;
} el se {
re su lt = fib ( i
- 1 ) + f ib( i
- 2);
}
} r et ur n r e sult ;
}

It's correct, but very ugly and crowded. And the conditional isn't even deeply nested. Now let's refactor
it to use guard clauses instead of the conditional:
p ub l ic i nt f ib(i n t i) {
i f ( i == 0 ) re t urn 0 ;
if (i
< =2 ) re tu rn1 ;
r e tu rn fi b (i
- 1 ) + f ib( i
- 2);
}

This is one case where I will drop the braces surrounding the body of
an if: guard clauses that perform one very simple action, typically a
re t urn. The intent is revealed much more by using this format
rather than the more canonical:
pu b lic i nt f ib(i n t i ) {
i f (i == 0 ) {
ret u rn 0 ;
}
i f (i
< = 2) {
ret u rn 1 ;
}
r etur n fib (i
- 1 ) + f ib(i
- 2 );
}

Using guard clauses results in code that is much cleaner, shorter, and clearer.

Amazon

Prev

don't be afraid of buying books

Next

REFACTORING TO PATTERNS
Design patterns are distillations of proven design ideas. To be good programmers we should know as
many patterns as possible, and be constantly learning new ones. More than that, we need to know when
to use them and, just as important, when not to.
If we don't know design patterns and/or don't use them, we are at risk of under-engineering. We won't
see similarities as easily, and will find ourselves solving the same problems again and again. Knowing
patterns helps us recognize recurring problems and gives us approaches to solving them.
The danger of design patterns is getting caught up in them. Many programmers overdo it when they
discover design patterns. They begin seeing Composites around every requirement. Worse yet, they
design by using patterns at the outset.
So, how should we be using patterns? The answer(if you haven't guessed yet, then you should reread
this chapter before continuing) is as targets for refactoring. The thing to remember is that we shouldn't
drop patterns into the design in final form, rather we should gradually evolve into using them via
refactoring. . . but only when we need to.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
Now we know a bit about refactoring: what it is, some of the specific techniques, and some of the
indicators that tell us that refactoring may be required.
It is very important to engrain the habit of refactoring and to become proficient at refactoring. Both are
accomplished through practice. We can start by pulling out some recent code and sniffing through it for
code smells with a newly critical nose. Then we can apply specific refactorings as appropriate.
The problem with refactoring working code is that our changes might break it. That's where TDD comes
in. If we are practicing TDD, all of the code is checked by our teststoensure its proper behavior. We
must always refactor as we do any TDD coding: in tiny increments. After making each component
change in a refactoring, we run the tests again to catch any small bugs that might have been
introduced. The level of confidence this gives us must be experienced to be understood.
Pretty soon we will have the courage to continually whittle down our code to the leanest, sparest
possible implementation. As we become proficient at refactoring through TDD, we'll find that we develop
a more discriminating eye (and nose!) for coding excellence, and a deeper level of software craft.

Refactoring Test Code


Just as we need to continually refactor application code to make it cleaner and clearer, we
also need to refactor test code for the same reasons. Here are some things we should keep
in mind:
Always use the appropriate assertion. For example:
- If you are asserting the equality of values, use
a ss ert Eq uals (exp ec te d , ac t u al) rather than a ss e rt Tr u e( ac t ua l
= = exp ec ted) or as se rt T r ue( a c tua l .e q ua ls ( ex p ec te d )) .
- If you are asserting thatavalueis fa l s e, use a ss er t Fa l se (v a lu e)
rather than assertTrue(!value).
Stay alert for opportunities to consolidate duplicate fixture code within test methods
into a single s etup () method.
Stay alert for fixtures that are not used uniformly by all the test methods in the
Tes tCa se . Look for opportunities to split Te s t Ca se s to keep them focused on
specific fixtures.
Stay alert for ways to collect Te s tCa s e s into T e st S ui te s. Make sure that
Tes tCa se s are not themselves serving as Te s t Su it es(organized around structure
or functionality instead of common fixture requirements).
Don't duplicate information in test method and T es tC as e names. Name the
Tes tCa se to reflect the fixture (e.g., Te s t Emp t yL i st ) and the test methods to
reflect the behavior tested (e.g., t e s tSi z e ()).
Look for ways to keep test methods independent of each other. Look for
opportunities to mock state-sensitive resources that bind your test methods
together.
Always look for better ways to organize your hierarchy of Te s t- S ui te s and

Tes tCa se s.
For more on test code smells and test code refactoring, see [39], [40].

Amazon

Prev

don't be afraid of buying books

Next

Chapter 3. PROGRAMMING BY INTENTION


'Tis but thy name that is my enemy;
...
O, be some other name!
Romeo and Juliet, Act 2 Scene 2 William Shakespeare
This chapter discusses programming by intention, a central idea in XP. It means making your intent clear
when you write code.
Have you ever had to work on a piece of code and found that it was hard to understand? Maybe the
logic was convoluted, the identifiers meaningless, or worse, misleading. You say there was
documentation? Was it readable? Did it make sense? Was it up to date with the code? Are you sure?
How do you know?
The main idea of programming by intention is communication, specifically, the communication of our
intent to whomever may be reading the code. The goal is to enable them to understand what we had in
mind when we wrote the code.
Let's look at what we need to do in order to have our code be as understandable and intent-revealing
as possible.

Amazon

Prev

don't be afraid of buying books

Next

NAMES
By names I mean the identifiers we choose to name the various classes, variables, methods, etc., that
we create as we work. We need to choose names that are semantically transparent, that is, they say
what they mean and mean what they say.
When I'm talking about choosing names, I like to pull out the quote from Romeo and Juliet that appears
at the beginning of this chapter. We can use whatever word/name we like to refer to a thing, but if it
does not convey the meaning that we intend then it is an enemy of clarity and may serve to confuse
people who read or work on this code in the future. The lesson here is to use names that make sense. .
. call it what it is.
My oldest daughter used to have a cute, although somewhat annoying, habit of coming up with her own
names for songs and stories that she liked. She would focus on one line from the whole song/story and
derive a name from that. Many nights at bedtime she would ask frantically for a new favorite, while we
went through a game of Twenty Questions to try and deduce what she was talking about. This is a
danger of using non-obvious names for things. Sure, it may make sense to us, but what about everyone
else?
There are several patterns that we can use when choosing names; we discuss them in detail next.
Use nouns or noun phrases for class names. Name classes for what they represent or
what they do:
publ ic cl ass M ovie {
// ...
}
publ ic cl ass M ovie Rati ngCo m p ara t o r i m pl e me nt s C o mp ar a to r {
// ...
}
publ ic cl ass X MLMo vieL istR e a der i mpl e me n ts M o vi e Li st R ea de r {
// ...
}

Use either adjectives or generic nouns and noun phrases for interfaces. Interfaces
are a bit different. If an adjective is used for an interface name it usually ends with able, for example, Runn able , S e ria l i zab l e . My advice is to avoid conventions that
prepend or append "I" to the name when possible. Sometimes there isn't a good name
for an interface other than "interface to something." In that case, IS om e th in g is
acceptable.
publ ic in ter fa ce S eria liza b l e {
}
publ ic in ter fa ce M ovie List W r ite r {
vo id wr ite (M ovie List mov i e Lis t ) th r ow s I OE x ce p ti on ;
}

Use verbs and verb phrases for method names. Methods do something, so using
verbs to name them makes sense. Examples include a dd and fe t ch Re v ie w.
priv ate i nt ca lcul ateA vera g e Rat i n g() {
in t t ot alR at ing = ca lcul a t eTo t a lRa t in g () ;
re tur n tot al Rati ng / rat i n gs. s i ze( ) ;
}

Use accepted conventions for accessors and mutators. Many languages have
generally accepted conventions for how to retrieve and modify instance variables. For
instance, if you are working in Java, you are advised to use the Java Beans conventions
of ge tX and se tX to retrieve/modify a variable named x .
publ ic Ca teg or y ge tCat egor y ( ) {
re tur n cat eg ory;
}
publ ic vo id se tCat egor y(Ca t e gor y aCa t eg o ry ) {
ca teg or y = a Cate gory ;
}
publ ic bo ole an isO fCat egor y ( Cat e g ory aC a te go r y) {
re tur n cat eg ory. equa ls(a C a teg o r y);
}

To access a boolean variable, use i sX in both Java and Smalltalk. An alternative in some
cases is to use the form is X() where X refers to an optional part of the object; for
example, either of the following could be used:
publ ic bo ole an isR ated () {
re tur n !ra ti ngs. isEm pty( ) ;
}
publ ic bo ole an has Rati ng() {
re tur n !ra ti ngs. isEm pty( ) ;
}

Note that this applies to computed properties as well as actual instance variables, for
example, when a circular queue class needs a method to query whether it is full, we
suggest is Ful l.
Sometimes it is clearer to drop the get prefix. For example, I tend to use s iz e rather
than g et Siz e to fetch the size of a composite object. This is often the case when the
value you are asking for is a property of the object as opposed to one of its attributes.
publ ic in t s iz e() {
re tur n mov ie s.si ze() ;
}

In this case, we are also conforming to the convention used in the Java class libraries. It
is a good idea to conform to the naming idioms of the environment you are working in
because that is what others who will be reading your code will be accustomed to.

Don't put redundant information in method names. If I needed a method to add an


instance of X, I would tend to call the method a d d rather than a d dX since the parameter
will be an X. This has the benefit that if later refactoring changes the type of the
argument (even simple renaming of the class), then the method name is out of sync with
its argument. The innocent redundancy has now become misleading and confusing.
Another benefit is that you gain clarity if you need to support adding of different types by
the ability to overload the method name.
publ ic vo id ad d(Mo vie movi e T oAd d ) th r ow s D up l ic a te Mo v ie Ex c ep ti o n {
if (t hi s.c on tain s(mo vieT o A dd) ) {
thr ow ne w Dupl icat eMov i e Exc e p tio n (m o vi eT o Ad d .g et N am e( ) );
}
mo vie s. add (m ovie ToAd d);
}

There are always exceptions. Sometimes it is just clearer, and reads better, if you have
type information in the method name.
publ ic vo id ad dRat ing( Rati n g ra t i ngT o Ad d ) {
ra tin gs .ad d( rati ngTo Add) ;
}

Use nouns and noun phrases for variable names. Variables hold things, so nouns
make sense, for example, rat in g, mo v i es, co n ne c ti on T oR e vi ew S er ve r .
publ ic cl ass M ovie {
pr iva te St ri ng n ame = "" ;
pr iva te Ca te gory cat egor y = C a t ego r y. U NC AT E GO R IZ ED ;
pr iva te Li st rat ings = n u l l;
}

// ...

Choose names carefully, but don't spend too much time at it. Remember, they are easy to change. If
later we decide that a different name would communicate the intent better, it can be changed. That's
one of the most basic refactorings. Using a tool that has automated refactoring support helps.

Amazon

Prev

don't be afraid of buying books

Next

SIMPLICITY
When we are explaining something unfamiliar to someone we speak as simply as we can. Likewise,
when we write code we should write it as simply as we can. Keep in mind that someone will be reading
that code later, trying to understand what it does and how it does it. We should strive to keep our code
as clear and intent-revealing as possible.

Do the Simplest Thing


So how do we keep our code clear? One way is to use simple algorithms as much as possible. Always
assume that the simplest way of doing something is the best. If it proves not to be, we can always
change it. One of the phrases heard a lot in the eXtreme Programing world is "What is The Simplest
Thing That Could Possibly Work?"
"What," you may ask, "does that mean?" Well, consider this simple test:
pub l ic vo id te stE mp tyLi st() {
M o vieLi st em pty Li st = new Mov i e Lis t ( );
a s sertE qua ls (0, e mpty List .siz e ( ));
}

In order to get this test to pass once it is compiling, we do the simplest thing that could possibly work.
Specifically:
pub l ic in t s iz e() {
r e turn 0;
}

When we do this we know that the siz e () method is too simple, but at this point we don't know what
it should be, and returning zero is the simplest thing that makes the test pass. We will have to
generalize this method later, but not until we write tests for non-empty list behavior.

Keep It Simple by Refactoring


Another way to keep code simple and intent-revealing is to change unclear code so that it is clear. This
is one way that refactoring is used. Chapter 2 has a more in-depth discussion of this incredibly
important topic.
There are many refactorings that deal with increasing the intent-revealing quality of a piece of code.
While it could be argued that most of Fowler's refactorings make code clearer, we only consider a
handful here. See [15] and [16] for more information on these and the remainder. The simplest of these
include renaming classes, methods, variables, etc. Some of the more complex intent-revealing
refactorings are explained next.
Introduce Explaining Variable If we have a complex expression, we take parts of it
and put those intermediate values in temporary variables with names that explain the
purpose of the subexpression.

Extract Method If we have a method that is getting overly complex, we make part of it
into a separate method. This is also useful if we see common code in multiple methods:
we extract the common part (possibly parameterized) into a separate method that can
be called by multiple clients.

Extract Class If we have a class that is doing too much or that has multiple
responsibilities, we extract related responsibilities into separate classes.

Replace Temp with Query Rather than computing a value and storing it in a temporary
variable, we extract the computation into a method that returns the result, and call that
instead.

Split Temporary Variable If we find a case where a temporary variable is reused, we


create a new temporary variable for each use.

Remove Assignments to Parameters If we find code that assigns to a parameter, we


use a temporary variable instead.

Amazon

Prev

don't be afraid of buying books

Next

WARRANTED ASSUMPTIONS
The test in the section titled Programming by Intention was the first test written in the project it came
from. It was written as shown and no other code had been written yet. Why is this significant? Well, if
we look at the test code we will see that it makes some assumptions:
1. there is a class called Movi eList ,
2. Movi eLi st has a zero-argument constructor, and
3. Movi eLi st has a method named s i ze which takes no parameters and returns an i nt .
Making these assumptions as we write tests (and later when we write the real code) gives us the ability
to design interfaces from the point of view of code that will use them. This allows us to choose names
that make sense and that read well, that is, are understandable and clear. It also allows us to decide
what behavior is required from a much better point of view: again, that of the client. This limits the
behavior to exactly what is required, which lets us write less code, which lets us work faster, which lets
us deliver sooner and more often.
How does this let us write less code? Primarily by deferring the addition of any code until it is proven to
be required. This not only includes implementation detail, but also the existence of classes and methods.
You do not create a class until you have a test that creates an instance of it. Likewise, you do not add
a method until you have a test that calls it.
When you are writing code (especially tests), write what you want, without worrying about how to do it.
Make up the support classes, methods, and variables you want and worry about their implementation
later. This keeps you focused on what you have in mind at the time. The compiler will remind you about
the assumptions you made.

Amazon

Prev

don't be afraid of buying books

Next

HOW TO PROGRAM BY INTENTION


There are several techniques that can be used to enhance the communication of intent by your code.
We explore them and how to use them. Some of these include: use of metaphor, test first, refactor,
make assumptions, and let the compiler tell you what your next step is.

Common Vocabulary
Everyone involved with the project should use a common vocabulary to talk about the domain and the
system. This can be based on the reality of the situation or it can be metaphorical if that eases
understanding.
Having a common vocabulary helps us with two things:
Understanding the domain Because the activity of developing a complex software
system involves people with different backgrounds and areas of knowledge it can be
difficult to have everyone understand everyone else. However, to be an effective team we
need to achieve a common understanding of the problem domain and the specific
problem that the system is to solve. The difficulty here is that the technical members of
the team will very likely not have a deep knowledge of the business. The opposite
problem is making sure that the business people involved are able to have a clear
understanding of what the system does, and to a large extent, how it works. Using a
metaphor helps us bridge these two gaps by providing a description of the domain and
the system that all parties can understand. It does this by providing a description in
terms that everyone understands.
The original XP project at Chrysler is a prime example. The people at Chrysler understood
factory and assembly line terminology. The programmers used that as a metaphor for the
payroll system they were developing. An employee's paycheck was the product,
constructed from dollar parts. It passed through various workstations, each of which
performed some operation such as converting hour parts to dollar parts, taking a
deduction, adding a bonus, etc.

Choosing names If we choose names from the common vocabulary, everyone knows
what we mean. Having a common vocabulary means that everyone is using the same
terms to mean the same thing. A great deal of confusion and miscommunication can be
avoided, and a great deal of time and money can be saved. Not only should the
terminology be used by everyone involved, it should also make sense in a fairly obvious
way.

Test First
One of the biggest effects of working a test first is that we have a large suite of programmer tests that
make it possible for us to refactor mercilessly. Also, by writing the tests first, we have time to think
about what we need to do before we have to think about how to do it.
Another benefit to writing the test first is that when the test passes, we're done. This has at least two

advantages:
1. it helps you work faster, because when the test passes we're done and can move on to
something else, and
2. because we stop when we have the test passing, it's easier to avoid overengineering a more
complex solution than is required. This improves the overall quality of our code, as well as
making it smaller and more understandable.

Make Assumptions
This is closely related to writing tests first, but isn't limited to writing tests. You can make assumptions
during implementations as well. By making assumptions as we work, we first decide what we need and
what it should be called, and then create it. This lets us think about what we want to accomplish before
worrying about the technical details of how to accomplish it. It also allows us to focus on the task at
hand. If we made some assumptions that turn out to be invalid (i.e., what we assumed existed isn't
there yet), we will find out when we try to compile. At that point we can deal with it; we have finished
what we were doing when we had to make the assumption. The alternative is to deal with our
assumptions as we make them. If we do that we risk losing track of what we were originally doing.
Keep a notepad or stack of blank cards next to the workstation for jotting down notes and To Do items.

Refactor
By refactoring as we see the need for it, we can change what we have already done to make our
intention clearer. Refactoring was discussed earlier in this chapter as well as in Chapter 2.

Let the Compiler Tell You


This technique helps us to defer work until it is required, by allowing us not to worry about keeping
track of the assumptions we make. When we make an assumption which turns out to be false (i.e., the
class, method, etc., that we are using in our code does not yet exist) the computer will inform us. In
some cases (e.g., Java and C++) the compiler will report that something is missing and we will have to
correct the issue (by stubbing the missing classes or methods). In other cases (such as Smalltalk) will
we receive notification at runtime that a class, method, etc., is missing.
For example, consider the following test which was the first written for a project:
pub l ic vo id te stE mp tyLi stSi ze() {
M o vieLi st em pty Li st = new Mov i e Lis t ( );
a s sertE qua ls ("S iz e of emp ty m o v ie l i st s h ou ld b e 0 . ", 0, e mp ty L is t. s iz e( ) );
}

If this is the first code written in a project, compiling will produce the following message:
Mov i eList ca nn ot be res olve d or i s n o t a t y pe .

So we create the required class:

pub l ic cl ass M ovi eL ist {


}

Compiling again results in:


The metho d s iz e() i s un defi ned f o r t h e ty p e M ov ie L is t

Next, we add a stub for the method:


pub l ic in t s iz e() {
r e turn 0;
}

By leveraging the compiler like this, you can stop worrying about the assumptions you made that have
to be dealt with. . . let the compiler tell you. This is also a way to avoid doing more than you absolutely
need to.

Do the Simplest Thing


I've said it before, but it's worth repeating: Strive for simplicity. If code starts out simple it's easier to
keep it simple than to make complex code into simple code. You may not always do the simplest thing,
but your code will be simpler and clearer if you take the time to figure out what the simplest thing
would be. An earlier section discussed simplicity in more detail.
There is a difference of opinion on this point. Some advise always doing the simplest thing that could
possibly work. I agree that forcing yourself to do the simplest thing is preferred when you are just
starting out. Get into the habit of being simple. Once you have that, actually doing the simplest thing is
not as important as long as you are aware of what it is.

Amazon

Prev

Next

don't be afraid of buying books

"NO COMMENT"
There are valid reasons to write comments. We'll talk about these later. However, as we discussed in
Chapter 2, most comments are not written for valid reasons. Fowler's Refactoring[16] calls comments
"deodorant". . . they are there to try to hide the bad smell of the code. The code is unclear, the code is
poorly written, names are badly chosen, the logic is obtuse, etc. Comments were added to try to explain
what the code does. The code should have been, and should be, refactored to make the comments
unnecessary.
Don't get me wrong. I'm not saying "Don't write documentation." Nobody really doing XP will say that.
Sometimes it is important to the customer to have specific documentation written. Also, I'm not saying
"Never write comments." What I am saying is "Never write unnecessary comments." Most comments are
unnecessary if the code is written so that the intent is clear. If and when we do write comments, we need
to make sure they communicate why and not how.

Valid Comments
As I mentioned above, there are several valid reasons for us to write comments.
Incomplete code This type of comment serves as a note to what we were in the midst of
working on, or how we see the code evolving. There generally is not much need for this
type of comment, since tasks should be no larger than what can be accomplished in a
single day. If you find that you do need to make such a note, choose a standard tag for it
so you can quickly search for loose ends. And stay away from generic n o t d o n e y e t
comments. I'd suggest something like:
// T ODO : The tree shou ld b e b a l a n c e d a f t e r d o i n g t h e i n s e r t i o n .

A valid use for this type of comment might be to note code that could benefit from
refactored. Maybe we saw the need to refactor but didn't have time to do it. Make
so that someone will spot it and do the refactoring when there is time. It might be
prudent to use standard wording for these "refactoring To Do" comments so that a
search can be performed as a rough code-debt metric. I suggest something similar
following:

being
a note
global
to the

// C ODE D EBT : the loop ing s t r u c t u r e i s a b i t c o n v o l u t e d , c o u l d u s e


// s ome m eth o d ex t ract ion .

Refactoring doesn't make it clear enough This isn't really a valid comment, rather it is
more like the previous type. . . it's an IOU. If refactoring doesn't clean up the code, either
someone else should try their hand at it, or (more likely) the code in question should be
scrapped and rewritten.
// N EED S WOR K : I t ried ex t r a c t m e t h o d , b u t i t ' s s t i l l a w k w a r d .
// M ayb e ref a ctor i ng t o a S t r a t e g y w o u l d c l e a n i t u p ?

Use of an unusual algorithm If we use an uncommon algorithm for some reason, we


should make a note of it with a comment. Point the reader to where they can learn more.
Don't try to document the algorithm in a huge comment. Just note what algorithm it is,
why it was used, and where to find more information.

// I us ed an AVL T ree alg o r i t h m h e r e t o k e e p t h e t r e e b a l a n c e d .

Use of a published algorithm If we use an algorithm that is published somewhere, we


should add a comment saying where and giving credit to the author. This type of comment
is often used with the previous type.
// T his A VL a lori t hm w as b a s e d o n B r a d A p p l e t o n ' s i m p l e m e n t a t i o n a t
// h ttp :/ /ww w .ent e ract .co m / ~ b r a d a p p / f t p / s r c / l i b s / C + + / A v l T r e e s . h t m l

Performance tuning This is important. If we tune for performance, we should add a


comment explaining it. At the very least we need to add a note saying that the method in
question has been tuned. If we tune without adding such a note we may find that
someone later refactors to make the code clearer, undoing the optimization in the process.
Keep in mind that we generally shouldn't be performance tuning until late in the project,
once performance has been measured and actual bottlenecks have been found.
// A ci rc ula r que u e is us e d h e r e f o r p e r f o r m a n c e r e a s o n s : t o a v o i d
// h avi ng to move elem ent s a r o u n d .

Class comment This is the one case that is often useful, in spite of the general
disapproval of comments. Not much is required. A simple note at the beginning of the class
briefly explaining why the class exists and what it is used for will suffice. Avoid writing a
how to use this class tutorial. That's one of the things the tests are for.
/* *
* T his c las s rep r esen ts a s i n g l e m o v i e t i t l e . I t i s r e s p o n s i b l e f o r
* m ain ta ini n g it s own ra t i n g s , r e v i e w s , e t c .
*/

A final note related to comments. Programmers often sign their work by including a comment in the file
header noting who wrote it. That's fine; credit where credit is due and all that, except:
1. If we are practicing XP, specifically collective code ownership, everyone will likely work on that
code. It is owned by the team.
2. A record of who worked on each file will be maintained by the source code control system (SCCS).
It is redundant to include the information in the file itself as well. Another comment on this:
Please don't include an expandable change log in the file (e.g., $ L o g $ in CVS). This clutters the
code, makes the files larger, and duplicates information that is easy to extract from the SCCS.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
In this chapter we've seen how even seemingly minor details, like picking a name for something, can
have a great impact on the code quality. We've discussed several techniques for choosing names,
achieving simplicity, staying focused, and making your code intent-revealing. The benefit to practicing
these techiques is code that is simple, understandable, and maintainable.
Sooner or later, someone will read our code. We will ourselves reread our code, sometimes after a long
absence. Any readers, including us, will be hindered or thwarted by code that is awkward, complex,
obscure, or in the worst case, misleading.
Well-crafted, simple, clear, intent-revealing code will speed us in our work instead. Such code also helps
teach readers how to program well. It teaches good solutions to problems, and teaches how to express
those solutions succinctly.
Learning to code with this clear intent is well worth any programmer's effort.

Amazon

Prev

don't be afraid of buying books

Part II: TOOLS AND TECHNIQUES


In this part of the book we will explore and discuss tools that can be used for TDD. The
most important of these is JUnit (and by extension, the whole xUnit family) and its
extensions. We also look at some other tools that are of use when practicing TDD and
work in conjunction with JUnit. Finally, we look at tools that are not specific to TDD, but
make practicing TDD easier, or have features that do so.

Next

Amazon

Prev

don't be afraid of buying books

Next

Chapter 4. JUNIT
I do owe them still
Coriolanus, Act 2 Scene 2 William Shakespeare
In this chapter we'll be looking at and learning a bit about the de facto tool for Test-Driven Development
using Java: the JUnit programmer test framework. Specifically, we'll be looking at the latest released
version as of this writing: JUnit 3.8.
The esteem in which developers hold JUnit is evidenced by the fact that it has repeatedly won honors at
the Java-World Editors Choice Awards in the Best Java Performance Monitoring/Testing Tool category.

Amazon

Prev

don't be afraid of buying books

Next

ARCHITECTURAL OVERVIEW
First let's look at how JUnit is structured. Figure 4.1 shows a high-level UML class diagram of the core of
JUnit: the ju n it.f r amew o rk package.

Figure 4.1. High-level class diagram of

jj uu nn ii tt .. ff rr aa mm ee ww o r k .

The class jun i t.te x tui. T estR unner and its superclass are included to show how the test runners
are hooked up to the framework. Let's look at each class in turn and see what it does and how it fits in.
Test This is the interface that all types of test classes must implement. Currently in the
framework there are only two such classes: T e s t C a s e and T e s t S u i t e .

TestCase This class is the main class you will be extending when you write your tests. It
is the simplest type of Te st. A concrete T e s t C a s e (i.e., a class that extends
Te st Case ) has methods that implement individual tests as well as optional s e t U p and
te ar Down methods (see the section later on "Writing a TestCase" for details).

TestSuite This is another subclass of T e s t . Its purpose is to collect together T e s t s:


Te st Case s, other T e stSu ites, or any combination of the two. Later we'll explore
techniques and conventions for collecting tests together into suites.

Assert This is the superclass of T e s t C a s e that provides all of the a s s e r t methods that
are available to you when you write your tests.

TestFailure This class simply encapsulates an error or failure that occurs during a test
run. It keeps track of the Test that failed and the exception that was responsible for the
error or failure (in the case of a failure this will be an A s s e r t i o n F a i l e d E r r o r ).

TestResult This class accumulates the results of running the tests. It is also responsible
for informing any interested parties of the start and end of a test, as well as failures and
errors. It is this facility that enables the Graphical User Interface's (GUI) progress bar to
move for each test, and the results to be displayed.

TestListener This is an interface that is implemented by any class that wishes to track
the progress of a test run. Methods are declared for notification of the start and end of
each test, as well as the occurrence of errors and failures.

TestRunner JUnit comes with a selection of T e s t R u n n e r classes. Each extend


ju ni t.ru n ner . BaseT estR un n e r and provide various interfaces to the JUnit
framework.

For more discussion of the framework and how it is composed of design patterns, see [20].

Amazon

Prev

don't be afraid of buying books

Next

THE ASSERTIONS
When you are writing test methods you make heavy use of the capabilities you inherit (by way of
T es t Ca se ) from A s ser t. For all As se r t methods there are two versions: with an initial S t r i n g
parameter that contains a message to be displayed in the case of failure, and without.

fail
The simplest method (really we'll be talking about pairs of methods, but from here on I'll just consider
them single methods) is fa i l().
v oi d f ai l()
v oi d f ai l(S t ring mess a ge)

Calling fa il( ) causes an immediate test failure. This is useful in cases where the test can get to a
point that indicates something went wrong. An example is when you expect a method call to throw a
specific exception. You would place a call to f a i l ( ) immediately after the method call inside the t r y
block. The following example demonstrates this:
p ub l ic v oid test D ivis i onBy Zero ( ) {
try {
in t x = 3/ 0 ;
fa il( " Divi s ion b y ze ro d i d n ' t t h r o w a n e x c e p t i o n " ) ;
} ca tc h ( A rith m etic E xcep tion e x ) {
}
}

assertTrue and assertFalse


The next methods of interest are as ser t T r u e ( ) and a s s e r t F a l s e ( ) . a s s e r t T r u e ( ) used to be
named a ss ert ( ) but has been changed due to the addition of the a s s e r t keyword in J2SE 1.4..
v oi d
v oi d
v oi d
v oi d

a ss ert T rue( b oole a n co ndit i o n )


a ss ert T rue( S trin g mes sage , b o o l e a n c o n d i t i o n )
a ss ert F alse ( bool e an c ondi t i o n )
a ss ert F alse ( Stri n g me ssag e , b o o l e a n c o n d i t i o n )

If c o n dit io n is fal s e, the test fails. The a s s e r t F a l s e ( ) method does the opposite, failing if the
value of the condition is tr ue. This is provided as a cleaner alternative to using a s s e r t T r u e ( ) with a
negated condition.

negated condition.
Here's an example:
p ub l ic v oid test E mpty L ist( ) {
a s se rt Tru e ("An empt y lis t sh o u l d r e p o r t e m p t y " , e m p t y L i s t . i s E m p t y ( ) ) ;
E n um er ati o n em p tyEn u mera tor = e m p t y L i s t . m o v i e s ( ) ;
a s se rt Fal s e("E m pty l ist shou l d h a v e e m p t y E n u m e r a t i o n " ,
e m ptyE n umer ator . h a s M o r e E l e m e n t s ( ) ) ;
}

assertNull and assertNotNull


Next are two method pairs for checking for null or not null:
v oi d
v oi d
v oi d
v oi d

a ss ert N ull( O bjec t obj ect)


a ss ert N ull( S trin g mes sage , O b j e c t o b j e c t )
a ss ert N otNu l l(Ob j ect obje c t )
a ss ert N otNu l l(St r ing mess a g e , O b j e c t o b j e c t )

Each of these takes an Obje c t and checks whether it is or isn't n u l l , then fails if it isn't as expected.
p ub l ic v oid test E mpty L istE nume r a t i o n ( ) {
E n um er ati o n em p tyEn u mera tor = e m p t y L i s t . m o v i e s ( ) ;
a s se rt Not N ull( " Empt y lis t sh o u l d h a v e n o n - n u l l E n u m e r a t i o n " ,
e mpty E nume rato r ) ;
}

assertSame and assertNotSame


v oi d
v oi d
v oi d
v oi d

a ss ert S ame( O bjec t exp ecte d , O b j e c t a c t u a l )


a ss ert S ame( S trin g mes sage , O b j e c t e x p e c t e d , O b j e c t a c t u a l )
a ss ert N otSa m e(Ob j ect expe c t e d , O b j e c t a c t u a l )
a ss ert N otSa m e(St r ing mess a g e , O b j e c t e x p e c t e d , O b j e c t a c t u a l )

These methods succeed if the two supplied arguments are the same object (for a s s e r t S a m e ( ) )or
different objects (for asse r tNot Same( ) ).
p ub l ic v oid test I tera t ion( ) {
E n um er ati o n th r eeMo v ieEn umer a t o r = s c i f i L i s t . m o v i e s ( ) ;
a s se rt Sam e ("Fi r st m o vie from t h r e e M o v i e E n u m e r a t o r i s b a d " ,
sta r Wars ,
(Mo v ie) t hree Movi e E n u m e r a t o r . n e x t E l e m e n t ( ) ) ;
a s se rt Sam e ("Se c ond m ovie fro m t h r e e M o v i e E n u m e r a t o r i s b a d " ,
sta r gate ,
(Mo v ie) t hree Movi e E n u m e r a t o r . n e x t E l e m e n t ( ) ) ;
a s se rt Sam e ("Th i rd m o vie from t h r e e M o v i e E n u m e r a t o r i s b a d " ,
sta r Trek ,
(Mo v ie) t hree Movi e E n u m e r a t o r . n e x t E l e m e n t ( ) ) ;
}

assertEquals
This is really a family of methods, one for each Java type. Each method tests for equality using the
appropriate mechanism (i.e., = for primitives and the e q u a l s ( ) method for objects). For this reason
I've omitted types for e xpec t ed and ac t u a l .
v oi d a ss ert E qual s (exp e cted , ac t u a l )
v oi d a ss ert E qual s (Str i ng m essa g e , e x p e c t e d , a c t u a l )

It is important to understand the difference between a s s e r t S a m e and


ass e rtEq u als. The former passes only when the two objects being
compared are the same object. Make sure you understand that.. the
same object... not objects that are equals.

There is a set of methods (with and without an initial message parameter) for the following types:
b oo l ea n, b yte , c h ar, do u ble, f loa t , i n t , l o n g , O b j e c t , and s h o r t .
The version for Ob j ect makes use of the e q u a l s method of the e x p e c t e d argument.
p ub l ic v oid test R emov i ng() {
a s se rt Equ a ls(" B ad i n itia l si z e " , 3 , s c i f i L i s t . s i z e ( ) ) ;
s c if iL ist . remo v e(st a rWar s);
a s se rt Equ a ls(" B ad s i ze a fter r e m o v i n g t h e f i r s t m o v i e " ,
2,
sc i fiLi s t.si ze() ) ;

s c if iL ist . remo v e(st a rgat e);


a s se rt Equ a ls(" B ad s i ze a fter r e m o v i n g t h e s e c o n d m o v i e " ,
1,
sc i fiLi s t.si ze() ) ;

s c if iL ist . remo v e(st a r Tr ek);


a s se rt Equ a ls(" S houl d be empt y a f t e r r e m o v i n g t h i r d m o v i e " ,
0,
sc i fiLi s t.si ze() ) ;

The version for do u ble and f loat have an additional parameter (of type d o u b l e or f l o a t ,
respectively) that specifies the maximum difference allowed between the expected and actual values.

Amazon

Prev

don't be afraid of buying books

Next

WRITING A TESTCASE
Tes t Case is the class most often used in JUnit. You begin by creating a subclass of Te s tC as e to which
you will add related test methods. The examples here will be taken from the project that forms the core
of the book: keeping track of a list of movies you would like to see, with ratings, recommendations, and
so forth. We start with the idea of supporting a list of movies. Here is what the skeletal test class looks
like:[1]
[1] Note that prior to JUnit 3.8 a constructor was required which took a S tr i ng parameter, which
was then passed to the superclass' constructor. As of version 3.8 this is no longer required.

imp o rt ju nit .f ram ew ork. Test Case ;


pub l ic cl ass T est Mo vieL ist exte n d s T e s tCa s e {
}

The name of the class is important. It should either be of the form T es t My Cl a ss or M yC la s sT es t .


Using one of these forms of naming allows the collectors to automatically find your test classes. The
same applies to T est Su ites.
Now that we have a skeleton set up we can start writing tests. We do this by writing methods that
follow the basic pattern:
set up preconditions
exercise functionality being tested
check postconditions
Interestingly, it is best to write the tests in the opposite order:
1. write the assertions that test what you want as a result (one small assertion at a time)
2. do what needs to be done to achieve those results
3. set up any preconditions (this may be done implicitly by s et U p( ) ; more on this later).
Doing it this way leads to smaller, more focused tests.
Test methods should be short and to the point. Each method should test a specific bit of functionality. In
keeping with the TDD mantra of test a little, code a little, don't write all the tests then go about writing
code. Write one test method... now make it run... write another test... make it run... and so on. And try
to focus on keeping those tests as small as you can. Time for another example: time to write the first
test. This is often an awkward point: How do you start testing? What test should you start with? My
advice is to find the simplest thing to test. If there is a basis case, test that first, then move
incrementally to the larger cases.
In this example our basis case is an empty list of movies. Here's a test method for empty list behavior,
from which we can learn several things:
pub l ic vo id te stE mp tyLi st() {
M o vieLi st em pty Li st = newM ovie L i st( ) ;
a s sertE qua ls ("S iz e of an empt y lis t sho u l d be z e ro . ", 0 , e mp t yL is t .s iz e () );
a s sertT rue (" An em pty list sho u l d r e p ort e mp ty ." , e m pt yL i st .i s Em pt y () );
}

The first thing to notice is that we use a fairly specific method signature. All test methods should have a
signature of the form pu bl ic v oid te st W h ate v e r() . This allows them to be found and collected
automatically by the JUnit framework.
Next, notice the pattern:
set up preconditions... create a new M o vie L i st, get its En um e ra t io n of Mo v ie s.
exercise functionality... call empty L ist . s ize ( ) , em p ty Li s t. i sE mp t y( ),etc.
check postconditions... check that s ize ( ) returned 0 , check that is Em p ty () returned tr ue ,
etc.
Next, notice the a ss ert ... method calls. The final thing to notice is that the test refers to classes
(Mov i eList and Mo vi e)as well as methods (si z e (), is E mp t y( ), and m o vi es ( )) none of which
have been written yet. This is Test-Driven Development in action.
As we'll explore later on, you also want to be on the lookout for opportunities to refactor your test code
just as you do with the production code. Test code has characteristic smells of its own. At XP2001 there
was a great paper on refactoring test code[40].

Amazon

Prev

don't be afraid of buying books

Next

RUNNING YOUR TESTS


Now that we've discussed how to write tests we should look at what you do with your tests once you've
written them. This section will look at the T e s t R u n n e r implementations that are part of JUnit, as well
as plugins for some IDEs. As an example for comparison, we'll use the samples test suite from the JUnit
3.8 build.

Errors and Failures


When you run tests there are two types of feedback you get for each test:
whether the test passed or failed
if the test failed, whether it was an error or a failure, and what caused it
Additionally, you get feedback on the test run as a whole: pass (the diligently sought-after green bar) or
fail (the not-so-pleasant, but still invaluable, red bar). The overall pass/fail feedback is commonly
referred to as a greenbar/redbar due to the implementation of the graphical test runners: the progress
bar is green as long as tests are passing but turns red if one test fails (and stays red). This provides
immediate and undeniable feedback.
As mentioned above, when a test fails it can be due to an error or a failure. An error indicates that the
test terminated unsuccessfully due to an unexpected exception being thrown. A failure indicates an
unsuccessful termination due to an assertion failure.

JUnit's Text UI
We'll start with the simplest thing that could possibly work: a command line, textual T e s t R u n n e r .
You simply run j unit . text u i.Te stR u n n e r , passing it the name of the T e s t to run, as shown in
Figure 4.2. As each test is run, a character is printed to the console (actually S y s t e m . o u t ): . for a
pass, E for an error, or F for a failure.

Figure 4.2. The text TestRunner.

Following the individual test status output, a failure trace is output for each unsuccessful test.

JUnit's Swing GUI


The j un it. s wing u i.T es tRun ner, shown in Figure 4.3, provides a much more elaborate interface for
running tests. A notable addition in respect to the AWT GUI is the test hierarchy tab that allows you to
open test suites to recursively see the component tests. Each test case is annotated by a green check or
red X, indicating the pass/fail status of that particular test. Selecting a test and clicking on the Run
button will rerun that particular test.

Figure 4.3. The swing TestRunner.

Running Tests as an ANT TASK


There are two tasks in the optional task jar for using JUnit from within an ANT build file. Both support a
variety of attributes that we won't cover here. The ANT documentation does a more than adequate job.
The first task is ju nit , which runs a set of JUnit tests. The screen output from a j u n i t task is
minimal, and not overly useful. Here's a b u i l d . x m l snippet that shows basic use of the j u n i t task
which specifies an XML formatter that is needed in support of the task we'll discuss in a moment:

< ju n it p rin t summ a ry=" o n"


>
< fo r ma tt er t ype= " xml" /
>
< cl a ss pa th
>
< pa t he le men t loc a tion = "${d ist} " /
>
< pa t he le men t loc a tion = "${d ist} / j u n i t . j a r " /
>
< /c l as sp ath
>
< te s t na me= " juni t .sam p les. AllT e s t s " /
>
< /j u ni t
>

ANT provides another task for use with JUnit: j u n i t r e p o r t . The purpose of this task is to take status
reports (in XML) that are generated by one or more j u n i t tasks, consolidate them, and generate a
nested set of HTML pages that present the test results.
Using the j uni t repo r t task increases the value of running the entire test suite as part of an
automated build. The HTML output can be copied (by another ANT task) to a known place on the
development team's intranet site. By running the build regularly, you can make full and detailed status
information (as described by the tests) available in an up-to-date form. Also very useful is the highlevel test count. Coupled with a continuous integration and build process (e.g., CruiseControl [URL 37])
you can literally see the test count increase over the course of even hours. In an XP setting, this
feedback is quite valuable.
Figures 4.4 and 4.5 show parts of the resulting report (top level and details, respectively). Here's a
b ui l d. xm l snippet to build a report from the output of the previous snippet:

< ju n it re por t tod i r=". "


>
< fi l es et di r ="."
>
< in c lu de na m e="T E ST-* . xml" /
>
< /f i le se t
>
< re p or t for m at=" f rame s " to dir= " . / h t m l " /
>
< /j u ni tr epo r t
>

Figure 4.4. Top-level output report from the ANT


[View full size image]

j u n ii tt rr ee pp oo rr tt

task.

Figure 4.5. Detailed output from the ANT


[View full size image]

The JUnit plugin for Eclipse

jj uu nn ii tt rr e p o r t

task.

Erich Gamma was one of the original developers of JUnit, and his team at OTI has done a splendid job
of integrating JUnit into Eclipse. They don't just spawn the Swing JUnit TestRunner like many IDE
integrations do. They have written a TestRunner that is fully integrated into the Eclipse workbench,
taking full advantage of the Eclipse plugin architecture.
Figure 4.6 shows an Eclipse session with a JUnit view open (center right). The Eclipse TestRunner is a
view within workbench. It can be made into a fast view (i.e., docked in the perspective bar and popped
back on demand), run in debug mode, and invoked by running a class as a JUnit Test. This tight
integration also provides the ability to double-click a line of the failure trace and have an editor open on
the corresponding file, positioned at the line causing the failure. Nifty. Clicking on the name of a test in
the Failures or Hierarchy tabs opens an editor on the source of that test. Having JUnit this integrated
speeds up the programming experience greatly: make a change, a quick keystroke to save and compile,
and another quick keystroke to rerun the last set of tests. These features coupled with Eclipse's other
TDD supporting features (and the fact that it is open source) make it a great choice of IDE for TDD.

Figure 4.6. The JUnit view in Eclipse.


[View full size image]

Amazon

Prev

don't be afraid of buying books

Next

USING SETUP() AND TEARDOWN()


As you write test methods you may find that you are creating the same test object(s) in multiple test
methods. Hey, that smells! What you do in this situation is create a pu b li c v oi d s et Up ( ) method
and move the shared test setup code there, creating a test fixture that establishes a context in which
your test methods will run. When you move fixture creation into a se t Up () method, don't forget to
make the local variables into instance variables. If we found that we needed e m pt yL i st in other tests
we could add the following bit of code and just use e m p ty Li st as required in the test methods.
pri v ate M ovi eL ist e mpty List ;
pub l ic vo id se tUp () {
e m ptyLi st= ne w M ov ieLi st() ;
}

The counterpart of se tU p() is te arDow n () in which you perform any required clean up of the test
fixture. This is most useful if, as part of the fixture, you need to allocate limited resources that will not
be reclaimed automatically, such as databases and communication connections.
The setU p () method is called before each test method, and t e ar Do w n( ) is called after each one. This
keeps tests isolated and reduces the chance of side effects from test to test. If you want s et Up ( ) and
tea r Down( ) to be common for all tests in a T e stC a s e, there is a decorator that does just that.
Chapter 5 discusses this and other extensions.

Amazon

Prev

don't be afraid of buying books

Next

USING TESTSUITE
As mentioned earlier, Te st Suit es are used to collect a selection of T e st s so that they can be run as
a unit. Note that a T es tSui te may recursively contain other T e st Su i te s.
When you start out with JUnit you will likely be using Te s tS u it es without even knowing it. The
framework uses reflection to find and collect all of the test methods in a given T es t Ca se whose
signatures match:
pub l ic vo id te stW ha teve r()

When you use this facility, you have no control over or any guarantee of the order in which the test
methods will be run. Tests should be written in such a way that they are order independent, so this
should not be a problem. If, however, you have a situation where you want or need tests to run in a
specific order you can supply a pu blic s tat i c Te s t s ui te ( ) method which builds the T e st Su i te
as required. Here is an example:
pub l ic st ati c Tes t suit e() {
s u ite.a ddT es t(n ew Tes tMov ieLi s t ("t e s tEm p t yL is t" ) );
s u ite.a ddT es t(n ew Tes tMov ieLi s t ("t e s tAd d i ng ") );
r e turn sui te ;
}

I can't stress enough that test methods within a T e stC a se should be completely independent. You
should seldom have to resort to writing your own sui t e () method. In xUnit for languages that lack a
reflection mechanism, you will have to build suites by hand, as it were.
If you find that you want or need to have test methods run in a specific order you should see if there is
a way to remove that requirement. It may be that you are relying on an external resource whose state
is modified by each subsequent test. Try to find a way to tease apart those dependencies, setting up the
appropriate fixture individually for each test. You might be able to do this by mocking such an external
resource. For more on mock objects, see Chapter 7.
Another more valid use of TestS uite, in my opinion, is to create smaller T es t Su it e s which include a
specific subset of the tests. There are two main reasons to do this:
1. You can create custom suites of tests that relate directly to the task you are working on. By
doing this you can save time running tests by focusing the tests that are run on the area of the
code you are working on. But always keep that suite and the code it tests in sync. As you find
yourself needing to involve code that your suite doesn't test, be sure to add the tests for that
code to your suite so that you can proceed, protected and confident.
2. You will most likely want to have a Te s t Sui t e that tests a complete package or module. My
practice is to have a suite in each package called Te st Al l that includes all tests in that
package and all subpackages. By doing this, I can easily test any subtree of a project. Here's an
example:
pack age c om. sa orsa .now play i n g.p d . tes t s;
impo rt ju nit .f rame work .Tes t ;
impo rt ju nit .f rame work .Tes t S uit e ;
publ ic cl ass T estA ll e xten d s Te s t Sui t e {
pu bli c Tes tA ll() {
su pe r() ;
}

pu bli c sta ti c Te st s uite ( ) {


Te st Sui te sui te = new T est S u ite ( );
su it e.a dd Test Suit e(Te s t Mov i e .cl a ss ) ;
su it e.a dd Test Suit e(Te s t Mov i e Lis t .c l as s) ;
re tu rn su ite;
}

Amazon

Prev

don't be afraid of buying books

Next

HOW DOES IT ALL FIT TOGETHER?


Tests, cases, suites, fixtures. How do they all fit together? What really happens when the tests run? This
section will answer these questions. By the end of it, you should have a much deeper understanding of
what happens when we run our tests. This will help us write and organize groups of tests that are
simpler, cleaner, more understandable, and more maintainable.
Let's begin by considering T estC ase. It is used to group related tests together. But what does related
mean? It is often misunderstood to mean all tests for a specific class or specific group of related classes.
This misunderstanding is reinforced by some of the IDE plugins that will generate a T e s t C a s e for a
specified class, creating a test method for each method in the target class. These test creation facilities
are overly simplistic at best, and misleading at worst. They reinforce the view that you should have a
T es t Ca se for each class being tested, and a test for each method in those classes. But that approach
has nothing to do with TDD, so we won't discuss it further.
This structural correspondence of tests misses the point. You should write tests for behaviors, not
methods. A test method should test a single behavior. Examples include:
The size of an empty list should be zero.
An empty list should answer tru e when it is asked if it i s E m p t y ( ) .
The iterator returned by an empty list should have no elements.
Here's a T es tCas e corresponding to the above behaviors:
p ub l ic c las s Tes t Empt y List ext e n d s T e s t C a s e {
p r iv at e L i st e m ptyL i st;
p r ot ec ted void setU p () t hrow s E x c e p t i o n {
em pt yLi s t = n ew A r rayL ist( ) ;
}
p u bl ic vo i d te s tSiz e () {
as se rtE q uals ( "Siz e of an e m p t y l i s t s h o u l d b e z e r o . " ,
0,
empt y List .siz e ( ) ) ;
}
p u bl ic vo i d te s tIsE m pty( ) {
as se rtT r ue(" E mpty list sho u l d r e p o r t e m p t y . " ,
e m ptyL i st.i sEmp t y ( ) ) ;
}

p u bl ic vo i d te s tIte r ator () {
It er ato r emp t yLis t Iter ator = e m p t y L i s t . i t e r a t o r ( ) ;
as se rtF a lse( " Iter a tor from e m p t y l i s t s h o u l d b e e m p t y . " ,
e mpty L istI tera t o r . h a s N e x t ( ) ) ;
}

TestCase is a mechanism to allow fixture reuse. Each T e s t C a s e subclass represents a fixture, and
contains a group of tests that run in the context of that fixture. A fixture is the set of preconditions and
assumptions with which a test is run. It is the runtime context for the test, embodied in the instance
variables of the Te stC a se, the code in the s e t U p ( ) method, and any variables and setup code local to
the test method. In the above example, the fixture consisted of e m p t y L i s t , an A r r a y L i s t with no
elements.
An instance of a T e stC a se is created for each individual test method. When the T e s t C a s e object is
run, it builds the fixture (using se tUp() ), runs its single test, and tears down the fixture (using
t ea r Do wn ()). Figure 4.7 shows this. Note that the two test case objects are the same object, but with
methods from the super and subclass. By allowing T e s t C a s e to contain multiple tests (i.e., p u b l i c
v oi d t es tXX X () methods) you are sharing a fixture definition.

Figure 4.7. Sequence of running a single TestCase.

This hardwired fixture-orientation makes T e s t C a s e a bad choice for grouping tests structurally or
conceptuallyT estS u ite is better for that, as we'll see later. So instead of using T e s t C a s e to group
tests for a given class, try thinking about it as a way to group tests that need to be set up in exactly
the same way.
A measure of how well your Tes tCase is mapping to the requirements of a single fixture is how
uniformly that fixture (as described by the s e t U p ( ) method) is used by all of the test methods.
Whenever you discover that your se tUp ( ) method contains code for some of your test methods, and
different code for other test methods, consider it a smell that indicates that you should refactor the
T es t Ca se into two or more Tes tCase s.
Running a cohesion metric on your test code can help bring this to
light.

Once you get the hang of defining T est C a s e s this narrowly, you will find that they are easier to
understand and maintain. And again, as we will see shortly, there are still lots of ways to organize the
tests for a specific class (or for any other natural grouping) so that they are easy to identify.
For example, here is a Tes t Case similar to the one above, but with multiple fixtures:
p ub l ic c las s Tes t List exte nds T e s t C a s e {
p r iv at e L i st e m ptyL i st;
p r ot ec ted void setU p () t hrow s E x c e p t i o n {
em pt yLi s t=ne w Arra y List ();
}

p u bl ic vo i d te s tEmp t yLis tSiz e ( ) {


as se rtE q uals ( "Siz e of an e m p t y l i s t s h o u l d b e z e r o . " ,
0,
empt y List .siz e ( ) ) ;
}
p u bl ic vo i d te s tEmp t yLis tIsE m p t y ( ) {
as se rtT r ue(" E mpty list sho u l d r e p o r t e m p t y . " ,
e m ptyL i st.i sEmp t y ( ) ) ;
}
p u bl ic vo i d te s tEmp t yLis tIte r a t o r ( ) {
It er ato r emp t yLis t Iter ator = e m p t y L i s t . i t e r a t o r ( ) ;
as se rtF a lse( " Iter a tor from e m p t y l i s t s h o u l d b e e m p t y . " ,
empt y List Iter a t o r . h a s N e x t ( ) ) ;
}
p u bl ic vo i d te s tOne I temL istS i z e ( ) {
Li st on e Item L ist = new Arr a y L i s t ( ) ;
on eI tem L ist. a dd(" O ne") ;
as se rtE q uals ( "Siz e of a on e i t e m l i s t s h o u l d b e o n e . " ,
1,
oneI t emLi st.s i z e ( ) ) ;
}

p u bl ic vo i d te s tOne I temL istI s E m p t y ( ) {


Li st on e Item L ist = new Arr a y L i s t ( ) ;
on eI tem L ist. a dd(" O ne") ;
as se rtF a lse( " One i tem list s h o u l d n o t r e p o r t e m p t y . " ,
o neIt e mLis t.is E m p t y ( ) ) ;
}

Here, we want to extract the common fixture code in t e s t O n e I t e m L i s t S i z e ( ) and


t es t On eI tem L istI s Empt y () into a common fixture using the s e t U p ( ) method. The problem is that
the other three tests don't need it. This class should be split into two fixtures, the earlier
T es t Em pt yLi s t and the following:
p ub l ic c las s Tes t OneI t emLi st e x t e n d s T e s t C a s e {
p r iv at e L i st o n eIte m List ;
p r ot ec ted void setU p () t hrow s E x c e p t i o n {
on eI tem L ist = new Arra yLis t ( ) ;
on eI tem L ist. a dd(" O ne") ;
}
p u bl ic vo i d te s tOne I temL istS i z e ( ) {
as se rtE q uals ( "Siz e of a on e i t e m l i s t s h o u l d b e o n e . " ,
1,
oneI t emLi st.s i z e ( ) ) ;
}

p u bl ic vo i d te s tOne I temL istI s E m p t y ( ) {


as se rtF a lse( " One i tem list s h o u l d n o t r e p o r t e m p t y . " ,
o neIt e mLis t.is E m p t y ( ) ) ;
}

This is better, but notice that it leaves a little smell behind: overly long test method names that echo
the fact that we are testing a list with one item. Because the class name makes it clear what size list
we are testing, we can shorten these method names to describe just the behaviors being tested:
t es t Si ze () and t e stIs E mpty ().
T es t Su it e is a mechanism for grouping a collection of T e s t instances and running them sequentially.
These can be either Tes tC ases or, recursively, other T e s t S u i t e s. Figure 4.8 shows this.

Figure 4.8. Sequence of running a TestSuite.

T es t Ca se has the ability to create a T e s t S u i t e containing all possible instances of itself. This is done
by using reflection to create an instance for each constituent test. You also have the ability to create
explicit Te st Suit e s by subclassing Tes t S u i t e and writing a custom p u b l i c s t a t i c T e s t
s ui t e( ) method. This is useful for collecting together groups of smaller T e s t S u i t e s, which can be
other custom Test S uit es, or T estSu i t e s generated by T e s t C a s e s.
Both Te st Sui t e and Tes t Case implement the T e s t interface. The T e s t R u n n e r s work with objects
that are Te sts, hence, we can point a runner at any T e s t (i.e., T e s t S u i t e or T e s t C a s e )and
recursively run all the contained tests. At runtime, after the T e s t s have been recursively collected, the
structure looks something like that shown in Figure 4.9. The internal nodes are T e s t S u i t e s and the
leaves are T est Ca ses (remember, each T e s t C a s e instance represents a single test method).

Figure 4.9. A sample of the runtime structure of tests.


[View full size image]

Amazon

Prev

don't be afraid of buying books

Next

WHERE DO TESTS BELONG?


Now that you are writing tests using JUnit, there is the issue of where you should put them. There are
two main alternatives; we'll look at them in turn.
With the code being tested. This allows you to reference package-scoped members
from your test code. A disadvantage is that it clutters your packages with test classes,
making it less clear what is production code.

In a subpackage. Typically, this would be a subpackage named te st s . For example, if


you are testing code in the package c o m.s a o rsa . no w pl ay i ng the test code would be
in the package c om.s aors a.now p lay i n g.t e s ts . This has the advantage of limiting
access to only public members of the classes being tested.

A second issue is whether you put test code in the same source tree as the code being tested. My
personal preference is to answer this with a resounding "yes." There is no real technical advantage either
way, but I just like to keep the tests tightly bound to the tested code. Everything is in one place, and it
reinforces the philosophy that code without tests doesn't exist. Also, not all tools handle multiple source
trees very well.
Keep in mind that some organizations will have policies about where test code must reside, keeping test
code separate from production code, or about what code is where in general. If that's the case, you'll
generally have to go along with it.
Personally, I make a habit of placing my test code in a subpackage. I
find that this clean separation between production and test code is
beneficial. If all you have are T e stC a s e and Te st S ui t e subclasses,
then that separation can be maintained by naming conventions. This
becomes more problematic as you add other types of classes to the
test scaffolding. Some, like mock objects (see Chapter 7), can use
naming (e.g., M ockC om mP o r t), but often you will require other helper
classes and the name space gets crowded. Another reason I use a
subpackage is to reduce visibility to the public methods of the
production code. After all, you are working with the behavior of the
code, and this is made up of its public interface.
As for the question of using the same or parallel source tree, I use a
single source tree. This keeps things simple, binds the test code
closely to the corresponding production code, and works with all tools.

Amazon

Prev

don't be afraid of buying books

Next

TIPS
As with any technology or technique, there are certain idioms or rules of thumb that are helpful to know
when you are practicing TDD. I've polled the TDD community and collected a list of what the leading
practitioners consider good ideas.

Test the simple stuff first


Some things are easier to test, or it's obvious how to write the tests. The types of things that this could
include are:
proper handling of null (but only in cases where nu l l would be a potential value)
empty collection or null object behavior
generally, the basis case for recursive or iterative structures and computations.
By tackling these easy tests first you quickly get into the test-first rhythm.

Use assertEquals
You should use the ass er tEqu als( ) method as much as possible[31]. This is a matter of clarity.

Use the message argument


All of the methods that are inherited from As s e rt have a form that takes a message as the initial
argument. Use it![31] If you feel that the test is clear and failure would be unambiguous, please
disregard this advice. However, if you find that the failure isn't clear, including a message does help.
For example, if the test is as simple as possible, having only a single assertion, and is descriptively
named then there is little reason to have a message in the assertion. It is simply redundant. However, if
you have multiple assertions in a single test, use the message to aid in identifying them in the
TestRunner output. If an assertion fails, the message will indicate which one.
Failure to use the message argument is one of the smells reported by Arie van Deursen et.al. at
XP2001[40].
This message argument provides an explanation of the failure to the user. It makes it easier to
understand why the test is failing. This can be especially useful when you are not running JUnit in an
integrated environment where you can double-click on the error trace and jump to the assert line that
failed. It also serves as documentation for the tests, stating explicitly what the failure condition is for
that test.
In any case, the message shouldn't just echo what the test code, or the built-in error message, says.

Keep test methods small


Most importantly, keep the number of assertions to a minimum in each test method. Doing this will keep
your tests small and focused. This leads to easy-to-understand tests. The "extreme" of this is one
assertion per test.

Here's an initial version of a test from Chapter 8 which has one monolithic test method:
pub l ic cl ass T est Wi dget s ex tend s Tes t C ase {
p r otect ed vo id te stWi dget s() {
MockC ont ro l c on trol = E asyM o c k.n i c eCo n t ro lF or ( Mo v ie Li s tE di t or .c l as s) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
contr ol. ac tiv at e();
Movie Lis tW ind ow win dow = ne w Mov i e Lis t W in do w( m oc k Ed it o r) ;
windo w.i ni t() ;
windo w.s ho w() ;
JList mo vi eLi st = w indo w.ge t M ovi e L ist ( ) ;
asser tNo tN ull (" Movi e li st s h o uld b e n o n n ul l" , m o vi eL i st );
asser tTr ue ("M ov ie l ist shou l d be s how i n g" ,m ov i eL i st .i s Sh ow i ng () ) ;
JText Fie ld mo vi eFie ld = win d o w.g e t Mov i e Fi el d( ) ;
asser tNo tN ull (" Movi e fi eld s h oul d be n o n nu ll " , m ov ie F ie ld ) ;
asser tTr ue ("M ov ie f ield sho u l d b e sho w i ng ", m o vi e Fi el d .i sS h ow in g () );

JButt on ad dBu tt on = win dow. g e tAd d B utt o n () ;


asser tNo tN ull (" Add butt on s h o uld b e n o n n ul l" , ad d Bu tt o n) ;
asser tEq ua ls( "A dd b utto n sh o u ld b e la b e le d \" A dd \ "" ,
"A dd",
ad dBut ton. getT e x t() ) ;

Notice that this one method, tes tWidg e ts( ) , sets up a fixture and tests three different things: the
list, the field, and the button. Oops! Time to refactor, making a fixture and separating those tests. The
result is shown below. Notice how much cleaner and more understandable it is.
pub l ic cl ass T est Wi dget s ex tend s Tes t C ase {
p r ivate Mo ck Con tr ol c ontr ol;
p r ivate Mo vi eLi st Edit or m ockE d i tor ;
p r ivate Mo vi eLi st Wind ow w indo w ;
p r otect ed vo id se tUp( ) {
contr ol = Eas yM ock. nice Cont r o lFo r ( Mov i e Li st Ed i to r .c la s s) ;
mockE dit or = (M ovie List Edit o r ) c o n tro l . ge tM oc k () ;
contr ol. ac tiv at e();
windo w = n ew Mo vieL istW indo w ( moc k E dit o r );
windo w.i ni t() ;
windo w.s ho w() ;
}
p u blic voi d tes tL ist( ) {
JList mo vi eLi st = w indo w.ge t M ovi e L ist ( ) ;
asser tNo tN ull (" Movi e li st s h o uld b e n o n n ul l" , mo v ie Li s t) ;
asser tTr ue ("M ov ie l ist shou l d be s how i n g" ,m ov i eL i st .i s Sh ow i ng () ) ;
}

p u blic voi d tes tF ield () {


JText Fie ld mo vi eFie ld = win d o w.g e t Mov i e Fi el d( ) ;
asser tNo tN ull (" Movi e fi eld s h oul d be n o n nu ll " , m ov ie F ie ld ) ;
asser tTr ue ("M ov ie f ield sho u l d b e sho w i ng ", m o vi e Fi el d .i sS h ow in g () );
}

p u blic voi d tes tB utto n() {


JButt on ad dBu tt on = win dow. g e tAd d B utt o n () ;
asser tNo tN ull (" Add butt on s h o uld b e n o n n ul l" , a d dB ut t on );
asser tTr ue ("A dd but ton shou l d be s how i n g" , ad d Bu t to n. i sS ho w in g( ) );
asser tEq ua ls( "A dd b utto n sh o u ld b e la b e le d \" A dd \ "" ,
"A dd",
ad dBut ton. getT e x t() ) ;
}

Test boundary conditions early


This is similar to testing nu ll handling, but includes items such as empty strings, 0 , and MA X _I NT .
Don't forget about domain-specific boundary conditions. These are often more restrictive than the natural
ones. This is also a bootstrap or tester's block technique. These tests tend to be easy to write so if
you're casting about for what to write, try these if there are any untested.

Keep your tests independent of each other


Try writing tests in such a way as to make them as independent as possible. Conditions that cause one
test to fail shouldn't (ideally) cause other tests to fail as well.

Use fine-grained Interfaces liberally


When you are extracting interfaces keep them small. Interfaces are meant to be focused, so keep them
that way. An interface that declares too much is a smell. Interfaces with less than three declarations are
great.
An advantage of small, focused interfaces is that they make it easier to create and maintain mock
implementations.

Avoid System.out and System.err in your tests


A few lines of output per test look fine when running a small number of tests, but when you're running a
suite of 1,000-plus tests, it just becomes so much screen garbage. Worse than that it can actually slow
down the tests. One approach that you can take if there are log messages being generated is to reassign
the log's output stream in the se tUp() and tea r D own ( ) methods.
If you can check something visually, try to do the same with an assertion instead. After all, these tests
are supposed to be automated, with programmatic assertions to do the work.

Avoid testing against databases and network resources


When you are testing code that talks to a database or communications system (serial, wireless, network,

etc.) use interfaces to decouple the code from the actual resources. Then you can use mocks (see
Chapter 7 for more on mocks).
This decoupling is a good idea in any event, regardless of whether you are using TDD.

Add a mm aa i nn (( ) to your test cases


In each of your test cases, or suites for that matter, put in a ma i n( ) method that runs that test in the
textui TestR unn er . The following would suffice:
pub l ic cl ass T est So meth ing exte n d s T e s tCa s e {
/ / ...
p u blic sta ti c v oi d ma in(S trin g [ ] a r g s) {
junit .te xt ui. Te stRu nner .run ( T est S o met h i ng .c la s s) ;
}
}

Doing this lets you easily run any test from the command line or other tool.

Start with the assert


When you are writing a test, start by writing what you are testing: the assert. Then work backward and
fill in whatever is needed to set things up for the assert.

Always write a t oo SS t rr ii n g ( )) method


The text of various JUnit failure reports use the t o S tri n g( ) method of the expected and/or actual
objects involved in the assertion. If meaningful implementations of to S tr in g () are provided, these
failure reports will be more informative, saving time and effort.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
There are a variety of frameworks available for writing unit/programmer tests in a variety of languages.
The most effective are those that allow you to develop tests in the same language and same
environment as the code being tested. In this book we use the xUnit family of test frameworksJUnit,
in particular, since we are working in Java.
We looked at the architecture of JUnit, its assertion statement syntax, and the structure and style of
good JUnit Tes tC ase s and test methods. We looked at how to run JUnit tests and interpret the results.
We looked at the JUnit plugin for the Eclipse IDE, and their TDD-friendly tight integration with each
other. We discussed automatically running tests with an automated build system. We then discussed the
differences and best uses of T estC ase and T e stS u i te. We then discussed the topic of organizing test
code in relation to production code, and finally closed with a number of JUnit-related TDD tips.
Part IV provides a look at the xUnit implementation for some other languages.
The xUnit family of frameworks are useful in any development context, but in eXtreme Programming
projects their use is nearly universal. In a sense, XP and xUnit grew up together. The articulator of XP,
Kent Beck, was also the author of SUnit (the original member of the xUnit family) and an author of
JUnit. In the spirit of the Agile development process movement, JUnit is barely sufficient for the task. It
provides just enough overhead to accomplish the task, and no more.
Another reason for JUnit's success is the fact that it is open source. This has allowed its users to create
extensions and tools that work with JUnit to provide more complete or wide-reaching solutions to the
challenge of testing. We cover a number of those extensions in the next chapter.

Amazon

Prev

don't be afraid of buying books

Chapter 5. JUNIT EXTENSIONS


extended more than can be thought to begin from such a cottage
Winter's Tale, Act 4 Scene 2 William Shakespeare
This chapter discusses some of the useful extensions that have been made to JUnit. Some come with
JUnit while others are independent efforts. I have the good fortune to have become friends with the
creators of several of the extensions we will be looking at in this chapter. They have agreed to write
about the extensions they've created.

Next

Amazon

Prev

don't be afraid of buying books

Next

STANDARD EXTENSIONS
This section looks at the set of extensions that come as part of JUnit, in the package
j un i t. ex ten s ions .

ActiveTestSuite
This is a simple extension that allows you to write/run tests that must interoperate in realtime. When
the suite is run, it runs each test in its own thread and waits until all terminate before completing. This
is a subclass of Test S uite , so to use this capability we simply need to instantiate A c t i v e T e s t S u i t e
and add tests to it rather than to a Tes t S u i t e .

ExceptionTestCase
There are two ways to test that an Exc e p t i o n is being thrown when it should be. One is to do it
explicitly, failing immediately following the statement that should throw it:
p ub l ic v oid test F orTh r ow() {
try {
sh ou ldT h row( ) ;
fa il ("E x pect e d Sp e cial Exce p t i o n " ) ;
}
c at ch (S p ecia l Exce p tion e) {
}
}

There is another way, which I do not recommend: E x c e p t i o n T e s t C a s e . To do that, write the test
ignoring the exception that should be thrown and create a T e s t C a s e using E x c e p t i o n T e s t C a s e :
p ub l ic c las s Thr o wing T est exte n d s E x c e p t i o n T e s t C a s e {
p u bl ic vo i d te s tFor T hrow () {
sh ou ldT h row( ) ;
}
}

Then you create the tests, passing the E x c e p t i o n to be thrown as well as the name of the test to run:
n ew Th ro win g Test ( "tes t ForT hrow " , S p e c i a l E x c e p t i o n . c l a s s ) ;

To use this effectively you will need to maintain your own s u i t e ( ) method in any test cases that are
subclasses of Exce p tio nT estC ase, in order to supply the E x c e p t i o n that should be thrown. For the
above example you would need the following:
p ub l ic s tat i c Te s t su i te() {
T e st Su ite suit e = n e w Te stSu i t e ( ) ;
s u it e. add T est( n ew T h rowi ngTe s t ( " t e s t F o r T h r o w " , S p e c i a l E x c e p t i o n . c l a s s ) ) ;
r e tu rn su i te;
}

This approach is more limited than doing the E x c e p t i o n check explicitly (as in the first code snippet).
Firstly, having the exception explicit in the test communicates intent better. Secondly, you can no longer
rerun a single test method.

TestDecorator
This extension is used to wrap some additional behavior around a test run. To do this, you subclass
T es t De co rat o r and overload the r un ( ) method to do whatever is required around a call to
b as i cR un ().The following two extensions are built using T e s t D e c o r a t o r . Note that any T e s t can be
decorated.
I think a simple example is in order. Let's assume that we would like a test to be a bit more
instrumented when run on the command line. We can use T e s t D e c o r a t o r to achieve this.
p ub l ic c las s Ins t rume n tedT est e x t e n d s T e s t D e c o r a t o r {
p u bl ic In s trum e nted T est( Test a r g 0 ) {
su pe r(a r g0);
}

p u bl ic vo i d ru n (Tes t Resu lt r e s u l t ) {
Sy st em. o ut.p r intl n ("St arti n g " + f T e s t . t o S t r i n g ( ) ) ;
ba si cRu n (res u lt);
}

And here's an example of how we could use this:


p ub l ic c las s Ins t rume n tedT estT e s t e r e x t e n d s T e s t C a s e {
p u bl ic vo i d te s tMet h od() {
// .. .
}
p u bl ic st a tic T est s uite () {
Te st Sui t e su i te = new Test S u i t e ( ) ;
Te st te s t = n ew I n stru ment e d T e s t T e s t e r ( " t e s t M e t h o d " ) ;
su it e.a d dTes t (new Inst rume n t e d T e s t ( t e s t ) ) ;
re tu rn s uite ;
}

p u bl ic st a tic v oid m ain( Stri n g a r g s [ ] ) {


ju ni t.t e xtui . Test R unne r.ru n ( s u i t e ( ) ) ;
}

The output from running this code is:


S ta r ti ng te s tMet h od(I n stru ment e d T e s t T e s t e r )
.
T im e : 0. 036
O K ( 1 te sts )

RepeatedTest
This class lets us run a test a specified number of times. To use it, we simply create an instance,
passing it the test to be repeated and the number of times to repeat it. R e p e a t e d T e s t is a subclass of
T es t De co rat o r so we can wrap it around any instance of T e s t .
Here's an example of a use of Rep eate d T e s t which will run t e s t M e t h o d ( ) ten times:
p ub l ic c las s Rep e ated T estT este r e x t e n d s T e s t C a s e {
p u bl ic vo i d te s tMet h od() {
}
p u bl ic st a tic v oid m ain( Stri n g a r g s [ ] ) {

Te st te s t = n ew R e peat edTe s t T e s t e r ( " t e s t M e t h o d " ) ;


ju ni t.t e xtui . Test R unne r.ru n ( n e w R e p e a t e d T e s t ( t e s t , 1 0 ) ) ;

And here's the output from the textual T e s t R u n n e r :


. .. . .. .. ..
T im e : 0. 096
O K ( 10 t est s )

TestSetup
This class is a Decorator that allows us to set up and tear down a fixture that stays in place for the
duration of running the decorated test. Recall that the overriding goal is to have tests totally
independent and not sharing a fixture. There are resources that are expensive enough that we don't
want them to be allocated and freed for each test. That would cause the tests to run too slowly. For
these cases, we can use TestS etup to do one-time setup and teardown for an entire set of tests (that
are in a Te stS u ite). Resources that this could be done for include such things as database or network
connections. Another approach is the use of T e s t R e s o u r c e that is described later.
Use this decorator by subclassing it and overriding s e t U p ( ) and t e a r D o w n ( ) . Here's a trivial example.
It uses a Re p eate d Tes t to run a trivial test five times (to show the local fixture), and decorates that
with an instance of a Te st Setu p subclass, S e t u p E x a m p l e (to show the global fixture).
p ub l ic c las s Set u pExa m ple exte n d s T e s t S e t u p {
p u bl ic Se t upEx a mple ( Test arg 0 ) {
su pe r(a r g0);
}
p u bl ic vo i d se t Up() {
Sy st em. o ut.p r intl n ("Se ttin g u p g l o b a l f i x t u r e . . . " ) ;
}

p u bl ic vo i d te a rDow n () {
Sy st em. o ut.p r intl n ("Te arin g d o w n g l o b a l f i x t u r e . . . " ) ;
}
}
p ub l ic c las s Tes t ForS e tupE xamp l e e x t e n d s T e s t C a s e {
p u bl ic vo i d te s tMet h od() {
Sy st em. o ut.p r intl n ("te stMe t h o d " ) ;
}
p u bl ic vo i d se t Up() {
Sy st em. o ut.p r intl n ("\n loca l s e t U p " ) ;
}
p u bl ic vo i d te a rDow n () {
Sy st em. o ut.p r intl n ("lo cal t e a r D o w n " ) ;
}

p u bl ic st a tic v oid m ain( Stri n g a r g s [ ] ) {


Te st te s t = n ew T e stFo rSet u p E x a m p l e ( " t e s t M e t h o d " ) ;
Te st re p eate d Test = ne w Re p e a t e d T e s t ( t e s t , 5 ) ;
ju ni t.t e xtui . Test R unne r.ru n ( n e w S e t u p E x a m p l e ( r e p e a t e d T e s t ) ) ;
}

Here's the output that shows the global and local fixtures being set up and torn down:
S et t in g up g loba l fix t ure. ..
.
l oc a l se tUp
t es t Me th od
l oc a l te arD o wn
.
l oc a l se tUp
t es t Me th od
l oc a l te arD o wn
.
l oc a l se tUp
t es t Me th od
l oc a l te arD o wn
.
l oc a l se tUp
t es t Me th od
l oc a l te arD o wn
.
l oc a l se tUp
t es t Me th od

l oc a l te arD o wn
T ea r in g dow n glo b al f i xtur e...
T im e : 0. 037
O K ( 5 te sts )

Amazon

Prev

don't be afraid of buying books

Next

ADDING MISSING ASSERTS WITH MockObjects


MockObjects is a framework to make it easier to create mocks of classes. We'll explore this important
subject later in Chapter 7. Here we are restricting our-selves to looking at two classes in the
MockObjects frameworkA ssert Mo and T e s tCa s e Mothat are useful for writing programmer tests in
general.
Ass e rtMo extends As ser t from the JUnit framework and adds several assertion methods that are
lacking in the standard JUnit distribution: [1]
[1] In keeping with the philosophy of the folks who wrote MockObjects, these assert methods are
available only in a version with the initial message parameter.

assertEquals for Object[]


ass e rtEqu als (S tri ng mes sage , Ob j e ct[ ] ex p e ct ed , O bj e ct [ ] a ct u al )

This is another version of ass ertE quals that asserts that the length of two arrays is the same, and
that each pair of objects at a given index are equal.

assertExcludes
ass e rtExc lud es (St ri ng m essa ge, S t rin g sub s t ri ng , S tr i ng t a rg et )

This asserts that su bs tri ng does not appear in t arg e t .

assertIncludes
ass e rtExc lud es (St ri ng m essa ge, S t rin g sub s t ri ng , S tr i ng t a rg et )

As as s ertEx clu de s() , but asserts that su b s tri n g does appear in ta rg e t.

assertStartsWith
ass e rtSta rts Wi th( St ring mes sage , Str i n g p r e fi x, S t ri n g ta r ge t)

This asserts that pr ef ix appears at the beginning of ta r ge t .

Amazon

Prev

don't be afraid of buying books

Next

PERFORMANCE AND SCALABILITY WITH JUnitPerf


wi th M ike C lar k
This section was contributed by Mike Clark of Clarkware Consulting [URL 56], the developer
and maintainer of JUnitPerf [URL 15].

Introduction
Good mountain bikers first learn to meticulously pick their way through every obstacle on every trail. Great
mountain bikers must then learn to disregard smaller obstacles, crudely plowing over them to conserve
energy for the toughest terrain. Similarly, good programmers optimize every line of code using a variety of
techniques, but great programmers learn to write simple code first. They save optimizations for hot spots
and performance-critical sections of code. The trick is identifying the most important code sections and
continually measuring them over time as the software undergoes change.
JUnitPerf is an open source JUnit extension that uses the decorator pattern to adorn standard JUnit tests as
performance or scalability tests. A Time dT es t measures the elapsed time of an existing test. A Lo a dT es t
runs an existing test with a simulated number of concurrent users and iterations. Using JUnitPerf tests we
can transparently leverage our existing tests to ensure that the functionality being tested continues to meet
performance and scalability requirements over time.
Let's put JUnitPerf to the test with an example.

Tackling New Customer Stories


Our team is building a Web-based application for the medical industry. Today's particular story involves
querying the database for all prescriptions written by a given doctor, then displaying the prescription
information to the user.
We started by writing clean and simple code that expressed intent, driven by tests that validate that the
code is doing the right things. We've been wisely deferring any premature code optimizations because we
don't want to speculate and overspend on performance until we identify a problem. But now that we've
demonstrated working code, our customer decides there's value in improving the performance and scalability
of this story.
We don't want to compromise the reliability of prescription queries by complicating the code for performance
and scalability. JUnitPerf tests will fail if the tests they decorate fail, so we have confidence that tweaking
performance won't adversely affect existing functionality.

Tuning with Confidence


Our Web page is taking approximately 5.0 seconds to display 100 rows of prescription information. That's
less than ideal for our users. So our customer writes a story stating that the response time of a Web page
containing up to 100 prescriptions should not exceed 3.0 seconds. We've got our work cut out for us!
Many things happen behind that curtain of a Web page. We need to know which sections of the code
contribute the most to the page's overall response time. We will then focus on tuning those performancecritical areas first. We fire up our favorite profiling tool, hit the Web page, and let the profiler hunt down the
busiest code. Alas, it identifies the Pharm a cy. f i ndP r e scr i pt i on sB y Pr e sc ri b er () method,
contributing approximately 3.5 seconds to the total response time.
Thankfully, we already have the following JUnit test for that method:

pu bli c void te st Fin dP resc ript ions ( ) th r o ws E xc e pt io n {


Pre s cript ion Da tab as e da taba se = n ew P r esc r ip t io nD a ta b as e( ) ;
Pha r macy pha rm acy = new Pha rmac y ( dat a b ase ) ;
Col l ectio n p re scr ip tion s = phar m a cy. f i ndP r es c ri pt i on s By Pr e sc ri b er (" D r. J e ky l" ) ;
ass e rtEqu als (1 00, p resc ript ions . s ize ( ) );

Ite r ator pre sc rip ti onIt er = pre s c rip t i ons . it e ra to r () ;


whi l e (pr esc ri pti on Iter .has Next ( ) ) {
P r escri pti on p = (Pre scri ptio n ) pr e s cri p ti o nI te r .n e xt () ;
a s sertE qua ls ("D r. Jek yl", p.ge t P res c r ibe r () ) ;
}

The test validates that indeed every prescription written by Dr. Jekyl is returned. The Ph a rm ac y class
employs a Pre sc rip ti onD ataba se to perform the prescription query, without worrying about what kind
of persistence strategy is being used under the hood. In this case a one-time test fixture was used to
prepopulate the P res cr ipt ionDa tabas e with exactly 100 prescriptions signed by the villainous Dr. Jekyl.
We're glad we wrote well-factored code from the start because in doing so we enabled the profiler to point
us towards a single cohesive method that we can tune for maximum benefit. If we could only boost the
performance of the Ph arm ac y.fi ndPr es cr i p tio n s ByP r es c ri be r () method, our Web users would be
much happier. But how do we know when to stop tuning? Write an automated test for the performance
story, of course. When it passes, we're done!

Using TimedTest for Performance Testing


With the performance story in hand, we write a P har m a cyT i me d Te st that creates a T im ed T es t instance
passing in the existing P ha rmac yTes t.te s t Fin d P res c r ip ti on s () test case method and a maximum
elapsed time of 1.5 seconds. By default, the T i m edT e s t will wait for the completion of its decorated test
and then fail if the maximum elapsed time was exceeded. The Web page contributes another 1.5 seconds in
presentation overhead, so when this test passes we'll be on target for a 3.0 second total response time.
As a convenient way to run the Ph armac y Tim e d Tes t , we define a s u it e( ) method called by a
Te stR u nner in the ma in () method, as follows:
pu bli c clas s P ha rma cy Time dTes t ex t e nds T est C as e {
pub l ic st ati c Tes t suit e() {
l o ng ma xEl ap sed Ti meIn Mill is = 1 500 ;
T e st te st = new P harm acyT est( " t est F i ndP r es c ri pt i on s ") ;
T e st ti med Te st = new Time dTes t ( tes t , ma x El a ps ed T im e In Mi l li s) ;
r e turn tim ed Tes t;
}

pub l ic st ati c voi d main (Str ing a r gs[ ] ) {


j u nit.t ext ui .Te st Runn er.r un(s u i te( ) ) ;
}

We run the Pha rm acy Ti medT est and it fails with the following output:
.
Ti med T est ( WAI TI NG) : test Find Pres c r ipt i o ns( P ha r ma cy T es t ): 3 5 16 m s
F
Ti me: 3.516
Th ere was 1 fa il ure :
1) te s tFind Pre sc rip ti ons( Phar macy T e st)
ju nit . frame wor k. Ass er tion Fail edEr r o r:

Ma xim u m ela pse d tim e exce eded ! Ex p e cte d 150 0 ms , b ut wa s 3 51 6 ms .


FA ILU R ES!!!
Te sts run: 1, Fa ilu re s: 1 , Er rors : 0

The expected elapsed time was exceededwe expected 1.5 seconds but it actually took 3.5 seconds. We're
not surprised, and now we have a goal to work towards, as well as a test that tells us if we're getting
warmer or colder.
We then go after some low-hanging fruit: we tune the underlying SQL a bit and optimize some of the
object-relational mapping logic necessary to create P res c r ip ti on objects from database rows. Then we
run the Pharm acy Ti med Te st again and it passes with the following output:
.
Ti med T est ( WAI TI NG) :
te stF i ndPre scr ip tio ns (Pha rmac yTes t ) : 1 4 5 2 m s
Ti me: 1.452
OK (1 tests )

Much better! We're making progress. Had the test failed we would have continued to optimize until it
passed, but no further. Now that we have quantitative performance criteria in the form of an automated
test, the test will continue to keep its performance in check as we refactor the code.
The elapsed time measured by a Tim e d Te st decorating a single
t est XX X() test case method includes the total time of the s e tU p( ) ,
t est XX X(),and tear Do wn ( ) methods. For example, if the 100 test
prescriptions were added to the database in the se tU p () method, then
the T im edTe st would include this setup overhead. Therefore, the
maximum elapsed time should be adjusted accordingly to take into
account the setup and tear-down costs of the decorated test. One
technique for establishing a baseline for this overhead is to time an empty
t est XX X() method.

Scaling Up
We've successfully demonstrated single-user response time with a repeatable test. We haven't thought much
about multiuser performance because up until now the scalability requirements were unknown. Instead,
we've been relying on simple, modular code capable of responding when the customer writes a scalability
story. Now that it's a bit later in the project our customer has a better estimate of the expected load on the
production system.
It is expected that multiple pharmacies will be hitting the same Web page concurrently. So the customer
writes a scalability story that states that the response time of a Web page containing up to 100
prescriptions should not exceed 10.0 seconds under a load of 10 concurrent users.

Using LoadTest for Scalability Testing

As with the performance story, we want to write an automated test that specifies the expected response
time of the existing code under load, then tune until it passes. We write a P ha r ma cy L oa dT e st that
creates a Loa dT est instance passing in a Tim e d Tes t and a number of concurrent users (threads). The
Ti med T est in turn decorates the existing P h a rma c y Tes t .t e st Fi n dP r es cr i pt io n s( ) test case
method.
Similar to the Ti medT es t, the L oad T e st decorates at the granularity of
the s et Up() , t estX XX () ,and t ear D o wn () method sequence. By
default, the Loa dTes t uses the same instance of the decorated test for
all concurrent users. Therefore, each user will run s et U p( ),
t est XX X(),and tear Do wn ( ) concurrently with the same test instance.
If transient test data is being created in the se t Up () method of the
decorated test, use the T e s tFa c t ory class to ensure that each
concurrent user operates on its own instance of the decorated test. If
persistent test data is being created in the s et U p( ) method, as in the
case of the Pres crip ti on D a tab a s e, consider using the
j uni t. exte nsio ns.Te s tSe t u p class to create a one-time test
fixture.

The L o adTes t will run the Ti medT est with 10 concurrent users (threads), and each user will run the
Ph arm a cyTes t.t es tFi nd Pres crip tion s ( ) test case method once. We calculate 7.0 seconds to be a
sufficient response time under load based on the Web page incurring 1.5 seconds of additional overhead.
We write the following test that will fail if any user's response time exceeds 7.0 seconds:
pu bli c clas s P ha rma cy Load Test ext e n ds T e stC a se {
pub l ic st ati c Tes t suit e() {
i n t use rs = 10;
i n t ite rat io ns= 1;
l o ng ma xEl ap sed Ti meIn Mill is = 7 000 ;
T e st te st = new P harm acyT est( " t est F i ndP r es c ri pt i on s ") ;
T e st ti med Te st = new Time dTes t ( tes t , ma x El a ps ed T im e In Mi l li s) ;
T e st lo adT es t = n ew L oadT est( t i med T e st, us e rs , i te r at io n s) ;
r e turn loa dT est ;
}

pub l ic st ati c voi d main (Str ing a r gs[ ] ) {


j u nit.t ext ui .Te st Runn er.r un(s u i te( ) ) ;
}

We chose this order of test decoration because the scalability story explicitly related to each user's individual
response time under load. If instead the story were expressed in terms of throughput, we could switch the
order of test decoration such that the Time d Tes t decorated the L oa dT e st which in turn decorated the
Ph arm a cyTes t. This would fail if the cumulative response time of all concurrent users exceeded a
threshold.
We run the Pha rm acy Lo adTe st and it fails with the following output:
.. ... . ....
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :

test Find Pres c r ipt i o ns( P ha r ma cy T es t ):


test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):

1 5 22
2 3 53
3 1 75
4 0 16
4 8 37
5 6 78

ms
ms
ms
ms
ms
ms

Ti med T est
Ti med T est
F
Ti med T est
F
Ti med T est
F

( WAI TI NG) : test Find Pres c r ipt i o ns( P ha r ma cy T es t ): 6 5 19 m s


( WAI TI NG) : test Find Pres c r ipt i o ns( P ha r ma cy T es t ): 7 3 71 m s
( WAI TI NG) : test Find Pres c r ipt i o ns( P ha r ma cy T es t ): 8 2 12 m s
( WAI TI NG) : test Find Pres c r ipt i o ns( P ha r ma cy T es t ): 9 0 63 m s

Ti me: 9.303

Th ere were 3 f ai lur es :


1) te s tFind Pre sc rip ti ons( Phar macy T e st)
ju nit . frame wor k. Ass er tion Fail edEr r o r:
Ma xim u m ela pse d tim e exce eded ! Ex p e cte d 700 0 ms , b ut wa s 7 37 1 ms .
2) te s tFind Pre sc rip ti ons( Phar macy T e st)
ju nit . frame wor k. Ass er tion Fail edEr r o r:
Ma xim u m ela pse d tim e exce eded ! Ex p e cte d 700 0 ms , b ut wa s 8 21 2 ms .
3) te s tFind Pre sc rip ti ons( Phar macy T e st)
ju nit . frame wor k. Ass er tion Fail edEr r o r:
Ma xim u m ela pse d tim e exce eded ! Ex p e cte d 700 0 ms , b ut wa s 9 06 3 ms .
FA ILU R ES!!!
Te sts run: 10, F ail ur es: 3, E rror s : 0

Again, we're not surprised because we didn't concern ourselves with scalability when we wrote the original
code. We notice that the response times increased for each successive user until the last three users failed
the test. It appears to be a classic case of resource contention creating a bottleneck in our system. We
quickly conclude that concurrent users must be contending for a single database connection. Ideally, we
want to maximize our time by making a change with low cost and a high reward, so we decide to make a
minor change to the P re scri ptio nData b ase class to use a database connection pool with 10 active
connections. This should help the query scale.
We run the Pha rm acy Lo adTe st again and it passes with the following output:
.. ... . ....
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :
Ti med T est ( WAI TI NG) :

test Find Pres c r ipt i o ns( P ha r ma cy T es t ):


test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):
test Find Pres c r ipt i o ns( P ha r ma cy T es t ):

6 8 30
6 7 90
6 7 80
6 8 50
6 9 10
6 8 50
6 8 40
6 8 70
6 9 10
6 9 50

ms
ms
ms
ms
ms
ms
ms
ms
ms
ms

Ti me: 7
OK (1 0 test s)

Excellent! The scalability tests are passing and the decorated test continues to pass as well. We notice that
the response times are nearly consistent for each user there by confirming our theory about resource
contention. If scalability starts to degrade in the future, the test will let us know by failing.

Other Applications of JUnitPerf

Because JUnitPerf is based on decorating any class that implements JUnit's Test interface, we can use
JUnitPerf tests to measure the performance and scalability of other areas, including:
Web pages, by decorating HttpUnit tests
servlets and Enterprise JavaBeans (EJB), by decorating Cactus tests
algorithms and data structures
changes to the database
impacts of switching third-party software
As we write more performance and scalability tests, they may take a while to run, and we don't want these
types of tests slowing down our development pace. We'll organize them in a special test suite to be run at
least once a day. We won't run this test suite during our test-driven development cycle, unless of course
we're fiddling with performance-critical code.

Conclusion
It's all too easy to optimize early only to find out that the optimized code isn't executed frequently. Equally
troubling and expensive are intended optimizations that instead degrade performance and scalability. It's
always best to write simple code, identify performance-critical sections of code when the reward is high, and
then create benchmarks in the form of repeatable JUnitPerf tests. The tests help reduce the stress of tuning
and retain their value over time by failing whenever the response time and throughput of the software move
in the wrong direction. They can be run automatically to continually measure performance in the face of
change. Moreover, the tests tell us when to stop tuning and deliver the software.

Amazon

Prev

don't be afraid of buying books

Next

DAEDALOS JUNIT EXTENSIONS


w it h J en s U w e Pi p ka
This section was contributed by Jens Uwe Pipka of Daedalos Consulting GmbH [URL 57],
the author and maintainer of The Daedalos JUnit Extensions[URL 17].
The Daedalos JUnit Extensions, or in short djux, provide additional tools to speed up programmer testing.
The core components are test resources. Using test resources, it is possible to make static resources
available for all tests during the whole development cycle. So, time-consuming initializations are done
only once and remain active over a complete series of test runs. Furthermore, djux includes additional
tools for database tests as well as for the integration of external test programs. You can download the
latest version from the djux homepage.
We will present an example of how you can implement and use your own test resources and show how
tests are sped up. We will implement a test that checks entries inside a database. To do this, the test
operates as follows:
a database connection must be opened
the test checks if certain data exists in the database (this unit test is implemented using the
database checker that also comes with djux)
the database connection has to be closed.

A Programmer Test Implementation Using Test Resources


We start with a standard JUnit test. Our test case is named M y D a t a b a s e T e s t and contains a class
variable for the database name. We also need instance variables for the database connection as well as
for our database checker. Furthermore, the constructor registers the appropriate driver to access our
database. Here's the code:
p ub l ic c las s MyD a taba s eTes t ex t e n d s T e s t C a s e {
p r iv at e s t atic fina l Str ing D A T A B A S E N A M E = " j d b c : o d b c : M y D a t a b a s e " ;
p r iv at e D a taba s eChe c ker dbCh e c k e r = n u l l ;
p r iv at e C o nnec t ion c onne ctio n = n u l l ;

p u bl ic My D atab a seTe s t(St ring n a m e ) t h r o w s S Q L E x c e p t i o n {


su pe r(n a me);
Dr iv erM a nage r .reg i ster Driv e r ( n e w s u n . j d b c . o d b c . J d b c O d b c D r i v e r ( ) ) ;
}

Next, we implement the se t Up() method to open the database connection and initialize the database
checker. This is done by asking the DriverManager for the connection and passing it to the database
checker's constructor.
p ub l ic v oid setU p () t h rows SQL E x c e p t i o n {
c o nn ec tio n = D r iver M anag er.g e t C o n n e c t i o n ( D A T A B A S E N A M E ) ;
d b Ch ec ker = ne w Dat a base Chec k e r ( c o n n e c t i o n ) ;
}

We also need to implement t earD own( ) to close the database connection properly:
p ub l ic v oid tear D own( ) thr ows S Q L E x c e p t i o n {
i f ( co nne c tion != n u ll) {
co nn ect i on.c l ose( ) ;
co nn ect i on = null ;
db Ch eck e r = n ull;

After implementing the necessary actions to open and close the database connection, we can start with
the actual test. First, the existence of the table B O O K S is checked. Using the djux database checker, this
can be done fairly easily:
p ub l ic v oid test T able B ooks Exis t s ( ) t h r o w s S Q L E x c e p t i o n {
a s se rt Tru e (dbC h ecke r .tab leEx i s t s ( " B O O K S " ) ) ;
}

Now we can run our first test. Assuming that the database is set up properly and the table B O O K S really
exists, the test is successful. Now we can go on with defining additional tests; for example, one that
checks that the table does not contain any records:
p ub l ic v oid test T able B ooks Real l y E m p t y ( ) t h r o w s S Q L E x c e p t i o n {
a s se rt Equ a ls(" T here shou ld b e n o r e c o r d s " , 0 , d b C h e c k e r . r o w C o u n t ( " B O O K S " ) ) ;
}

Running the unit test again, it takes some time before the results are displayed. This is because for each
test a new connection is opened and closed. This gets even worse if you are working with a remote
database. This overhead can be reduced by implementing an appropriate T e s t S e t u p instead of using
the s et Up () and t e arDo w n() inside the test case, but the connection is still opened and closed for
each test run. Furthermore, it gets even more complex if several test cases refer to the same database
connection. It then becomes necessary to implement specific T e s t S u i t e s considering the different test
runs.
Here is where the idea of test resources comes into play: using them, it is possible to open the database
connection once and access it during the whole testing cycle. It is no longer necessary to reopen the
database connection before a test run is executed.
But what does this mean for our implementation? Let's have a look at our test case. We only have to
change the s e tUp( ) and tear Down () methods. Instead of handling the database connection there, we
implement the functionality inside a test resource.
Generally, a test resource can be implemented like this:
1. Define the new resource as a subclass of c o m . d a e d a l o s . j u n i t . T e s t R e s o u r c e
2. Override star t () to initialize your resource
3. Override stop ( ) to release your resource
4. Register your new test resource with T e s t R e s o u r c e F a c t o r y
5. Afterwards, you can access your test resource directly within your test cases.
Now we are going to define the connection to our database as a T e s t R e s o u r c e . We start refactoring
our existing testcase and implement a new class, D b C o n n e c t i o n T R , that inherits from
c om . da ed alo s .jun i t.Te s tRes ourc e . This new class holds the database connection. Therefore, the
constructor is used to register the appropriate driver. Furthermore, we add an instance variable,
c on n ec ti on, that holds the database connection. Finally, we define a constant for the database name.
p ub l ic c las s DbC o nnec t ionT R ex t e n d s T e s t R e s o u r c e {
p r iv at e s t atic fina l Str ing D A T A B A S E N A M E = " j d b c : o d b c : M y D a t a b a s e " ;
p r iv at e C o nnec t ion c onne ctio n = n u l l ;

p u bl ic Db C onne c tion T R() thro w s S Q L E x c e p t i o n {


su pe r() ;
Dr iv erM a nage r .reg i ster Driv e r ( n e w s u n . j d b c . o d b c . J d b c O d b c D r i v e r ( ) ) ;
}

Now we have to implement the inherited abstract methods s t a r t ( ) and s t o p ( ) . These methods are

called when the test resource is started at the beginning and stopped at the end of the test cycle,
respectively. We want to open the database connection when the T e s t R e s o u r c e is started and close it
at the end of the development cycle. So, we move this functionality from the test case to the
corresponding methods in the test resource.
The method s t art( ) tries to open the database connection. If it is successful, the return value is t r u e ,
otherwise f als e is returned. The return value is essential to enable the T e s t R e s o u r c e F a c t o r y to
manage the state of the existing test resources.
p ub l ic b ool e an s t art( ) {
try {
co nn ect i on = Driv e rMan ager . g e t C o n n e c t i o n ( D A T A B A S E N A M E ) ;
} ca tc h ( S QLEx c epti o n ex c) {
re tu rn f alse ;
}
r e tu rn tr u e;
}

The method s t op() closes the database connection. Again, the return value is t r u e if the operation
was successful and fa l se otherwise:
p ub l ic b ool e an s t op() {
try {
if ( con n ecti o n != null ) {
co nne c tion . clos e ();
co nne c tion = nu l l;
}
} ca tc h ( S QLEx c epti o n e) {
re tu rn f alse ;
}
r e tu rn tr u e;
}

Now we provide a getter method to access the database connection:


p ub l ic C onn e ctio n get C onne ctio n ( ) {
r e tu rn co n nect i on;
}

It is also possible to provide a short description of the test resource. This is an optional but useful way
to manage different test resources. To do this, we override the method g e t D e s c r i p t i o n ( ) by
returning the description:
p ub l ic S tri n g ge t Desc r ipti on() {
r e tu rn "A basi c tes t res ourc e e x a m p l e f o r h o l d i n g a d a t a b a s e c o n n e c t i o n " ;
}

As you can see, the implementation is exactly as in our test case before. All we have done is move the
functionality to another class.
Finally, we have to adapt our test case itself. The test case has to inherit from the class
c om . da ed alo s .jun i t.Te s tRes ourc e T e s t C a s e that is again a subclass from T e s t C a s e . In
addition to moving the implementation for opening and closing the database to the test resource class,
we must change the database access inside our test case. Instead of the database connection being
accessed directly as in the original implementation, it is now accessed via the D b C o n n e c t i o n T R class.

This is done by using the T estR esour c e F a c t o r y that is the unique interface to handle test resources
inside your tests. It uses the singleton pattern. To use a specific T e s t R e s o u r c e inside a test case, the
method p re par eT est R esou r ce() is expected. The argument is the name of the test resource, for
example, Db C onne c tio nT RStr ing in our example. After registering the test resource, you can access
it using the method ge t Test R esou rce ( ) ,which also expects the test resource name as argument.
After these modifications, our test case looks like this:
p ub l ic c las s MyD a taba s eTes t ex t e n d s T e s t R e s o u r c e T e s t C a s e {
p r iv at e D a taba s eChe c ker dbCh e c k e r = n u l l ;
p u bl ic vo i d se t Up() {
pr ep are T estR e sour c e("D bCon n e c t i o n T R " ) ;
Te st Res o urce F acto r y tr Fact o r y = T e s t R e s o u r c e F a c t o r y . c u r r e n t ( ) ;
Db Co nne c tion T R db T estR esou r c e =
(D bCo n nect i onTR ) trF acto r y . g e t T e s t R e s o u r c e ( " D b C o n n e c t i o n T R " ) ;
db Ch eck e r = n ew D a taba seCh e c k e r ( d b T e s t R e s o u r c e ) ;
}

p u bl ic vo i d te s tTab l eBoo ksEx i s t s ( ) t h r o w s S Q L E x c e p t i o n {


as se rtT r ue(d b Chec k er.t able E x i s t s ( " B O O K S " ) ) ;

pu b li c voi d tes t Tabl e Book sRea l l y E m p t y ( ) t h r o w s S Q L E x c e p t i o n {


a ss ert E qual s ("Th e re s houl d b e n o e n t r i e s " ,
0,
dbC h ecke r.ro w C o u n t ( " B O O K S " ) ) ;
}

Now, the test methods themselves are not modified. The most important changes are made inside the
s et U p( ) method. Specifically, the database connection is not opened anymore. Instead, the
D bC o nn ec tio n TR is added to the active test resources. Afterwards, the connection is obtained from
T es t Re so urc e Fact o ry directly. It is necessary to cast the T e s t R e s o u r c e to its definition class if
you want to access user defined methods. The other modifications are rather easy: The constructor now
does nothing else but delegate the work to its parent constructor. And, finally, teardown is no longer
necessary.
That's it! Now, you can run your Tes tC a s e inside your T e s t R u n n e r , for example,
j un i t. sw ing u i.Te s tRun n er. Running the test the first time, nothing seems to be changed: it still
takes a fair amount of time for the test to go green. But try it again: Instantly, the test is green again.
This is because, of course, the database connection must be opened by your first test run. But thereafter
the connection stays open, until you finally close the whole test runner.

Managing Your Test Resources


In the preceding example, the new test resource is accessed via its fully qualified class name, including
the entire package name. Happily, it is possible to map your test resources to logical names instead,
which keeps your test cases cleaner. For example, m y . e x a m p l e . D B C o n n e c t i o n T e s t R e s o u r c e can
be mapped to dbAccess.
For this purpose, the T e stRe s ourc eFa c t o r y provides the method r e g i s t e r T e s t R e s o u r c e ( ) .The
first argument specifies the logical name you want to use for the test resource; the second the class it
refers to. The registering of the Te stRe s o u r c e inside the T e s t R e s o u r c e F a c t o r y is only necessary
one time. If it is not done directly using the T e s t R e s o u r c e F a c t o r y , it can also be done by calling the
mapper class Regi s ter Te stRe sourc e . For the earlier example, you can register your test resource by
executing the following command:
$ j a va - cp j unit e xten s ions .jar : j u n i t . j a r : $ C L A S S P A T H \
> c o m. da eda l os.j u nit. u til. Regi s t e r T e s t R e s o u r c e d b A c c e s s D b C o n n e c t i o n T R

As a result of this command, a new test resource mapping is added to the file
t es t re so urc e s.pr o pert i es in your user directory. Now you can access the test resource inside
your test case with its logical name:
p ub l ic c las s MyD a taba s eTes t ex t e n d s T e s t R e s o u r c e T e s t C a s e {
...
p u bl ic vo i d se t Up() {
pr ep are T estR e sour c e("d bAcc e s s " ) ;
Te st Res o urce F acto r y tr Fact o r y = T e s t R e s o u r c e F a c t o r y . c u r r e n t ( ) ;
Db Co nne c tion T R db T es t Reso u r c e =
(D bCo n nect i onTR ) trF acto r y . g e t T e s t R e s o u r c e ( " d b A c c e s s " ) ;
db Ch eck e r = n ew D a taba seCh e c k e r ( d b T e s t R e s o u r c e ) ;
}
...
}

Furthermore, it is possible to reset your test resources. Simply call T e s t R e s o u r c e F a c t o r y . r e s e t ( )


from within a test case.

Using the TRTestRunner


Until now, a test case that uses a test resource is defined as a subclass from
T es t Re so urc e Test C ase. This class ensures that a specific test resource is accessible inside the test
case, and that it is started automatically when, for example, it is accessed for the first time.
Nevertheless, it is often better to manage the test resources manually in a more comprehensive test
environment. For this purpose, djux provides a specialized test runner based on the original swing test
runner. This test runner also includes a test resource browser that shows the current state of test
resources and enables the user to manage test resources.
You can start the TRTestRunner by executing the following command:
$ j a va - cp j unit e xten s ions .jar : j u n i t . j a r : $ C L A S S P A T H \
> c o m. da eda l os.j u nit. g ui.T RTes t R u n n e r

Then you can open the Test Resource Browser using the TestResourceBrowser entry in the JUnit menu. A
new window pops up that shows the state of the currently registered TestResources (see Figure 5.1).
You can start, stop, and reset test resources with this GUI. Furthermore, it is possible to register new
test resources with their logical and class names, as well as remove them. This is much handier than the
manual operation described earlier. You also have the option to "auto"-start your test resources, so that
all registered test resources are started when you start the TRTestRunner.

Figure 5.1. The Test Resource Browser window.


[View full size image]

Another advantage of the TRTestRunner is that test cases using test resources no longer have to be
subclasses of Test R eso ur ceTe stCas e but can extend T e s t C a s e directly. It is also not necessary to
add your test resource explicitly, because the handling is now done by the TRTestRunner and the
TestResourceBrowser. You only have to access your test resources as known via the
T es t Re so urc e Fact o ry. Moreover, all other test cases will run as known without any limitation or
change.
Using the TRTestRunner, our test case will look as follows:
p ub l ic c las s MyD a taba s eTes t ex t e n d s T e s t C a s e {
p r iv at e D a taba s eChe c ker dbCh e c k e r = n u l l ;
p u bl ic vo i d se t Up() {
Te st Res o urce F acto r y tr Fact o r y = T e s t R e s o u r c e F a c t o r y . c u r r e n t ( ) ;
Db Co nne c tion T R db T estR esou r c e =
(D bCo n nect i onTR ) trF acto r y . g e t T e s t R e s o u r c e ( " d b A c c e s s " ) ;
db Ch eck e r = n ew D a taba seCh e c k e r ( d b T e s t R e s o u r c e ) ;
}
p u bl ic vo i d te s tTab l eBoo ksEx i s t s ( ) t h r o w s S Q L E x c e p t i o n {
as se rtT r ue(d b Chec k er.t able E x i s t s ( " B O O K S " ) ) ;
}

p u bl ic vo i d te s tTab l eBoo ksEx i s t s ( ) t h r o w s S Q L E x c e p t i o n {


as se rtE q uals ( "The r e sh ould b e n o e n t r i e s " ,
0,
dbCh e cker .row C o u n t ( " B O O K S " ) ) ;
}

As you can see, this looks even more like a traditional JUnit test. After starting the dbAccess test
resource via the Test Resource Browser, our test runs again showing the green bar. If the test resource
is stopped, the unit test will fail with an E x c e p t i o n : " T e s t R e s o u r c e n o t a v a i l a b l e " .
The decision about which approach to use to manage your test resources is dependent on your
environment: If you work in a graphical environment, it is much more convenient to use the
T RT e st Ru nne r that is fully compatible with the Swing TestRunner and offers additional tools to manage
your test resources. But if your tests should also work with the AWT or the command-line-based test
runner, you should implement your tests that access test resources as subclasses of
T es t Re so urc e Test C ase. In either case, the test resource implementation is identical, so you can use
them in both environments.

Additional JUnit Test Tools


ExtensibleTestCase
Even though JUnit is a comprehensive testing tool, it is often necessary to also include external testing
tools. To simplify the integration of external testing tools, djux provides an easy-to-use mechanism to
plug in external testing tools as additional test cases: the E x t e n s i b l e T e s t C a s e .
This is based on the class com. Daed al o s . j u n i t . E x t e n s i b l e T e s t C a s e that is again a subclass of
j un i t. fr ame w ork. T estC a se. First, the expected result can be passed as an argument to the method
s et E xp ec ted R esul t ().All you have to do then is to implement the abstract method
r un T es tS ile n tly( ) which should implement the functionality to call the external tool and set the
result value accordingly. Finally, the method p o s t P r o c e s s T e s t ( ) compares the expected result with
the actual one. As an example of using E x t e n s i b l e T e s t C a s e , we have included code that shows how
to integrate the execution of native programs with djux.
Despite setting the expected return value, E x t e n s i b l e T e s t C a s e s are handled like any other standard
test cases and need no additional setup. Therefore, E x t e n s i b l e T e s t C a s e s can be executed using the
standard JUnit TestRunner.

DatabaseChecker
In the previous example, we used the D a t a b a s e C h e c k e r . This is a djux test utility that allows
executing simple database checks. In particular, the database checker provides checks concerning the
database structure as well as its content. It provides a simple and easy way to verify if the application
behavior has the expected effect on the persistent database content. If a middleware persistency
framework is used, this cross check becomes particularly important.
To introduce the database checker in more detail, we continue with our previous example. Here we
develop an application that provides a book list that allows adding and removing books. The
requirements are that the book list be stored immediately in a database table B O O K S each time it is
changed. The table contains the columns T I T L E , A U T H O R , and I S B N .
In the example above, we have already checked that the table BOOKS exists. We continue defining the
test case for the new user story. Therefore, a new test case B o o k L i s t T e s t is created as a subclass of
T es t Ca se . It contains an instance dbC h e c k e r of class D a t a b a s e C h e c k e r that is initialized in the
s et U p( ) method of our test by instantiating the class c o m . d a e d a l o s . d b c h e c k . D a t a b a s e C h e c k e r .
Note that the constructor expects a valid database connection that is used for all database access. Again,
we use the database test resource that we developed in the previous example. We also use an instance
variable booklist that manages the new class representing the book list.
p ub l ic c las s Boo k List T estC ase e x t e n d s T e s t C a s e {
p r iv at e D a taba s eChe c ker dbCh e c k e r = n u l l ;
p r iv at e B o okLi s t bo o klis t;

p u bl ic Bo o kLis t Test C ase( Stri n g n a m e ) {


su pe r(n a me);
}

p u bl ic vo i d se t Up() {
Te st Res o urce F acto r y tr Fact o r y = T e s t R e s o u r c e F a c t o r y . c u r r e n t ( ) ;
Db Co nne c tion T estR e sour ce d b T e s t R e s o u r c e =
(D bCo n nect i onTe s tRes ourc e ) t r F a c t o r y . g e t T e s t R e s o u r c e ( " d b A c c e s s " ) ;
db Ch eck e r = n ew D a taba seCh e c k e r ( d b T e s t R e s o u r c e . g e t C o n n e c t i o n ( ) ) ;
bo ok lis t = n e w Bo o kLis t();
}

Now we are going to define the interface of the B o o k L i s t class by defining the test cases for our user
story. Because all changes have a direct effect on the database, D a t a b a s e C h e c k e r is a valuable tool
to verify the functionality of the BookL i s t class.
First, we have to initialize the book list. After this, it should contain no entries. To check this, we use
the database checker method row Count ( t a b l e N a m e ) that returns the number of rows for a specific
table. In our example, we have to check the table B O O K S :
p ub l ic v oid test T able B ooks Exis t s ( ) t h r o w s S Q L E x c e p t i o n {
b o ok li st. i niti a lize ( );
a s se rt Equ a ls(0 , dbC h ecke r.ro w C o u n t ( " B O O K S " ) ) ;
}

Next, a new book is added. If the book already exists, it should not be added to the book list. To check
if a book is added correctly, we use the method r o w E x i s t s ( t a b l e N a m e , w h e r e C l a u s e ) .It returns
whether or not a row exists in the specified table for the given where clause. The method returns t r u e
if there is exactly one row that fulfills the where clause. If there exists more than one row or the entry
is not found, the return value is false .
p ub l ic v oid test H andl e ABoo k() t h r o w s S Q L E x c e p t i o n {
b o ok li st. i niti a lize ( );
t h is .a sse r tEqu a ls(0 , dbC heck e r . r o w C o u n t ( " B O O K S " ) ) ;

B o ok n ewB o ok=n e wBoo k ("Di nner f o r t w o " , " M i k e G a y l e " , " 0 3 4 0 8 2 3 4 2 9 " ) ;
b o ok li st. a dd(n e wBoo k );
S t ri ng qu e ry = "tit l e=\" Dinn e r f o r t w o \ " " +
"and auth or=\ " M i k e G a y l e \ " " +
"and isbn =\"0 3 4 0 8 2 3 4 2 9 \ " " ;
t h is .a sse r tTru e ("Di n ner for t w o s h o u l d b e t h e r e " ,
dbC h ecke r.ro w E x i s t s ( " B O O K S " , q u e r y ) ) ;
b o ok li st. a dd(n e wBoo k );
a s se rt Tru e ("Di n ner f or t wo s h o u l d s t i l l b e t h e r e " ,
dbC h ecke r .row Exis t s ( " B O O K S " , q u e r y ) ) ;

a s se rt Equ a ls(" F inal l y, o nly o n e e n t r y e x p e c t e d " ,


1,
d b Chec k er.r owCo u n t ( " B O O K S " , " i s b n = \ " 0 3 4 0 8 2 3 4 2 9 \ " " ) ) ;

Here, we add the same book two times. It is the task of the B o o k L i s t class to meet the condition that
there is only one entry for each book. If this doesn't work, the database checker method
r ow E xi st s(t a bleN a me, w here Clau s e ) would cause the test to fail, because more than one entry
is found in the database. To check the simple existence of a specific entry, where more than one entry
is allowed, the database checker provides the method m u l t i p l e R o w s E x i s t s ( t a b l e N a m e ,
w he r eC la use ) that returns true if any row matches.
With database checker method r owCou n t ( t a b l e N a m e , w h e r e C l a u s e ) it is also possible to check
how many entries for the specific entry exists. In the last test case in the example above, we use this
method to verify that there is exactly one entry for the specific I S B N of the new entry.
Now we want to implement a test for removing a book. We add some books and then remove the
second book. Again, we verify each step with the help of the database checker and the methods we
used before:
p ub l ic
B o ok
B o ok
B o ok

v oid test M anag e Book s() t h r o w s S Q L E x c e p t i o n {


b ook 1 = n e w Bo o k("D inne r f o r t w o " , " M i k e G a y l e " , " 0 3 4 0 8 2 3 4 2 9 " ) ;
b ook 2 = n e w Bo o k("H igh F i d e l i t y " , " N i c k H o r n b y " , " 3 4 2 6 6 0 4 3 6 1 " ) ;
b ook 3 = n e w Bo o k("H err L e h m a n n " , " S v e n R e g e n e r " , " 3 8 2 1 8 0 7 0 5 9 " ) ;

b o ok li st. i niti a lize ( );


a s se rt Equ a ls(" N o en t ries exp e c t e d " , 0 , d b C h e c k e r . r o w C o u n t ( " B O O K S " ) ) ;
b o ok li st. a dd(b o ok1) ;
a s se rt Equ a ls(" O ne e n try expe c t e d " , 1 , d b C h e c k e r . r o w C o u n t ( " B O O K S " ) ) ;
b o ok li st. a dd(b o ok2) ;
a s se rt Equ a ls(" T wo e n trie s ex p e c t e d " , 2 , d b C h e c k e r . r o w C o u n t ( " B O O K S " ) ) ;
b o ok li st. a dd(b o ok3) ;
a s se rt Equ a ls(" T hree entr ies e x p e c t e d " , 3 , d b C h e c k e r . r o w C o u n t ( " B O O K S " ) ) ;
b o ok li st. r emov e (boo k 2);
a s se rt Equ a ls(" A gain , two ent r i e s e x p e c t e d " , 2 , d b C h e c k e r . r o w C o u n t ( " B O O K S " ) ) ;
a s se rt Equ a ls(" C heck the titl e f o r t h e f i r s t b o o k " ,
" Dinn e r fo r tw o " ,
d bChe c ker. getS t r i n g C o l u m n ( " B O O K S " ,
"TITLE",
"ISBN=\"0340823429\""));
a s se rt Equ a ls(" C heck the auth o r f o r t h e f i r s t b o o k " ,
" Mike Gayl e",
d bChe c ker. getS t r i n g C o l u m n ( " B O O K S " ,
"AUTHOR",
"ISBN=\"0340823429\""));

a s se rt Equ a ls(" C heck the titl e f o r t h e t h i r d b o o k " ,


" Herr Lehm ann" ,
d bChe c ker. getS t r i n g C o l u m n ( " B O O K S " ,
"TITLE",
"ISBN=\"3821807059\""));
a s se rt Equ a ls(" C heck the auth o r f o r t h e s e c o n g b o o k " ,
" Sven Rege ner" ,
d bChe c ker. getS t r i n g C o l u m n ( " B O O K S " ,
"AUTHOR",
"ISBN=\"3821807059\""));

Here we check whether the number of books in the book list is growing constantly with the addition of
each new book. Similarly, we verify that it is decreased when a book is removed. But the test is not
complete until it checks whether the correct book is removed. We accomplish this by verifying that the
book list still contains the entries for the other two books. We use the method
g et S tr in gCo l umn( t able N ame, col u m n N a m e , w h e r e C l a u s e ) that returns the value of the
specified column of the first row that matches the given where clause in the specified table. Thus, we
verify that for each I SBN the expected author and title are still available inside the book list.
With this test case it is now possible to implement the appropriate domain class B o o k L i s t that contains
the mapping between the Java code and the database content. This small example shows how the
database checker can be used to verify if this mapping works as expected and the database content is
consistent with the application behavior.

Amazon

Prev

don't be afraid of buying books

Next

WRITING XML-BASED TESTS WITH XMLUnit


w it h T im Ba c on
This section was contributed by Tim Bacon of ThoughtWorks UK [URL 58], the author and
maintainer of xmlUnit[URL 18].

What is XMLUnit?
XMLUnit enables JUnit-style assertions to be made about the content and structure of XML. It is an open
source project hosted at sourceforge.net that grew out of a need to test a system that generated and
received custom XML messages. The problem that we faced was how to verify that the system generated
the correct message from a known set of inputs. Obviously, we could use a DTD or a schema to validate
the message output, but this approach wouldn't allow us to distinguish between valid XML with correct
content (e.g., element <foo > bar< /foo > )and valid XML with incorrect content (e.g., element
< fo o >b az </f o o>). What we really wanted was an a s s e r t X M L E q u a l s ( ) method so we could
compare the message that we expected the system to generate and the message that the system
actually generated. And that was the beginning of XMLUnit.

Quick tour
XMLUnit provides a single JUnit extension class, X M L T e s t C a s e , and a set of supporting classes that
allow assertions to be made about:
the differences between two pieces of XML
the validity of a piece of XML
the outcome of transforming a piece of XML using XSLT
the evaluation of an XPath expression on a piece of XML
individual nodes in a piece of XML that are exposed by DOM Traversal
XMLUnit can also treat HTML content (even badly-formed HTML) as valid XML to allow these assertions
to be made about Web pages.

Glossary
As with many projects, some words in XMLUnit have particular meanings, so a quick review would
probably be helpful. A piece of XML is either a DOM Document, a S t r i n g containing marked-up content,
or a R ead er that allows access to marked-up content in some underlying source. XMLUnit compares the
expected control XML to some actual test XML. The comparison can reveal that two pieces of XML are
identical, similar, or different. The unit of measurement used by the comparison is a difference, and
differences can be either recoverable or unrecoverable. Two pieces of XML are identical if there are no
differences between them, similar if there are only recoverable differences between them, and different if
there are any unrecoverable differences between them.

Configuring XMLUnit
There are many Java XML parsers available, and XMLUnit should work with any JAXP-compliant parser
library, such as Xerces from the Apache Jakarta project. To use the XSL and XPath features of XMLUnit
a Trax-compliant transformation engine is required, such as Xalan, from the Apache Jakarta project. To
configure XMLUnit to use your parser and transformation engine set three System properties before any

tests are run:


S ys t em .s etP r oper t y("j a vax. xml. p a r s e r s . D o c u m e n t B u i l d e r F a c t o r y " ,
"or g .apa che. x e r c e s . j a x p . D o c u m e n t B u i l d e r F a c t o r y I m p l " ) ;
S ys t em .s etP r oper t y("j a vax. xml. p a r s e r s . S A X P a r s e r F a c t o r y " ,
"or g .apa che. x e r c e s . j a x p . S A X P a r s e r F a c t o r y I m p l " ) ;
S ys t em .s etP r oper t y("j a vax. xml. t r a n s f o r m . T r a n s f o r m e r F a c t o r y " ,
"or g .apa che. x a l a n . p r o c e s s o r . T r a n s f o r m e r F a c t o r y I m p l " ) ;

Alternatively, there are static methods on the X M L U n i t class that can be called directly. The advantage
of this approach is that you can specify a different parser class for control and test XML and change the
current parser class at any time in your tests, should you need to make assertions about the
compatibility of different parsers.
X ML U ni t. set C ontr o lPar s er(" org. a p a c h e . x e r c e s . j a x p . " +
"Do c u m e n t B u i l d e r F a c t o r y I m p l " ) ;
X ML U ni t. set T estP a rser ( "org .apa c h e . x e r c e s . j a x p . D o c u m e n t B u i l d e r F a c t o r y I m p l " ) ;
X ML U ni t. set S AXPa r serF a ctor y("o r g . a p a c h e . x e r c e s . j a x p . S A X P a r s e r F a c t o r y I m p l " ) ;
X ML U ni t. set T rans f orme r Fact ory( " o r g . a p a c h e . x a l a n . p r o c e s s o r . " +
"TransformerFactoryImpl");

Writing XML comparison tests


Let's say we have two pieces of XML that we wish to compare and assert that they are equal. We could
write a simple test class like this:
p ub l ic c las s MyX M LTes t Case ext e n d s X M L T e s t C a s e {
p u bl ic My X MLTe s tCas e (Str ing n a m e ) {
s up er( n ame) ;
}
p u bl ic vo i d te s tFor E qual ity( ) t h r o w s E x c e p t i o n {
S tr ing myCo n trol X ML = "<m s g > < u u i d > 0 x 0 0 4 3 5 A 8 C < / u u i d > < / m s g > " ;
S tr ing myTe s tXML = "< msg> < l o c a l I d > 2 3 7 6 < / l o c a l I d > < / m s g > " ;

a ss ert X MLEq u al(" C ompa ring t e s t x m l t o c o n t r o l x m l " ,


myC o ntro lXML ,
myT e stXM L);

The a ss ert X MLEq u al test will pass if the control and test XML are either similar or identical.
Obviously, in this case, the pieces of XML are different and the test will fail with the message:
C om p ar in g t e st x m l to cont rol x m l
[ di f fe re nt] Expe c ted e leme nt t a g n a m e ' u u i d ' b u t w a s ' l o c a l I d ' comp a ring <uui d... > t o < l o c a l I d . . . >

When comparing pieces of XML, XMLUnit is creating an instance of the D i f f class behind the scenes.
The D if f class stores the result of an XML comparison and makes it available through the methods
s im i la r( ) and id e ntic a l().The XM L T e s t C a s e class tests the value of s i m i l a r ( ) in the

a ss e rt XM LEq u als( ) method and the value of i d e n t i c a l ( ) in a s s e r t X M L I d e n t i c a l ( ) .However,


it is easy to create a D i ff directly in test cases:
p ub l ic v oid test X MLId e ntic al() t h r o w s E x c e p t i o n {
S t ri ng my C ontr o lXML = "< stru c t > " +
"<in t > 3 < / i n t > " +
"<bo o l e a n > f a l s e < / b o o l e a n > " +
"< /str u c t > " ;
S t ri ng my T estX M L = " <str uct> " +
"<b oole a n > f a l s e < / b o o l e a n > " +
"<i nt>3 < / i n t > " +
" </st ruct > " ;

D i ff m yDi f f = n ew D i ff(m yCon t r o l X M L , m y T e s t X M L ) ;


a s se rt Tru e ("XM L sim i lar " +m y D i f f . t o S t r i n g ( ) , m y D i f f . s i m i l a r ( ) ) ;
a s se rt Tru e ("XM L ide n tica l " + m y D i f f . t o S t r i n g ( ) , m y D i f f . i d e n t i c a l ( ) ) ;

This test fails as two pieces of XML are considered similar but not identical if their nodes occur in a
different sequence. The failure message reported by JUnit from the call to m y D i f f . t o S t r i n g ( ) looks
like this:
[ no t i de nti c al] E xpec t ed s eque n c e o f c h i l d n o d e s ' 0 ' b u t w a s ' 1 ' c ompa r ing <int . . . > t o < i n t . . . >

For efficiency reasons, a Diff stops the comparison process as soon as the first difference is found. To
get all the differences between two pieces of XML an instance of the D e t a i l e d D i f f class, a subclass of
D if f , is required. Consider this test:
p ub l ic v oid test A llDi f fere nces ( ) t h r o w s E x c e p t i o n {
S t ri ng my C ontr o lXML = "< news > " +
"<item id=\"1\">War</item>" +
"<item id=\"2\">Plague</item>" +
"<item id=\"3\">Famine</item>" +
" </ne w s > " ;
S t ri ng my T estX M L = " <new s>" +
"<it e m i d = \ " 1 \ " > P e a c e < / i t e m > " +
"<it e m i d = \ " 2 \ " > H e a l t h < / i t e m > " +
"<it e m i d = \ " 3 \ " > P l e n t y < / i t e m > " +
"</n ews> " ;

D e ta il edD i ff m y Diff = ne w De t a i l e d D i f f ( n e w D i f f ( m y C o n t r o l X M L , m y T e s t X M L ) ) ;
L i st a llD i ffer e nces = my Diff . g e t A l l D i f f e r e n c e s ( ) ;
a s se rt Equ a ls(m y Diff . toSt ring ( ) , 2 , a l l D i f f e r e n c e s . s i z e ( ) ) ;

This test fails with the following message as each of the three news items differs between the control
and test XML:
[ di f fe re nt] Expe c ted t ext valu e
comp a ring <ite m... >
[ di f fe re nt] Expe c ted t ext valu e
comp a ring <ite m... >
[ di f fe re nt] Expe c ted t ext valu e
comp a ring <ite m... >
e xp e ct ed <2 > but was < 3>

'War' but was 'Peace' to <item...>


'Plague' but was 'Health' to <item...>
'Famine' but was 'Plenty' to <item...>

The L is t returned from the ge tAll Di f f e r e n c e s ( ) method contains instances of the D i f - f e r e n c e


class. Dif fe renc e objects are instantiated at runtime by XMLUnit using f i n a l s t a t i c fields in the
D if f er en ceC o nsta n ts class, and these instances are passed in notification events to a registered
D if f er en ceL i sten e r by the Di ffer e n c e E n g i n e class as it compares two pieces of XML. The D i f f
class implements the D i ffer e nceL ist e n e r interface but it is possible to override the default behavior
by implementing the interface in your own class.

The I gn ore T extA n dAt tr ibut eValu e s D i f f e r e n c e L i s t e n e r class is an example of how to


implement a custom Di f feren ceLi st e n e r . It allows an XML comparison to be made that ignores
differences in the values of text and attribute nodes, for example, when comparing a skeleton or outline
piece of XML to some generated XML. The following test illustrates its use:
p ub l ic v oid test C ompa r eToS kele t o n X M L ( ) t h r o w s E x c e p t i o n {
S t ri ng my C ontr o lXML = "< loca t i o n > " +
"<s t r e e t - a d d r > 2 2 a n y s t r e e t < / s t r e e t - a d d r > " +
"<p o s t c o d e > X Y 0 0 9 9 Z < / p o s t c o d e > " +
"< /loc a t i o n > " ;

S t ri ng my T estX M L = " <loc atio n > " +


"< stre e t - a d d r > 2 0 e a s t c h e a p < / s t r e e t - a d d r > " +
"< post c o d e > E C 3 M 1 E B < / p o s t c o d e > " +
" </lo cati o n > " ;

D i ff er enc e List e ner m yDif fere n c e L i s t e n e r =


new I gnor e Text AndA t t r i b u t e V a l u e s D i f f e r e n c e L i s t e n e r ( ) ;
D i ff m yDi f f = n ew D i ff(m yCon t r o l X M L , m y T e s t X M L ) ;
m y Di ff .ov e rrid e Diff e renc eLis t e n e r ( m y D i f f e r e n c e L i s t e n e r ) ;
a s se rt Tru e ("te s t XM L mat ches c o n t r o l s k e l e t o n X M L " , m y D i f f . s i m i l a r ( ) ) ;

Comparing XML Transformations


XMLUnit can test XSL transformations at a high level using the T r a n s f o r m class that wraps a
j av a x. xm l.t r ansf o rm.T r ansf orme r instance. Knowing the input XML, input style-sheet, and
expected output XML, we can assert that the output of the transformation matches the expected output
as follows:
p ub l ic v oid test X SLTr a nsfo rmat i o n ( ) t h r o w s E x c e p t i o n {
S t ri ng my I nput X ML = "... ";
F i le m ySt y lesh e etFi l e = new F i l e ( " . . . " ) ;

T r an sf orm myTr a nsfo r m = new T r a n s f o r m ( m y I n p u t X M L , m y S t y l e s h e e t F i l e ) ;


S t ri ng my E xpec t edOu t putX ML = " . . . " ;
D i ff m yDi f f = n ew D i ff(m yExp e c t e d O u t p u t X M L , m y T r a n s f o r m ) ;
a s se rt Tru e ("XS L tra n sfor mati o n w o r k e d a s e x p e c t e d " , m y D i f f . s i m i l a r ( ) ) ;

The g et Res u ltSt r ing () and get Res u l t D o c u m e n t ( ) methods of the T r a n s f o r m class can be used
to access the result of the XSL transformation programmatically if required, for example, as seen here:
p ub l ic v oid test A noth e rXSL Tran s f o r m a t i o n ( ) t h r o w s E x c e p t i o n {
F i le m yIn p utXM L File = ne w Fi l e ( " . . . " ) ;
F i le m ySt y lesh e etFi l e = new F i l e ( " . . . " ) ;
T r an sf orm myTr a nsfo r m = new T r a n s f o r m ( n e w S t r e a m S o u r c e ( m y I n p u t X M L F i l e ) ,
new StreamSource(myStylesheetFile));
D o cu me nt m yExp e cted O utpu tXML =
X M LU ni t.b u ildD o cume n t(XM LUni t . g e t C o n t r o l P a r s e r ( ) ,
n e w Fi leRe a d e r ( " . . . " ) ) ;

D i ff m yDi f f = n ew D i ff(m yExp e c t e d O u t p u t X M L ,


my Tran s f o r m . g e t R e s u l t D o c u m e n t ( ) ) ;
a s se rt Tru e ("XS L tra n sfor mati o n w o r k e d a s e x p e c t e d " , m y D i f f . s i m i l a r ( ) ) ;

Validation Tests
XML parsers that validate a piece of XML against a DTD are common; however, they rely on a DTD
reference being present in the XML, and they can only validate against a single DTD. When writing a
system that exchanges XML messages with third parties there are times when you would like to validate
the XML against a DTD that is not available to the recipient of the message and so cannot be referenced
in the message itself. XMLUnit provides a V a l i d a t o r class for this purpose.
p ub l ic v oid test V alid a tion () t h r o w s E x c e p t i o n {
X M LU ni t.g e tTes t Docu m entB uild e r F a c t o r y ( ) . s e t V a l i d a t i n g ( t r u e ) ;
/ / A s the docu m ent i s pa rsed i t i s v a l i d a t e d a g a i n s t i t s r e f e r e n c e d D T D
D o cu me nt m yTes t Docu m ent = XM L U n i t . b u i l d T e s t D o c u m e n t ( " . . . " ) ;

S t ri ng my S yste m Id = ". . .";


S t ri ng my D TDUr l = n e w Fi le(" . . . " ) . t o U R L ( ) . t o E x t e r n a l F o r m ( ) ;
V a li da tor myVa l idat o r = new V a l i d a t o r ( m y T e s t D o c u m e n t , m y S y s t e m I d , m y D T D U r l ) ;
a s se rt Tru e ("te s t do c umen t va l i d a t e s a g a i n s t u n r e f e r e n c e d D T D " ,
my V alid a tor. isVa l i d ( ) ) ;

Xpath Tests
One of the strengths of XML is the ability to programmatically extract specific parts of a document using
XPath expressions. The X MLTes tCas e class offers a number of XPath related assertion methods, as
demonstrated in this test:
p ub l ic v oid test X Path s () t hrow s E x c e p t i o n {
S t ri ng my S olar S yste m XML = "< s o l a r - s y s t e m > " +
"<planet name='Earth'" +
"position='3'" +
"supportsLife='yes'/>" +
"<planet name='Venus' position='4'/>" +
"</s o l a r - s y s t e m > " ;
a s se rt Xpa t hExi s ts(" / /pla net[ @ n a m e = ' E a r t h ' ] " , m y S o l a r S y s t e m X M L ) ;
a s se rt Not X path E xist s ("// star [ @ n a m e = ' a l p h a c e n t a u r i ' ] " , m y S o l a r S y s t e m X M L ) ;
a s se rt Xpa t hsEq u al(" / /pla net[ @ n a m e = ' E a r t h ' ] " ,
"//p lane t [ @ p o s i t i o n = ' 3 ' ] " ,
mySo larS y s t e m X M L ) ;

a s se rt Xpa t hsNo t Equa l ("// plan e t [ @ n a m e = ' V e n u s ' ] " ,


" //pl a n e t [ @ s u p p o r t s L i f e = ' y e s ' ] " ,
m ySol a r S y s t e m X M L ) ;

When an XPath expression is evaluated against a piece of XML a N o d e L i s t is created that contains the
matching No d es. The methods in the previous testa s s e r t X P a t h E x i s t s ( ) ,
a ss e rt No tXP a thEx i sts( ) , asse rtX P a t h s E q u a l ( ) ,and a s s e r t X P a t h s N o t E q u a l ( ) use these

a ss e rt No tXP a thEx i sts( ) , asse rtX P a t h s E q u a l ( ) ,and a s s e r t X P a t h s N o t E q u a l ( ) use these


N od e Li st s. However, the contents of a N o d e L i s t can be flattened (or String-ified) to a single value,
and XMLUnit also allows assertions to be made about this single value, as in this test:
p ub l ic v oid test X Path V alue s() t h r o w s E x c e p t i o n {
S t ri ng my J avaF l avou r s = "<ja v a - f l a v o u r s > " +
"<jvm current='some platforms'>1.1.x</jvm>" +
"<jvm current='no'>1.2.x</jvm>" +
"<jvm current='yes'>1.3.x</jvm>" +
"<jvm current='yes' latest='yes'>1.4.x</jvm>" +
"</j a v a - f l a v o u r s > " ;
a s se rt Xpa t hEva l uate s To(" 2", " c o u n t ( / / j v m [ @ c u r r e n t = ' y e s ' ] ) " , m y J a v a F l a v o u r s ) ;
a s se rt Xpa t hVal u esEq u al(" //jv m [ 4 ] / @ l a t e s t " ,
"//j v m [ 4 ] / @ c u r r e n t " ,
myJa v a F l a v o u r s ) ;

a s se rt Xpa t hVal u esNo t Equa l("/ / j v m [ 2 ] / @ c u r r e n t " ,


"/ / j v m [ 3 ] / @ c u r r e n t " ,
my J a v a F l a v o u r s ) ;

Xpaths are especially useful where a document is made up largely of known, unchanging content with
only a small amount of changing content created by the system. One of the main areas where constant
boilerplate markup is combined with system-generated markup is of course in Web applications. The
power of XPath expressions can make testing Web page output quite trivial, and XMLUnit supplies a
means of converting even very badly formed HTML into XML to aid this approach to testing.
The H TM LDo c umen t Bui ld er class uses the Swing HTML parser to convert marked-up content to Sax
events. The Tol er ant S axDo c umen tBu i l d e r class handles the Sax events to build up a DOM
document in a tolerant fashion, that is, without mandating that opened elements are closed. (In a purely
XML world this class would have no purpose as there are plenty of Sax event handlers that can build
DOM documents from well-formed content.)The following test illustrates the use of these classes:
p ub l ic v oid test X path s InHT ML() t h r o w s E x c e p t i o n {
S t ri ng so m eBad l yFor m edHT ML = " < h t m l > " +
"<title>Ugh</title>" +
"<body>" +
"<h1>Heading" +
"<ul>" +
"<li id='1'>Item One" +
"<li id='2'>Item Two";
T o le ra ntS a xDoc u ment B uild er t o l e r a n t S a x D o c u m e n t B u i l d e r =
ne w Tol e rant S axDo cume n t B u i l d e r ( X M L U n i t . g e t T e s t P a r s e r ( ) ) ;
H T ML Do cum e ntBu i lder html Docu m e n t B u i l d e r =
n ew H TMLD o cume n tBui lder ( t o l e r a n t S a x D o c u m e n t B u i l d e r ) ;
D o cu me nt w ellF o rmed D ocum ent =
html D ocum e ntBu ilde r . p a r s e ( s o m e B a d l y F o r m e d H T M L ) ;

a s se rt Xpa t hEva l uate s To(" Item O n e " ,


" /htm l / b o d y / / l i [ @ i d = ' 1 ' ] " ,
w ellF o r m e d D o c u m e n t ) ;

One of the key points about using Xpaths with HTML content is that extracting values in tests requires
the values to be identifiable. (This is just another way of saying that testing HTML is easier when it is

written to be testable.) In the previous example id attributes were used to identify the list item values
that needed to be testable; however, class attributes or span and div tags can also be used to identify
specific content for testing.

Testing by Tree Walking


The DOM specification allows a Do cumen t to optionally implement the D o c u m e n t T r a v e r s a l interface.
This interface allows an application to iterate over the N o d e s contained in a D o c u m e n t , or to walk the
DOM tree. The XMLUnit Nod e Test class and N o d e T e s t e r interface make use of D o c u m e n t T r a v e r s a l
to expose individual N odes in tests: the former handles the mechanics of iteration, and the latter allows
custom test strategies to be implemented. A sample test strategy is supplied by the
C ou n ti ng Nod e Test e r class that counts the nodes presented to it and compares the actual count to
an expected count. The test here illustrates its use:
p ub l ic v oid test C ount i ngNo deTe s t e r ( )
S t ri ng te s tXML = "< f ibon acci > " +
"<va l>1< / v a l > "
"<va l>2< / v a l > "
"<va l>3< / v a l > "
"<va l>5< / v a l > "
"<va l>9< / v a l > "
"< / fibo nacc i > " ;

throws Exception {
+
+
+
+
+

C o un ti ngN o deTe s ter c ount ingN o d e T e s t e r = n e w C o u n t i n g N o d e T e s t e r ( 4 ) ;


a s se rt Nod e Test P asse s (tes tXML , c o u n t i n g N o d e T e s t e r , N o d e . T E X T N O D E ) ;

This test fails as there are five text nodes, and JUnit supplies the following message:
E xp e ct ed no d e te s t to pass , bu t i t f a i l e d !
C ou n te d 5 n o de(s ) but expe cted 4

Note that if your DOM implementation does not support the D o c u m e n t T r a v e r s a l interface, then
XMLUnit will throw an I l legal Argu me n t E x c e p t i o n informing you that you cannot use the N o d e T e s t
or N o d eT es ter classes. Unfortunately, even if your DOM implementation does support
D oc u me nt Tra v ersa l , attributes are not exposed by iteration; however, they can be examined from
the E le me nt node that contains them.
While the previous test could have been performed easily using XPath, there are times when Node
iteration is more powerful. In general, this is true when there are programmatic relationships between
nodes that can be more easily tested iteratively. The following test uses a custom N o d e T e s t e r class to
illustrate the potential:
p ub l ic v oid test C usto m Node Test e r ( ) t h r o w s E x c e p t i o n {
S t ri ng te s tXML = "< f ibon acci > " +
"<va l>1< / v a l > " +
"<va l>2< / v a l > " +
"<va l>3< / v a l > " +
"<va l>5< / v a l > " +
"<va l>9< / v a l > " +
"< / fibo nacc i > " ;

N o de Te st n odeT e st = new Node T e s t ( t e s t X M L ) ;


a s se rt Nod e Test P asse s (nod eTes t , n e w F i b o n a c c i N o d e T e s t e r ( ) ,
ne w sho r t[ ] {Nod e.TE X T N O D E , N o d e . E L E M E N T N O D E } , t r u e ) ;

p ri v at e cla s s Fi b onac c iNod eTes t e r e x t e n d s A b s t r a c t N o d e T e s t e r {


p r iv at e i n t ne x tVal = 1, las t V a l = 1 , p r i o r V a l = 0 ;
p u bl ic vo i d te s tTex t (Tex t te x t ) t h r o w s N o d e T e s t E x c e p t i o n {
in tv al = Inte g er.p a rseI nt(t e x t . g e t D a t a ( ) ) ;
if ( nex t Val ! = va l ) {
t hro w new Node T estE xcep t i o n ( " I n c o r r e c t s e q u e n c e v a l u e " , t e x t ) ;
}

ne xt Val = va l + l a stVa l;
pr io rVa l = l a stVa l ;
la st Val = va l ;

p u bl ic vo i d te s tEle m ent( Elem e n t e l e m e n t ) t h r o w s N o d e T e s t E x c e p t i o n {


St ri ng n ame = ele m ent. getL o c a l N a m e ( ) ;

if ( "fi b onac c i".e q uals (nam e ) | | " v a l " . e q u a l s ( n a m e ) ) {


r etu r n;
}
th ro w n e w No d eTes t Exce ptio n ( " U n e x p e c t e d e l e m e n t " , e l e m e n t ) ;

p u bl ic vo i d no M oreN o des( Node T e s t n o d e T e s t ) t h r o w s N o d e T e s t E x c e p t i o n {


}

As expected, the test fails because the XML contains the wrong value for the last number in the
sequence.
E xp e ct ed no d e te s t to pass , bu t i t f a i l e d !
I nc o rr ec t s e quen c e va l ue [ #tex t : 9 ]

Amazon

Prev

don't be afraid of buying books

Next

GARGOYLE SOFTWARE JUNIT EXTENSIONS


w it h M ik e Bow le r
This section was contributed by Mike Bowler of Gargoyle Software Inc. [URL 59], the author
and maintainer of the Gargoyle JUnit Extensions[URL 19].
As a consultant, I find myself being asked to write the same code over and over again. Many of my clients
share the same needs and require the same base utility classes. Because each client owns the code that I
wrote when working for them, I am unable to reuse that code for the next client and consequently end up
rewriting the same classes repeatedly.
In 1998, I got sufficiently frustrated with this situation so I started writing these classes on my own time
and releasing them as open source. This way I would only have to write each class once and I could reuse
them for each one of my clients. This collection of classes has become the GSBase project.
The testing classes within GSBase originally started out as support for the testing of GSBase itself but have
shown themselves to be generally useful for testing other projects.

RecursiveTestSuite
The standard mechanism for running tests in JUnit is to build nested hierarchies of test suites. In addition
to being very monotonous work, this way is extremely error prone as it is very easy to accidentally miss a
test in a suite. I wanted JUnit to figure out where all my tests were and to just execute them. Because
JUnit itself doesn't do this, I created R ec u r s i v e T e s t S u i t e , a special T e s t S u i t e that will recursively
walk through the directory structure looking for subclasses of T e s t C a s e .
If you are using Ant for your builds then the Ant junit target provides
the same kind of functionality as R e c u r s i v e T e s t S u i t e . If that target
had been available when I first started using JUnit then writing this class
would have been unnecessary.

The most common usage of Recu rsive T e s t S u i t e is to create a subclass like this:
p ub l i c c la ss Ma inT e stSu i te e xte n d s R e c u r s i v e T e s t S u i t e {
p u b l ic M ain Te stS u ite( f inal St r i n g n a m e ) t h r o w s I O E x c e p t i o n {
s u pe r( new F ile ( ".") , new Ac c e p t A l l T e s t F i l t e r ( ) ) ;
}
}

The reason for creating a subclass is that you need a class that can be passed into the test runners that
will initialize everything the way you want. When you have this class, you can invoke the gui test runner
with this command:
j av a j un it .sw in gui . Test R unne r m y p a c k a g e . M a i n T e s t S u i t e

...and the text test runner with this command...


j av a j un it .te xt ui. T estR u nner my p a c k a g e . M a i n T e s t S u i t e

The RecursiveTestSuite has the following constructor


p ub l i c R ec urs iv eTe s tSui t e(fi nal F i l e s t a r t i n g D i r e c t o r y ,
fi nal T e s t F i l t e r t e s t F i l t e r ) ;

The first parameter that is passed into R e c u r s i v e T e s t S u i t e is the directory to start at. This is the root
of your classpath.
The second parameter is a test filter. The main advantage to recursively walking the directories looking for
tests is that you won't miss any by mistake. The main disadvantage is that you may include tests that you
don't want to run. The test filter is intended to solve that problem. It allows you to exclude certain tests
from the full run. The Acce p tAll TestF i l t e r specified in the example will accept all of the tests. If you
want to selectively exclude tests then you need to implement the T e s t F i l t e r interface yourself.
For example, if you have your infrastructure code separate from your business logic and you wanted to be
able to run the tests for one or the other, only then you might write a test filter like this:
p ub l i c c la ss In fra s truc t ureT est F i l t e r i m p l e m e n t s T e s t F i l t e r {
p u b l ic b ool ea n a c cept ( fina l C l a s s c l a z z ) {
S t ri ng na me = c lazz . getN ame ( ) ;
r e tu rn na me .st a rtsW i th(" com . m y c o m p a n y . m y p r o d u c t . i n f r a s t r u c t u r e " ) ;
}
}
p ub l i c c la ss Bu sin e ssCo d eTes tFi l t e r i m p l e m e n t s T e s t F i l t e r {
p u b l ic b ool ea n a c cept ( fina l C l a s s c l a z z ) {
S t ri ng na me = c lazz . getN ame ( ) ;
r e tu rn na me .st a rtsW i th(" com . m y c o m p a n y . m y p r o d u c t . b u s i n e s s " ) ;
}
}

In each case, the filter only accepts tests located within certain package structures. It checks the class
name of the test class to see if it starts with a given prefix and accepts or rejects the class based on its
package.

OrderedTestSuite
Assuming that your tests are in a class that subclasses T e s t C a s e , there are two ways to identify which
tests should be run. The easiest way is to use the TestSuite constructor that takes a class. This will
automatically create a new test for every method that follows a specific naming convention
p ub l i c v oi d t es tNe g ativ e Valu e() {
}

The other way is to use the default constructor for T e s t S u i t e and then manually add each test that you
want run.
p ub l i c s ta tic T est suit e () {
f i n a l Te stS ui te s uite = ne w T e s t S u i t e ( ) ;
s u i t e. ad dTe st (ne w Mon e yTes t(" t e s t N e g a t i v e V a l u e " ) ) ;
s u i t e. ad dTe st (ne w Mon e yTes t(" t e s t N u l l C u r r e n c y " ) ) ;
r e t u rn s uit e;
}

The problem with the first approach is that there is no way to specify the order in which the tests will be
run. The second approach solves that problem but introduces the potential for accidentally missing tests.
What I wanted was something that automatically found the tests and yet allowed me to specify the order
in which the tests would be run. O rdere d T e s t S u i t e was created to solve this problem.
Whenever possible it is advisable to avoid order dependencies between
your tests. If tests must be run in a specific order, then one failing test
can often cause other subsequent tests to also fail. Ideally, every test is
completely independent of all other tests so that one failing test will not
cause any other tests to fail.

p ub l i c s ta tic T est suit e () {


r e t u rn n ew Or der e dTes t Suit e(M o n e y T e s t . c l a s s ,
new S t r i n g [ ] {
"testNegativeValue",
"testNullCurrency"
});
}

O rd e r e dT es tSu it e will guarantee that t e s t N e g a t i v e V a l u e and t e s t N u l l C u r r e n c y will be


executed in order. It will then use reflection to find other methods starting with "test" and will execute
them. This means that if you write te st T h r e e but forget to add it to s u i t e ( ) then it will still get
executed, albeit not in a defined order.

EventCatcher
When testing objects that fire events, it becomes neccessary to collect events to ensure that the right
events were fired at the right time.
f in a l Li st co ll ect e dEve n ts = ne w A r r a y L i s t ( ) ;
f in a l JF ra me fr ame = ne w JFr ame ( " t e s t " ) ;
f ra m e . ad dW ind ow Lis t ener ( new Win d o w A d a p t e r ( ) {
p u b l ic v oid w ind o wClo s ing( fin a l W i n d o w E v e n t e v e n t ) {
co ll ect ed Eve n ts.a d d(ev ent ) ;
}
} );

The typical approach requires creating many inner classes for the various types of events that you want to
listen for. This quickly becomes very tedious and introduces the potential for error as very similar code is
written over and over again.
E ve n t C at ch er is the solution to this problem. This is a single object that can listen for any kind of event
that might be fired.
f in a l JF ra me fr ame = ne w JFr ame ( " t e s t " ) ;
f in a l Ev en tCa tc her even t Catc her = n e w E v e n t C a t c h e r ( ) ;
e ve n t C at ch er. li ste n To(f r ame) ;

In this example, the event catcher will register itself as a listener for every type of event that is thrown
by frame. Note that this will work for any event so long as the object has a method for registering
listeners that follows the form addX XXLi s t e n e r ( ) .
An E v e ntC at cher R eco r d will be created for each event that is caught. This will contain the event itself,

the method that was invoked on the listener, and the name of the thread on which this method was
called. These records can be retrieved with E v e n t C a t c h e r . g e t E v e n t R e c o r d A t ( i n t i n d e x ) .
The preceding example caught every event fired by the J F r a m e . Although convenient, sometimes that is
too much informationsometimes you only want to catch one specific kind of event. E v e n t C a t c h e r
provides support for this by generating listener objects which you can then manually add to the object you
wish to listen to.
f in a l JF ra me fr ame = ne w JFr ame ( " t e s t " ) ;
f in a l Ev en tCa tc her even t Catc her = n e w E v e n t C a t c h e r ( ) ;
f in a l Wi nd owL is ten e r li s tene r =
( Wi ndo wL ist e ner) e vent Cat c h e r . g e t L i s t e n e r ( W i n d o w L i s t e n e r . c l a s s ) ;
f ra m e . ad dW ind ow Lis t ener ( list ene r ) ;

The g et Li ste n er() method takes a class object that is a listener interface such as A c t i o n L i s t e n e r
or W i n do wLi s tene r . It returns an object that implements that interface and that will log all events that
are passed into it.
A common testing idiom is to perform an a s s e r t E q u a l s ( ) on a collection of expected values and
another collection of collected values. The problem in this case is that this idiom assumes that all objects
in both collections implement the e quals ( ) method. None of the core events in Java implement
e qu a l s () so this idiom doesn't work. Instead, we can take a best guess of whether they are equal
f in a l Li st ex pe cte d Even t s = new A r r a y L i s t ( ) ;
f in a l Li st co ll ect e dEve n ts = ne w A r r a y L i s t ( ) ;
. ..
e ve n t C at ch er. as ser t Even t sApp ear E q u a l s ( e x p e c t e d E v e n t s , c o l l e c t e d E v e n t s ) ;

This will call every accessor on each object and compare the results. If the values of all the properties are
the same, then we assume that the objects themselves are in fact equal. This isn't perfectit is possible
to get false positivesbut it's accurate enough in most cases.

EqualsTester
E qu a l s Te st er is used to test the equals contract on objects. The contract as specified by
j av a . l an g. Obj ec t states that if A.eq u a l s ( B ) is true then B . e q u a l s ( A ) is also true. It also specifies
that if A. equ a ls(B ) is true then A .has h C o d e ( ) will equal B . h a s h C o d e ( ) .
It is not a trivial exercise to fully test the equals contract, so many people don't bother. Instead, they
assume that e q uals ( ) and has hCod e( ) are simple enough that they couldn't break and therefore don't
need to be tested. Ironically enough, it is very common for this contract to be implemented incorrectly.
Peter Haggar goes into depth on the common pitfalls when implementing the equals contract in his book
Practical Java[23].
In my experience, the most common problems when implementing equals are as follows:
1. The null case isn't handled.
According to the contract, the eq u a l s ( ) method must return false but many implementations
don't explicitly handle null and consequently throw a N u l l P o i n t e r E x c e p t i o n .
2. eq ual s (Foo ) is written instead of e q u a l s ( O b j e c t )
The problem with this is that the wrong method has been overridden and it will not be called in all
situations.
pu bli c boo l ean e qual s(fin a l M o v i e m o v i e ) {
...
}

Given this sample code:


fi nal Movi e mov i e1 = new M o v i e ( " O n e " ) ;
fi nal Movi e mov i e2 = new M o v i e ( " T w o " ) ;

fi nal Obje c t mo v ieAs Objec t = m o v i e 2 ;

mo vie 1 .equ a ls(m o vie2 ) will call the method declared above and will correctly compare the two
movies.
mo vie 1 .equ a ls(m o vieA sObje c t ) will call the superclasses e q u a l s ( O b j e c t ) method which
will not return the expected result.
3. An ins t ance o f check was used for comparison rather than g e t C l a s s ( )
// Wr o ng c o mpar i son
pu bli c boo l ean e qual s(fin a l O b j e c t o b j e c t ) {
if ( obje c t == null ) ret u r n f a l s e ;
if ( !(ob j ect i nsta nceof M o v i e ) ) r e t u r n f a l s e ;
fin a l Mo v ie o t herM ovie = ( M o v i e ) o b j e c t ;
ret u rn g e tNam e ().e quals ( o t h e r M o v i e . g e t N a m e ( ) ) ;
}

This will return false positives in some situations when comparing against subclasses. If the class in
question is final then using inst a n c e o f is safe to use, but if it can be overridden then
ge tCl a ss() should be used instead.
The reason that false positives are possible is that i n s t a n c e o f checks to ensure that the object
is an instance of this class or a subclass of this class. In the case of a subclass, then this object is
not able to properly perform a comparison as it only knows about the fields that it has and is
unable to perform a comparison against fields present in the subclass but not present in the
superclass. As a result it is possible for a . e q u a l s ( b ) to not equal b . e q u a l s ( a ) .
For example, if we have a Mo vie and its subclass R a t e d M o v i e :
pu bli c cla s s Mo v ie {
pri v ate f inal Stri ng na m e ;
pub l ic M o vie( f inal Stri n g n e w N a m e ) {
n a me = newN a me;
}

pub l ic b o olea n equ als(f i n a l O b j e c t o b j e c t ) {


i f (ob j ect = = nu ll) r e t u r n f a l s e ;
i f (!( o bjec t ins tance o f M o v i e ) ) r e t u r n f a l s e ;
f i nal M ovie othe rMovi e = ( M o v i e ) o b j e c t ;
r e turn getN a me() .equa l s ( o t h e r M o v i e . g e t N a m e ( ) ) ;
}

pu bli c cla s s Ra t edMo vie e x t e n d s M o v i e {


pri v ate f inal Stri ng ra t i n g ;
pub l ic R a tedM o vie( final S t r i n g n e w N a m e , f i n a l S t r i n g n e w R a t i n g ) {
s u per( n ewNa m e);
n a me = newN a me;
r a ting = ne w Rati ng;
}
pu bli c boo l ean e qual s(fin a l O b j e c t o b j e c t ) {
if ( obje c t == null ) ret u r n f a l s e ;
if ( !(ob j ect i nsta nceof R a t e d M o v i e ) ) r e t u r n f a l s e ;
fin a l Ra t edMo v ie o therM o v i e = ( R a t e d M o v i e ) o b j e c t ;
ret u rn g e tRat i ng() .equa l s ( o t h e r M o v i e . g e t R a t i n g ( ) & &
s uper . equa ls(ob j e c t ) ) ;
}
}

then given the following instances, we can perform comparisons.


fi nal Movi e a = new Movie ( " T h e O n e " ) ;
fi nal Rate d Movi e b = new M o v i e ( " T h e O n e " , " G r e a t " ) ;

This case, a. e qual s (b), will return t r u e because b is an instance of M o v i e and the names are

the same. The reverse is not true; however, b . e q u a l s ( a ) will return f a l s e as a is not an
instance of Ra t edMo v ie. This has violated the portion of the equals contract that says
a. equ a ls(b ) must be the same as b . e q u a l s ( a ) . Instead of using i n s t a n c e o f , the proper
comparison should have been written as follows, using the g e t C l a s s ( ) method.
if (ob j ect ! = nu l l && obje c t . g e t C l a s s ( ) = = g e t C l a s s ( ) )

Had both eq ual s () methods been written this way then both of them would have returned f a l s e
which would have satisfied the contract.
4. eq ual s (Obj e ct) was overridden but h a s h C o d e ( ) was not
The contract states that if two objects return true for e q u a l s ( ) , then they must return the same
hash code. By default, the value returned by h a s h C o d e ( ) is fairly random so if you override
eq ual s () to be safe, then you must also override h a s h C o d e ( ) .
While it is possible for two objects to return different values from e q u a l s ( ) at different times (as
property values on the object change, for example), h a s h C o d e values must remain the same for
the entire lifetime of the object. If the value of h a s h C o d e was to change after the object had
been placed into a hash table then unpredictable behavior would result.
Note that it is perfectly acceptable to return a constant value (e.g., r e t u r n 2 ; ) from the
ha shC o de() method. Be aware that if you do return a constant and this object is used as a key
in a hash table then performance will be extremely bad. Many objects are not used as keys and
therefore performance is not an issue.
pu bli c lon g has h Code () {
ret u rn g e tNam e ().h ashCo d e ( ) ;
}

A correct implementation of eq u als( ) resembles the following:


p ub l i c b oo lea n equ a ls(f i nal Obj e c t o b j e c t ) {
i f ( ob je ct == nu l l) r e turn fa l s e ;
i f ( ob je ct. ge tCl a ss() != t his . g e t C l a s s ( ) ) r e t u r n f a l s e ;
f i n a l Mo vie o the r Movi e = ( Mov i e ) o b j e c t ;
r e t u rn g etN am e() . equa l s(ot her M o v i e . g e t N a m e ( ) ) ;
}

While it is perfectly legal to directly access instance variables within the


e q ual s () method, I prefer to use the accessors to ensure that the
accessors themselves have been adequately tested.

Many developers will also put in a check for t h i s as a quick return. There is nothing wrong with this but
it should be noted that this check is purely done for performance reasonsadding this check does not
make the method any more "correct."
p ub l i c b oo lea n equ a ls(f i nal Obj e c t o b j e c t ) {
i f ( ob je ct == th i s) r e turn tr u e ;
i f ( ob je ct == nu l l) r e turn fa l s e ;
i f ( ob je ct. ge tCl a ss() != t his . g e t C l a s s ( ) ) r e t u r n f a l s e ;
f i n a l Mo vie o the r Movi e = ( Mov i e ) o b j e c t ;
r e t u rn n ame .e qua l s(ot h erMo vie . n a m e ) ;
}

The E qu al sTe s ter is designed to test the contract and ensure that none of the common problems are
present. A test using Eq u alsT e ster would look like this:
p ub l i c v oi d t es tEq u als( ) {
f i n a l Mo vie a = n ew M o vie( "A" ) ;
f i n a l Mo vie b = n ew M o vie( "A" ) ;
f i n a l Mo vie c = n ew M o vie( "B" ) ;
f i n a l Mo vie d = n ew M o vie( "A" ) { } ;
n e w Eq ua lsT es ter ( a, b , c, d);
}

The four values that you pass into the Eq u a l s T e s t e r are as follows:
1. The original object that will be the reference for comparision.
2. Another object of the same class that has the same values as the first. We expect this object to
return t r ue when compared to the first.
3. Another object of the same class that has different values than the first. We expect this object to
return f a lse when compared to the first.
4. An instance of a subclass of the first class that has the same values as the first object. If the first
object is final, then this must be null as it is not possible to subclass a final class.
If any part of the equals contract is broken then an A s s e r t i o n F a i l e d E r r o r will be thrownthis is the
same as calling the fai l () method in JUnit.

Detailed exceptions
When testing parameter checking, we often write code like this:
p ub l i c v oi d t es tNu l lInM o neyC ons t r u c t o r ( ) {
f i n a l Cu rre nc y c u rren c y = nul l
try {
n e w Mo ney (5 5, c urre n cy);
f a il (" Exp ec ted Null P oint erE x c e p t i o n " ) ;
}
c a t c h( fi nal N ull P oint e rExc ept i o n e ) {
/ / E xp ect ed pa t h
}
}

The problem with this code is that while we are checking to ensure that a NullPointerException was
thrown, we aren't checking to ensure that the exception was thrown from the place we expected.
p ub l i c M on ey( fi nal int n ewVa lue , f i n a l C u r r e n c y n e w C u r r e n c y ) {
g e t L og () .tr ac e(" E nter i ng M one y c o n s t r u c t o r " ) ;
v a l u e = new Va lue ;
c u r r en cy = ne wCu r renc y ;

i f ( n ew Cu rre nc y = = nul l ) {

t h ro w new N ull P oint e rExc ept i o n ( " n e w C u r r e n c y " ) ;

In this example, if ge t Log( ) returns nu l l then a N u l l P o i n t e r E x c e p t i o n will be thrown out of this


constructor. The test assumes that parameter validation was successful because the exception was thrown,
although the cause of the exception had nothing to do with the value that we thought we were validating.
The next best solution would be to put the name of the null parameter in the exception and check that
from the test code.
p ub l i c v oi d t es tNu l lInM o neyC ons t r u c t o r ( ) {
try {
n e w Mo ney (5 5, n ull) ;
f a il (" Exp ec ted Null P oint erE x c e p t i o n " ) ;
} c a tc h( fin al Nu l lPoi n terE xce p t i o n e ) {
a s se rt Equ al s(" n ewCu r renc y", e . g e t M e s s a g e ( ) ) ;
}
}

The problem with this is that expecting to get structured data out of a message field is always dangerous.
Inevitably someone will change the message while they are debugging something else.
For this reason, I created Detai ledN ul l P o i n t e r E x c e p t i o n , which is a subclass of
N ul l P o in te rEx ce pti o n and contains more detailed information about what was n u l l .
p ub l i c M on ey( fi nal int n ewVa lue , f i n a l C u r r e n c y n e w C u r r e n c y ) {
g e t L og () .tr ac e(" E nter i ng M one y c o n s t r u c t o r " ) ;
v a l u e = new Va lue ;
c u r r en cy = ne wCu r renc y ;

i f ( n ew Cu rre nc y = = nul l ) {
t h ro w new D eta i ledN u llPo int e r E x c e p t i o n ( " n e w C u r r e n c y " ) ;
}

Then the test becomes:


p ub l i c v oi d t es tNu l lInM o neyC ons t r u c t o r ( ) {
try {
n e w Mo ney (5 5, n ull) ;
f a il (" Exp ec ted Deta i ledN ull P o i n t e r E x c e p t i o n " ) ;
} c a tc h( fin al De t aile d Null Poi n t e r E x c e p t i o n e ) {
a s se rt Equ al s(" n ewCu r renc y", e . g e t A r g u m e n t N a m e ( ) ) ;
}
}

In this case, if g e tLog ( ) returns n ull then a regular N u l l P o i n t e r E x c e p t i o n will be thrown, which
will not be caught by the test and will result in a failing test.
The same problem occurs with Ill egalA r g u m e n t E x c e p t i o n s , so there is also a
D et a i l ed Il leg al Arg u ment E xcep tio n .
There is also a Det a ile d Illeg alAr gu m e n t E x c e p t i o n that subclasses I l l e g a l A r g u m e n t E x c e p t i o n
to provide additional information.
p ub l i c M on ey( fi nal int n ewVa lue , f i n a l C u r r e n c y n e w C u r r e n c y ) {
g e t L og () .tr ac e(" E nter i ng M one y c o n s t r u c t o r " ) ;
v a l u e = new Va lue ;
c u r r en cy = ne wCu r renc y ;
i f ( n ew Cu rre nc y = = nul l ) {
t hr ow ne w Det a iled N ullP oin t e r E x c e p t i o n ( " n e w C u r r e n c y " ) ;
}

i f ( n ew Va lue
< 0) {
t hr ow ne w Det a iled I lleg alA r g u m e n t E x c e p t i o n ( " n e w V a l u e " ,
newValue,
"Must be greater than zero");
}
}

The test then looks like this:


p ub l i c v oi d t es tNu l lInM o neyC ons t r u c t o r ( ) {
try {
n e w Mo ney (
- 55 , C ur re ncy .U SD) ;
f a il (" Exp ec ted Deta i ledI lle g a l A r g u m e n t E x c e p t i o n " ) ;
} c a tc h( fin al De t aile d Ille gal A r g u m e n t E x c e p t i o n e ) {
a s se rt Equ al s(" n ewCu r renc y", e . g e t A r g u m e n t N a m e ( ) ) ;
a s se rt Equ al s(n e w In t eger (55 ) , e . g e t A r g u m e n t V a l u e ( ) ) ;
}
}

BaseTestCase
B as e T e st Ca se is the common superclass that all of the GSBase test cases inherit from. It includes
common methods that we would like to see in JUnit such as a s s e r t I n s t a n c e O f ( ) and
a ss e r t Co ll ect io nsE q ual( ) . It also overrides some existing methods in T e s t C a s e that don't
normally provide enough information. For example, a s s e r t S a m e ( ) doesn't tell you what the two objects
are in the case that they are different. B a s e T e s t C a s e overrides a s s e r t S a m e ( ) to print out more
information.
a ss e r t In st anc eO f() asserts that the specified object is an instance of a given class.
f in a l Pa ge pa ge = w ebCl i ent. get P a g e ( n e w U R L ( " h t t p : / / w w w . y a h o o . c o m " ) ) ;
a ss e r t In st anc eO f(" S houl d hav e r e t u r n e d a n h t m l d o c u m e n t " , p a g e , H t m l P a g e . c l a s s ) ;

a ss e r t Co ll ect io nsE q uals ( ) compares two collections for equality independent of order. Often, you
don't care what order items have been added to a collection so long as all the items are present in the
correct quantities. Calling eq u als( ) on the collection will in some cases compare against a specific
ordering.
f in a l Co ll ect io n e x pect e dErr orM e s s a g e s = g e t E x p e c t e d E r r o r M e s s a g e s ( ) ;
f in a l Co ll ect io n c o llec t edEr ror M e s s a g e s = g e t C o l l e c t e d E r r o r M e s s a g e s ( ) ;
a ss e r t Co ll ect io nsE q uals ( expe cte d E r r o r M e s s a g e s , c o l l e c t e d E r r o r M e s s a g e s ) ;

n ot I m p le me nte d( ) indicates that a given test has not yet been completed. This is a simple way to mark
a class so that you get a simple reminder every time you run the tests. This method will print out a
message indicating that the test is not complete and will provide the name of the test.
p ub l i c v oi d t es tNe g ativ e Mone yVa l u e ( ) {
n o t I mp le men te d() ;
}

Amazon

Prev

don't be afraid of buying books

Next

Chapter 6. JUNIT-RELATED TOOLS


Sirs, take you to your tools.
Titus Andronicus, Act 4 Scene 3 William Shakespeare
In this chapter we'll discuss a few tools that help with practicing TDD and/or using JUnit. The first three
assist in verifying that you can have confidence in your tests: one by giving us feedback on how precise
our tests are, and the remaining two by letting us know how much of our code is being tested. We'll
then look at two IDEs that provide support for TDD: Eclipse and Idea.
There's a great number of useful tools out there, with more written every day. I've selected a few that
are handy and specifically related to TDD. Furthermore, I've mostly picked open source tools. Idea and
Clover are the exceptions.

Amazon

Prev

don't be afraid of buying books

Next

JESTER
Jester is a tool written by Ivan Moore for finding code that is not tested. The way it works is by making
systematic changes to your application's source code (as opposed to your tests' source code), recompiling,
and running the test suite. If all the tests still pass after making the change, then there is potentially a
problem and it is reported.
The Jester Web site[URL 21] has links to download as well as more information regarding installation and
use. There was also a paper at XP2001 about Jester[35].
Jester makes changes textually, not syntactically. The changes it makes are selected to change the logic of
the code, for example, replacing true with f als e , forcing a conditional to tr ue or f al s e, etc. The
rationale is that if such a major logical change is made it should cause a test to fail. If all the tests still
pass, then more than likely there is a problem.
As packaged, Jester makes a small number of simple changes to your code, shown in Figure 6.1. By
editing the file, m ut ati on s.cf g, you can add more. Additionally, it changes numeric constants.
Let's see how we can use Jester to improve our tests. Here are a few tests that drove some initial
development of the Mo vie class:

Figure 6.1. Standard Jester changes.


t ru e
f als e
if (
i f(t r ue ||
if (
i f ( t rue | |
if (
i f(f a lse & &
if (
i f ( f als e &&
==
!=
++
--

p ub lic cla ss Te st Mov ie ext ends T es t C ase {


p riv a te Mo vie s tar Wa rs;
p ubl i c v oi d s et Up( ) {
st a rWa rs = ne w M ov ie(" Star W ar s " );
}
p ubl i c v oi d t es tTi tl e() {
as s ert Eq ual s( "Ba d titl e re po rt e d .", " Sta r W a rs ", s ta r Wa rs . ge tT i tl e( ) );
}
p ubl i c v oi d t es tRa ti ng() thr ow s R a tin g O utO f Ra n ge Ex c ep t io n {
ass e rtE qu als (" Def au lt r atin g sh o u ld b e 0" , 0, s ta rW a rs . ge tR a ti ng ( )) ;
fo r (i nt i = 1; i <=5; i++) {

s tar Wa rs. se tRa ti ng(i );


a sse rt Equ al s(" Ra ting not w ha t was s et" , i, s ta rW a rs . ge tR a ti ng ( )) ;

p ubl i c v oi d t es tBa dR atin g()

tr y {
s tar Wa rs. se tRa ti ng(7 );
f ail (" Too b ig of rat ing wa s a c cep t e d") ;
} c atc h (Ra ti ngO ut OfRa ngeE xc ep t i on e x ) {
}

p ubl i c v oi d t es tNe ga tive Rati ng () {


tr y {
s tar Wa rs. se tRa ti ng(- 1);
f ail (" Neg at ive r atin g wa s ac c e pte d " );
} c atc h (Ra ti ngO ut OfRa ngeE xc ep t i on e x ) {
}
}
}

And here's the Mo vie class in Movi e.jav a :


/ **
*C opy r igh t (c) 2 002 S aors a De ve lo p m ent I nc.
*a uth o r S ao rsa D eve lo pmen t In c.
*/
pu bli c cl as s M ov ie {
p riv a te St rin g tit le ;
p riv a te in t r at ing ;
p ubl i c M ov ie( St rin g movi e Ti tl e) {
tit l e = m ovi e Tit le ;
rat i ng = 0;
}
p ubl i c i nt ge tR ati ng () {
ret u rn ra tin g;
}
p ubl i c v oi d s et Rat in g(in t ne wR at i n g) t h row s R a ti ng O ut O fR an g eE xc e pt io n {
if ( new Ra tin g < 0 | | ne wRat in g > 5) {
t h row n ew Ra tin gO utOf Rang eE xc e p tio n ( "Ra t in g s et to " +n e wR at i ng );
}
rat i ng = new Ra tin g;
}

p ubl i c S tr ing g etT it le() {


ret u rn ti tle ;
}

Here's the supporting Ex cept ion class in R ati n g Out O f Ra ng eE x ce p ti on . ja va :

/ **
* Cop y rig ht (c ) 200 2 Saor sa D ev el o p men t Inc .
* aut h or Sa ors a Dev el opme nt I nc .
*/
/* *
* Thi s Ex ce pti on is t hrow n wh en a < cod e > Mov i e< / co de > 's ra ti n g is
* set to a val ue ou ts ide of t he a l l owe d ran g e
*/
p ub lic cla ss Ra ti ngO ut OfRa ngeE xc ep t i on e x ten d s E xc ep t io n {
p ubl i c R at ing Ou tOf Ra ngeE xcep ti on ( ) {
sup e r() ;
}

p ubl i c R at ing Ou tOf Ra ngeE xcep ti on ( S tri n g me s sa g e) {


sup e r(m es sag e) ;
}

To run Jester, you first have to set your CLASSPATH (not by using the - c p option of the j av a command)
to include everything that your test needs to run. For this example the directory structure is:
.

Te stM o vie .c las s


sr c
Mov i e.j av a
Rat i ngO ut OfR an geE xc epti on.j av a

The commands to run Jester on this code are:


$ C LAS S PAT H= /us r/ loc al /jav a/li b/ je s t er. j a r:/ u sr / lo ca l /j a va /l i b/ ju n it .j a r: ./ s rc :
$ j ava jes te r.T es tTe st er T estM ov ie . /sr c

Be sure to run Jester only on a copy of your code. Jester makes changes
to the source files. Don't let it loose on files you can't replace. But then
again, you have everything under version control anyway, right? Right!

Here's the Jester console output: [1]


[1] Edited slightly for formatting and clarity. The '#'characters show the bit of code that Jester
changed.

F or Fi l e . /s rc/ Mo vie .j ava:


2 mu t ati on s s ur viv ed out of 4 ch a n ges
S cor e = 50
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 2 ( ch ar i nde x = 21)
f ro m "2 "
t o "3"
/ **
* Cop y rig ht (c ) #2# 00 2 Sa orsa D ev e l opm e n t I n c.
* @au t hor S aor s
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 18 (c ha r ind e x =43 9 )
f ro m "5 "
t o "6"
( ne wRa t ing < 0 || ne wR atin g > #5 #) {
th ro w n ew Rat ingO ut Of R a nge E x
F or Fi l e . /s rc/ Ra tin gO utOf Rang eE xc e p tio n . jav a :
1 mu t ati on s s ur viv ed out of 1 ch a n ges .
S cor e = 0
. /s rc/ R ati ng Out Of Ran ge Exce ptio n. ja v a

- ch a nge d sou rc e o n line 2 ( ch ar i nde x = 21)


f ro m "2 "
t o "3"
/ **
* Cop y rig ht (c ) #2# 00 2 Sa orsa D ev e l opm e n t I n c.
* @au t hor S oar s
3 m uta t ion s sur vi ved o ut o f 5 ch an g e s. S c ore = 4 0
t oo k 0 min ut es

One shortcoming of the current implementation of Jester is immediately obvious: It makes (and counts)
changes in comments.[2] This leads to false positives in the results since changes to comments will, of
course, not cause tests to fail. Future versions of Jester will no doubt address this and other shortcomings,
doing more syntactic analysis of the code when deciding what changes to make.
[2] As the writing of this book finished, Jester had some experimental code to skip comments.

So, the comment changes account for two out of the three potential problems. What's the third one?
Looking at the output we can see that this change was to the constant that sets the upper limit to valid
ratings. The requirement was that valid ratings were in the range 05, inclusive. This code looks OK, so
why did a change to the upper limit (changing it to 6) not break a test? You would think that a change to
a limit should break a testafter all, boundary conditions should be tested! Hmm... let's look at the test
that checks for rating above the acceptable range:
p ub lic voi d tes tB adR at ing( ) {
t ry {
st a rWa rs .se tR ati ng (7);
fa i l(" To o b ig of r atin g wa s ac c e pte d " );
} ca t ch (R ati ng Out Of Rang eExc ep ti o n ex ) {
}
}

OK, all together now: "Ah Ha!" Our test wasn't close enough to the boundary condition. It tests a rating
too high. The upper limit could be changed to 6 and still let this test pass. The test is sloppy and needs to
be tightened up. The value passed to s et Ra t i ng should be 6, which is one more than the allowable limit.
Making this change results in the following Jester output:
F or Fi l e . /s rc/ Mo vie .j ava:
1 mu t ati on s s ur viv ed out of 4 ch a n ges .
S cor e = 75
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 2 ( ch ar i nde x = 21)
f ro m "2 "
t o "3"
/ **
* Cop y rig ht (c ) #2# 00 2 Sa orsa D ev e l opm e n t I n c.
* @au t hor S aor s
F or Fi l e . /s rc/ Ra tin gO utOf Rang eE xc e p tio n . jav a :
1 mu t ati on s s ur viv ed out of 1 ch a n ges

S cor e = 0
. /s rc/ R ati ng Out Of Ran ge Exce ptio n. ja v a
- ch a nge d sou rc e o n line 2 ( ch ar i nde x = 21)
f ro m "2 "
t o "3"
/ **
* Cop y rig ht (c ) #2# 00 2 Sa orsa D ev e l opm e n t I n c.
* @au t hor S aor s
2 m uta t ion s sur vi ved o ut o f 5 ch an g e s. S c ore = 6 0
t oo k 0 min ut es

That's better. The spurious comment changes are still there, but everything else is as it should be. This
example showed one way in which Jester can be used to improve a test.
Another way in which Jester can help us is if we found that a method contained several changes that did
not cause a test to fail. In this case, it is likely that we need more tests. Let's give that a try. We'll add a
t oS tri n g() method to M ovie : [3]
[3] If we were doing this for real, we'd write a test first. Since we want to have an untested method,
we'll just write the method. Don't try this at home!

p ub lic Str in g t oS tri ng () {


S tri n gBu ff er re sul t = ne w St ri ng B u ffe r ( );
r esu l t.a pp end (" (") ;
r esu l t.a pp end (t itl e) ;
r esu l t.a pp end (" - ") ;
i f ( r ati ng == 0 ) {
re s ult .a ppe nd ("N o Rati ng") ;
} el s e {
re s ult .a ppe nd (ra ti ng);
}
r esu l t.a pp end (" )") ;
r etu r n r es ult .t oSt ri ng() ;

Now when we run Jester we get this output:


F or Fi l e . /s rc/ Mo vie .j ava:
5 m u tat io ns su rvi ve d ou t of 8 c h a nge s .
Sco r e = 3 8
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 2 ( ch ar i nde x = 21)
f ro m "2 "
t o "3"
/ **
* Cop y rig ht (c ) #2# 00 2 Sa orsa D ev e l opm e n t I n c.
* @au t hor S aor s
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 34 (c ha r ind e x =80 0 )
f ro m "i f ("

t o "if (tru e || "


tl e) ;
re su lt. ap pen d( " - ");
#i f (#r at ing = = 0) {
r esu lt .app end( "N o R a tin g
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 34 (c ha r ind e x =80 0 )
f ro m "i f ("
t o "if (fal se & & "
t le );
r es ult .a ppe nd (" - ");
# if (# ra tin g == 0 ) {
res ul t.ap pend (" No R ati n g
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 34 (c ha r ind e x =81 1 )
f ro m "= ="
t o "!="
I t app en d(" - ");
i f (ra ti ng #= =# 0 ) {
res ul t.ap pend (" No R ati n g ");
} e lse
. /s rc/ M ovi e. jav a
- ch a nge d sou rc e o n line 34 (c ha r ind e x =81 4 )
f ro m "0 "
t o "1"
a pp end (" - ") ;
i f (ra ti ng == #0# ) {
res ul t.ap pend (" No R ati n g ");
} e lse {
F or Fi l e . /s rc/ Ra tin gO utOf Rang eE xc e p tio n . jav a :
1 mu t ati on s s ur viv ed out of 1 ch a n ges .
S cor e = 0
. /s rc/ R ati ng Out Of Ran ge Exce ptio n. ja v a
- ch a nge d sou rc e o n line 2 ( ch ar i nde x = 21)
f ro m "2 "
t o "3"
/ **
* Cop y rig ht (c ) #2# 00 2 Sa orsa D ev e l opm e n t I n c.
* @au t hor S aor s

6 m uta t ion s sur vi ved o ut o f 9 ch an g e s. S c ore = 3 4


t oo k 0 min ut es

What can we learn from this output? Well, Jester made four changes to line 34 of M ov ie . ja va (the i f
statement in the to St rin g( ) method) that didn't cause any test to fail. That indicates that the
t oS tri n g() isn't being tested well enough. Well, that's no surprise since we added it explicitly to get this
kind of result. But consider how useful it would be to run Jester occasionally (daily?) to catch this kind of
hole in our tests. This example is somewhat overstated, since if we are practicing TDD, it's not likely that
this much code would not be tested. But we can easily get ourselves into a situation where a branch of a
conditional isn't being tested well enough. Jester can help you find cases like that. This is especially useful
for teams that are in the process of transitioning to TDD and adding tests to their existing code.
In addition to the console output, Jester creates an XML file that contains comparable information. Here's
an example:
< Je ste r Rep or t>
< Jes t edF il e f il eNa me ="./ src/ Mo vi e . jav a "
nu mbe rO fCha nges Th at D i dNo t C aus e Te s ts To F ai l =" 5"
nu mbe rO fCha nges =" 8"
sc ore =" 38">
<C h ang eT hat Di dNo tC ause Test sT oF a i l i n d ex= " 21 " f ro m =" 2 " to = "3 "/ >
<C h ang eT hat Di dNo tC ause Test sT oF a i l i n d ex= " 80 0 " fr o m= " if ( " t o= " if ( t ru e | |" />
<C h ang eT hat Di dNo tC ause Test sT oF a i l i n d ex= " 80 0 " fr o m= " if ( "
t o = "if (f a ls e & am p ;& am p ;" />
<C h ang eT hat Di dNo tC ause Test sT oF a i l i n d ex= " 81 1 " fr o m= " == " t o= "! = "/ >
<C h ang eT hat Di dNo tC ause Test sT oF a i l i n d ex= " 81 4 " fr o m= " 0" t o =" 1" / >
< /Je s ted Fi le>
< Jes t edF il e f il eNa me ="./ src/ Ra ti n g Out O f Ran g eE x ce pt i on . ja va "
nu mbe rO fCha nges Th at D i dNo t C aus e Te s ts To F ai l =" 1"
nu mbe rO fCha nges =" 1"
sc ore =" 0">
<C h ang eT hat Di dNo tC ause Test sT oF a i l i n d ex= " 21 " f ro m =" 2 " to = "3 "/ >
< /Je s ted Fi le>
< /J est e rRe po rt>

Jester also has python scripts that take that XML file and use it to create a set of HTML pages that detail
each change that it was able to make without breaking tests. Table 6.1 and Figure 6.2 show an example of
this. This is the same information, but presented in a more meaningful way.

Table 6.1. Jester Index for the Movie Example

In Chapter 20 we will use Jester on our example project.

Amazon

Prev

don't be afraid of buying books

Next

NOUNIT
NoUnit allows you to see how good your JUnit tests are. It generates a report from your code to
graphically show you how many of your project's methods are being tested, and how well.
The report (see Table 6.2 for a sample) includes a line for each method in your code indicating:
Whether it's called directly, indirectly, or not at all from any test method. This is indicated by the
color of the third and fourth columns and the icon in the third column:

Figure 6.2. Sample Jester change report for the Movie example.
pub li c S tr ing toSt ring ( ) {
S tri ng Buff er r esul t = n e w St r in g Bu ff e r( ) ;
r esu lt .app end( "(") ;
r esu lt .app end( titl e ) ;
r esu lt .app end( " - " ) ;
if ( if (t rue | |if (fal se & & r ati n g
==!=
01) {
re sult .app e n d(" N o Ra t in g ") ;
} el se {
re sult .app e n d(r a t ing ) ;
}
r esu lt .app end( ")") ;
r etu rn res ult. toSt r i ng( ) ;
}

how many times it's called for all test methods reported on (the second column)
the minimum number of methods between it and a test method (the first column).

Table 6.2. NoUnit Report for the Movie Example


[View full size image]

The fourth column also contains the method name being reported on in that line, and the fifth column
indicates the class of that method.
NoUnit works by analyzing the class files from a project. It can answer several questions about your
tests:
1. Are all methods called from a test?
2. Are all methods tested reasonably directly?
3. Where are the holes in our test net?
4. Are some areas better tested than others?
If you are practicing Test-Driven Development, NoUnit isn't as useful as it would be if you were writing
tests after the code. Doing TDD implies there is no code in the system that wasn't written to get a test
to pass. That means that everything should be covered by some test.
Even so, it is worth running NoUnit on your code occasionally to make sure some-thing bad hasn't
happened. If you are working with legacy code, NoUnit can be very useful as you are adding tests to
existing code. Since NoUnit works with class files, and not the Java source code, you can even use it to
analyze the test coverage of third-party libraries.
To see NoUnit in action, let's run it on the code we used in the final Jester example (i.e., including the
untested toS tr ing () method):
$ pwd
/ h ome/d ave /p roj ec ts/b ooks /tdd / s amp l e s/n o u ni t
$ ls
M o vie.c las s

M o vie.j ava
o u tput
R a tingO utO fR ang eE xcep tion .cla s s
R a tingO utO fR ang eE xcep tion .jav a
T e stMov ie. cl ass
T e stMov ie. ja va
$ $ N OUNIT _HO ME /ba tc h/no unit .
G e nerat ing R epo rt :
X M L in: ./o ut put /p roje ct.x ml
X S LT fi le: /h ome /d ave/ work spac e / nou n i t/b a t ch /. ./ x sl t /n o- u ni t. x sl
O u tput fil e: ./o ut put/ inde x.ht m l
T r ansfo rma ti on Su cces sful

The results were shown in Table 6.2. It is much clearer in that report that the t oS tr i ng () method is
not being tested. Also notice that it has identified that the constructors for the exception class are only
called indirectly.
NoUnit excels at finding gaping holes in your test suite, that is, methods that are not tested, or only
tested indirectly. Jester is better at finding smaller holesconditional branches that aren't being tested.
The result of running NoUnit is an HTML page containing the report and legend, ready for posting on our
project's intranet site. In the example, the table has been extracted from the entire page. The NoUnit
Web site[URL 20] gives instructions on downloading, installing, and using NoUnit.
There are some problems with NoUnit that I found; it has some bugs and short-comings. Fortunately,
there is a fairly large online user community that has posted bug reports, solutions, and workarounds at
the NoUnit SourceForge site. Since NoUnit is open source, you can easily fix the problems on your own,
especially in the cases where someone else has figured it out and posted instructions.
In Chapter 20 we will use NoUnit on the project we develop, and in the process bring to light some of its
shortcomings.

Amazon

Prev

don't be afraid of buying books

Next

CLOVER
Clover is a classic code coverage tool. During a run it measures what code is executed and how many
timesdown to the statement level. The results can be reported in a variety of formats, including a nice
HTML format that lets you drill down to the source code level. At the package and class method it shows
the percentage of the code which was executed. At the source level it shows how often each line was
executed, and visually highlights lines that were not executed.
Clover works by instrumenting the source code. When the code is then compiled and run it writes
execution information to a logfile which is later processed to generate the report. This instrumentation is
transparent and automatic. All that you need to do is add some additional lines to your Ant build script.
Clover is very nicely integrated with Ant, and hence with anything that can use Ant to perform builds.
The instrumentation is done to a copy of your source; your source tree is untouched by the process.
Clover's Ant integration is done very nicely. Clover institutes its own compiler adapter in place of the
standard ja vac adapter. The Clover adapter takes care of instrumenting the code in your source tree,
placing the results in a temporary location (that it cleans up afterward), and then using the original
compiler adapter to compile the instrumented code, placing the resulting class files in your project's build
directory. Totally transparent. Nice job.
A benefit of Clover's approach (i.e., instrumenting the source) is that no modification or extensions are
required to the runtime environment.
I won't go into detail here on how you hook Clover into your b ui l d. x ml ; the Clover site [URL 22] has
a nicely done user guide and tutorial walking you through it. What I will show you is some examples of
the HTML report. These were generated from the tutorial example that ships with Clover, the JUnit
Money example. We'll start at the top level with the project report, shown in Figure 6.3. You can see
the coverage results for the overall project, as well as for each top level package, of which there is a
single one in this example.

Figure 6.3. Project level Clover report.

By selecting the package, we can see the package level report, which shows us results for each class
(and subpackages, if there were any). See Figure 6.4.

Figure 6.4. Package level Clover report.


[View full size image]

Selecting a class shows us the code with execution counts. Figure 6.5 shows us a piece of the M o ne y
class that Clover has found never to be executed: one branch in the e q ua ls ( ) method that handles
comparing with a zero value Mone y. Notice the color highlighting on the execution counts of the
offending lines. [4]
[4] In reality the highlighting is pink, but that didn't have enough contrast when converted to
grayscale.. . so I've taken artistic license.

Figure 6.5. Class level Clover report.


[View full size image]

In addition to the HTML report, Clover can generate XML and plain text reports. It also includes a Swingbased report viewer, shown in Figure 6.6. This flexibility makes it a wonderful tool. You can quickly
browse through the result on a local build with the Swing GUI, or generate HTML reports as part of a
central build process.

Figure 6.6. Swing-based Clover report viewer.


[View full size image]

We're discussing Clover here because it is especially useful for us if the execution used for gathering
coverage data is the run of the top level test suite. Clover's report will tell us if there is production code
that is not getting tested. If we're using TDD, theoretically there shouldn't be any. However, something
almost always slips through, whether it's a t o Str i n g() method, or an event handler that never gets
used. Using a coverage analyzer like Clover in conjunction with Jester will provide you with a fairly
complete picture of how comprehensive and effective your tests are.
One thing about Clover that sets it apart from most of the others we've looked at: it's a commercial
product. Even so, the price is very reasonable. Given the data it provides it's worth what it costs.

Amazon

Prev

don't be afraid of buying books

Next

ECLIPSE
Eclipse[URL 32] is a fairly new development environment that is becoming very popular. It was written
by Object Technology International (OTI), a subsidiary of IBM. OTI is the company behind IBM's
Smalltalk and Java tools. IBM's Websphere Application Development Studio (WSAD) is built on top of
Eclipse. Eclipse is an open source project that is freely available for download.
What makes Eclipse so interesting is how XP- and TDD-friendly it is. It should be no surprise that one of
the people central to the Eclipse effort is Erich Gamma (coauthor of Design Patterns [19] and JUnit).

JUnit integration
Eclipse is architected as a common core platform and a series of plugins. In fact, the Java development
environment is itself a set of Eclipse plugins. The JUnit plugin is of special interest, in particular for the
way it is integrated into the Java toolset. JUnit plugin's features include:
a TestCase creation wizard
a TestSuite creation and updating wizard
a deeply integrated TestRunner that provides:
- a view for test results
- the ability to hide packages in the stack trace
- clickable test lists and stack trace (clicking opens the corresponding file/line in an
editor)
- an easy way to rerun tests
an integration with the Run menus, allowing a class to be run as a JUnit Test, including in the
debugger
the ability to keep JUnit running to increase the speed of running tests, and to allow the
rerunning of individual tests.
Having JUnit integrated this tightly with the development environment greatly streamlines the TDD
experience.

Problem indication
Eclipse indicates problems that will prevent code from compiling. Offending code is underlined with a
wavy red line, and the line is flagged in the left margin with a yellow lightbulb. You can have Eclipse
offer corrections by clicking on the lightbulb, or positioning the caret on the offending code and typing
CTR L -1. Select a correction from the list to have it applied. Figure 6.7 shows an example of this in
action.

Figure 6.7. Problem indication and correction in Eclipse.

This capability gets us to the point where we have a compiling, failing test very quickly. It really
streamlines the process of letting the compiler tell you what you need to do. You can skip the manual
process of compiling, checking the error messages, adding the appropriate stubs, etc. You can have
Eclipse add the stubs as appropriate (including classes, interfaces, methods, instance variables, and local
variables) while you are still thinking about the problem at hand. Then you can get on with making the
test pass.

Refactoring support
Eclipse supports several automated refactorings that greatly speed development. Refactoring provided
includes:
Extracting a method
Renaming a package
Renaming a class or interface
Renaming a method
Renaming a field
Renaming a local variable
Renaming method parameters
Reordering method parameters
Extracting a local variable
Inlining a local variable
Self-encapsulating a field
Replacing a local variable with a query
Pulling members up to their superclass

Moving static members between types


Being able to use automated refactorings increases both your confidence, since the refactorings are
proven not to introduce errors, and your speed, since you don't have to perform each step of the
refactoring manually.
Eclipse includes all of the standard features expected in an IDE, such as code assist, incremental
compilation, documentation lookup, etc., all of which serve to streamline the programming process.

Amazon

Prev

don't be afraid of buying books

Next

IDEA
wit h Brya n D ol ler y
I've asked Bryan to talk about IDEA IDE from IntelliJ. Bryan is a well-known consultant
in New Zealand and an outspoken IDEA user and advocate.
IDEA[URL 33] has many features that will help with practicing TDD; in this section we will look at
several of them.
If I use a class that hasn't yet been imported, IDEA will use a tool-tip to tell me, and offer to import it
for me very simple, very fast. Once the class is imported its methods and attributes are available to
me for code completion.
If I use a class that doesn't exist, something that we all do at first with TDD, then IDEA puts a lightbulb
in the gutter which, if I click on it, offers me a number of code generation options, including the option
to create the class for me.
If I have an object and attempt to call a method that doesn't exist, IDEA will use the lightbulb again to
tell me that it can help out. Clicking on it gives me the option to create the method. Here is where IDEA
starts to show its real intelligence. When generating a method, IDEA has to make certain assumptions:
the return type, the parameter types, and their names.
If I have started with:
fra g ment. doS om eth in g("n ame" );

within fragment's class, IDEA can generate:


voi d doSo met hi ng( St ring s) {
}

It will then put a red box (a live template query) around void, with my cursor in it. It's telling me that
void was an assumption, and that I need to either accept it by pressing enter or tab, or change it. Once
I'm happy with the return type, I can press enter to move to the next assumption, the type for the
parameter. The final assumption here is the name for the parameter, which I don't like, so I can change
it. Of course, if I provide it with more information, say, by assigning the return type to a variable, then
IDEA will make better assumptions.
To run the tests I have a few choices available to me. I can compile, test, run, or debug, at a class
level or a method level. If I right-click on a method then it assumes that I'm interested in that method
and offers choices based on the method, but if I right-click on the class name (or a gap between
methods) then I'll be offered these choices for the whole class (which is what I usually want). I can also
choose to run all the tests within a given package.
To run or debug I don't need a main method, only the test methods. Being able to debug at a testmethod level is very useful. I don't have to play around getting to the method I really want to test; it's
all done for me.
The integration with JUnit is very tight. If the GUI runner shows a stack trace I can double-click on a
line of the trace and be taken straight to that line in the editor. Fix the error, recompile, and alt-tab
back to the GUI runner to rerun the tests. I can also choose to run the text-runner, the output of which
appears in IDEA's messages window.
However, refactoring is the jewel in the crown for IDEA. Look at your current editor right now, and open
the refactoring menu. If it's not at least half the size of your screen then you're really missing out.

There are 22 refactorings listed on its menu, but some of those are multirefactorings. Take, for example,
the rename refactoring. It works on variables of any scope, methods, classes, and packages that
makes it four similar refactorings in one. When it renames a class, it also renames the file it's in, and
when it renames a package it'll rename the directory and ensure that the change is recorded in CVS
this is a very bright tool, nothing is left unfinished.
One of my favorites is Change Signature I can use it to add, remove, reorder, and rename parameters
to a method all at once. If I change a parameter's name it'll do a rename refactoring automatically
for me, before it does the rest of the changes. If I add a parameter it asks for a default value. If I
reorder the parameters it'll ensure that the method is called correctly throughout my project.
IDEA attempts to transparently automate repetitive and common tasks. It leaves nothing undone, and
asks for clarification when it's guessing. It's highly polished, looks great, and will probably speed up your
coding significantly.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 7. MOCK OBJECTS


You mock me, sir.
Hamlet ,Act 5 Scene 2 William Shakespeare
What is the Holy Grail of TDD? What do you strive for when you write tests? Something like:
focused tests: ideally one assert each
independent tests: the fixture is built and cleaned up for each test, allowing tests to run in any
order
fast tests: you want to be able to run them frequently
There is a potential conflict here. Small, focused tests mean that you will have lots of tests, each very
small and focused. To keep them independent you need to have a clean fixture for each. That means
you need to rebuild (and re-cleanup) the fixture for each test, every time it is run. OK, so far so good.
The last goal is the problem. We want the tests to be fast...as fast as possible...so that we can run them
frequently. For trivial fixtures, that's OK. But what happens if your test fixtures get complex and timeconsuming to build and cleanup?
You may simply have a lot of fixture to put in place, or you may be working in the context of a large,
complex system. This could be a database, a workflow system, or some system for which you are
developing an extension (e.g., an IDE). Your fixture may involve getting the system into a specific state
so that it responds the way your tests require. This may be impossible to do quickly.
In light of such problematic test resources, how do we reconcile our three goals of focus, independence,
and speed? Mock objects provide one approach that has proven successful. Mock objects are used when
it is diffcult or impossible to create the required state for a problematic resource, or when access to that
resource is limited. There are other interesting uses for mock objects. In this chapter we'll talk about
mock objects, what they are, how to use them, a framework for working with them, and some tools to
make that easier.

Amazon

Prev

don't be afraid of buying books

Next

MOCK OBJECTS
Mock objects (we will follow convention and call them mocks)take the place of real objects for the
purposes of testing some functionality that interacts with and is dependent on the real objects. An
excellent paper on mocks was presented at XP2000[32], and there is a site dedicated to the idea[URL
23].
The basic idea behind mocks is to create lightweight, controllable replacements for objects you need in
order to write your tests. Mocks also enable you to specify and test your code's interaction with the
mocks themselves.

Amazon

Prev

don't be afraid of buying books

Next

AN ILLUSTRATIVE EXAMPLE
We're going to consider a simple, somewhat contrived example of a situation where mocks can help.
First we will do it the hard way: creating our mocks entirely by hand. Later we will explore excellent
tools for automating much of the business of mock creation and setup.
Our example involves writing an adventure game in which the player tries to rid the world of foul
creatures such as Orcs. When a player attacks an Orc they roll a 20-sided die to see if they hit or not.
A roll of 13 or higher is a hit, in which case they roll the 20-sided die again to determine the effect of
the hit. If the initial roll was less than 13, they miss.
Our task at the moment is to test the code in P l a y e r that governs this process. Here's P l a y e r (I
know, we aren't test driving this code. ..assume we inherited it):
p ub l ic c las s Play e r {
D i e my D20 = nu l l;
p u bl ic Pl a yer( D ie d 2 0) {
my D2 0 = d20;
}
p u bl ic bo o lean atta c k(Or c an O r c ) {
if ( myD 2 0.ro l l()
> = 1 3) {
re tur n hit ( anOr c );
} el se {
re tur n mis s ();
}
}
p r iv at e b o olea n hit ( Orca nOrc ) {
an Or c.i n jure ( myD2 0 .rol l()) ;
re tu rn t rue;
}

p r iv at e b o olea n mis s () {
re tu rn f alse ;
}

Here's the initial take at a test. We'll start simply with the case where the attack misses. We'll assume
that a Di e class already exists:
p ub l ic c las s Die {
p r iv at e i n t si d es = 0;
p r iv at e R a ndom gene r ator = n u l l ;
p ub l ic D ie( i nt n u mber O fSid es) {
s i de s = n u mber O fSid e s;
g e ne ra tor = newR a ndom ( );
}
p ub l ic i nt r oll( ) {
r e tu rn ge n erat o r.ne x tInt (sid e s ) + 1 ;
}
}

Here's a first stab at the test for a missed attack:


p ub l ic v oid test M iss( ) {
D i e d2 0 = new D ie(2 0 );
P l ay er ba d Figh t er = new Play e r ( d 2 0 ) ;
O r c an Orc = ne w Orc ( );
a s se rt Fal s e("A t tack shou ld h a v e m i s s e d . " , b a d F i g h t e r . a t t a c k ( a n O r c ) ) ;

The problem is that there is a random number generator involved. Sometimes the test passes, other
times it fails. We need to be able to control the return value in order to control the preconditions for the
test. This is a case where we cannot (or rather, should not) get the actual test resource into the state
we need for the test. So instead, we use a simple mock object for that. Specifically, we can mock the
D ie class to return the value we want. But first we need to extract an interface from D i e , and use it in
place of D ie: [1]
[1] There are other ways of approaching this, such as creating a subclass that returns the constant,
but I like working with interfaces.

p ub l ic i nte r face Roll a ble {


i n t ro ll( ) ;
}
p ub l ic c las s Die impl e ment s Ro l l a b l e {
/ /. . .
}
p ub l ic c las s Play e r {
R o ll ab le m yD20 = nu l l;
p u bl ic Pl a yer( R olla b le d 20) {
my D2 0 = d20;
}
//. . .
}

Now we can create a mock for a 20-sided die that always returns a value that will cause an attack to
miss, say, 10:
p ub l ic c las s Moc k D20F a ilin gAtt a c k i m p l e m e n t s R o l l a b l e {
p u bl ic in t rol l () {
re tu rn 1 0;
}
}

Now we use the mock in our test:


p ub l ic v oid test M iss( ) {
R o ll ab le d 20 = new M ockD 20Fa i l i n g A t t a c k ( ) ;
P l ay er ba d Figh t er = new Play e r ( d 2 0 ) ;
O r c an Orc = ne w Orc ( );
a s se rt Fal s e("A t tack shou ld h a v e m i s s e d . " , b a d F i g h t e r . a t t a c k ( a n O r c ) ) ;
}

There, the test always passes now. Next, we write a corresponding test with a successful attack:
p ub l ic v oid test H it() {
R o ll ab le d 20 = new M ockD 20Su c c e s s f u l A t t a c k ( ) ;
P l ay er go o dFig h ter = new Pla y e r ( d 2 0 ) ;
O r c an Orc = ne w Orc ( );
a s se rt Tru e ("At t ack s houl d ha v e h i t . " , g o o d F i g h t e r . a t t a c k ( a n O r c ) ) ;
}

This requires a new mock:


p ub l ic c las s Moc k D20S u cces sful A t t a c k i m p l e m e n t s R o l l a b l e {
p u bl ic in t rol l () {
re tu rn 1 8;

Now, these two mocks are almost identical, so we can refactor and merge them into a single
parameterized class:
p ub l ic c las s Moc k Die i mple ment s R o l l a b l e {
p r iv at e i n t re t urnV a lue;
p u bl ic Mo c kDie ( int c onst antR e t u r n V a l u e ) {
re tu rnV a lue = con s tant Retu r n V a l u e ;
}

p u bl ic in t rol l () {
re tu rn r etur n Valu e ;
}

Our tests are now:


p ub l ic v oid test M iss( ) {
R o ll ab le d 20 = new M ockD ie(1 0 ) ;
P l ay er ba d Figh t er = new Play e r ( d 2 0 ) ;
O r c an Orc = ne w Orc ( );
a s se rt Fal s e("A t tack shou ld h a v e m i s s e d . " , b a d F i g h t e r . a t t a c k ( a n O r c ) ) ;
}
p ub l ic v oid test H it() {
R o ll ab le d 20 = new M ockD ie(1 8 ) ;
P l ay er go o dFig h ter = new Pla y e r ( d 2 0 ) ;
O r c an Orc = ne w Orc ( );
a s se rt Tru e ("At t ack s houl d ha v e h i t . " , g o o d F i g h t e r . a t t a c k ( a n O r c ) ) ;
}

Next, we want to write tests for the cases where an attack hurts the Orc, and where it kills it. For this
we will need to extend Mock D ie so that we can specify a sequence of return values (successful attack
followed by the amount of damage). In order to maintain the current behavior, M o c k D i e repeatedly
loops through the return value sequence as r o l l ( ) is called.
p ub l ic c las s Moc k Die i mple ment s R o l l a b l e {
p r iv at e V e ctor retu r nVal ues = n e w V e c t o r ( ) ;
p r iv at e i n t ne x tRet u rned Inde x = 0 ;
p u bl ic Mo c kDie ( ) {
}
p u bl ic Mo c kDie ( int c onst antR e t u r n V a l u e ) {
ad dR oll ( cons t antR e turn Valu e ) ;
}
p u bl ic vo i d ad d Roll ( int retu r n V a l u e ) {
re tu rnV a lues . add( n ew I nteg e r ( r e t u r n V a l u e ) ) ;
}

p u bl ic in t rol l ()

in t val = (( I nteg e r)re turn V a l u e s . g e t ( n e x t R e t u r n e d I n d e x + + ) ) . i n t V a l u e ( ) ;


if ( nex t Retu r nedI n dex >=re t u r n V a l u e s . s i z e ( ) ) {
ne xtR e turn e dInd e x = 0;
}
re tu rn v al;

Using the R oll a ble interface allows us to easily and cleanly create mocks without impacting the
P la y er class at all. We can see this in the class diagram shown in Figure 7.1.

Figure 7.1. Class diagram of the Player and Die aspects of the example.
[View full size image]

So far we've written simple mocks, really just stubs that return some predefined values in response to a
method call. Now we'll start writing and using a real mock, one that expects and can verify specific calls.
Again, so far, we're doing it all by hand.
First, consider this implementation of Or c and G a m e :
p ub l ic c las s Orc {
p r iv at e G a me g a me = null ;
p r iv at e i n t he a lth = 0;

p u bl ic Or c (Ga m e th e Game , in t h i t P o i n t s )

ga me = t heGa m e;
he al th = hit P oint s ;

p u bl ic vo i d in j ure( i nt d amag e ) {
he al th
- =d a ma ge ;
if ( hea l th
< =0 ) {
di e() ;
}
}
p r iv at e v o id d i e() {
ga me .ha s Died ( this ) ;
}

p u bl ic bo o lean isDe a d() {


re tu rn h ealt h <=0 ;
}

p ub l ic i nte r face G ame {


v o id h asD i ed(O r c or c );
}

Now we can write tests like this:


p ub l ic v oid test N oKil l ()
{
M o ck Ga me m ockG a me = new Mock G a m e ( ) ;
O r c st ron g Orc = new Orc( mock G a m e , 3 0 ) ;
M o ck Di e d 2 0 = n ew M o ckDi e();
d 2 0. ad dRo l l(18 ) ;
d 2 0. ad dRo l l(10 ) ;

P l ay er fi g hter = ne w Pla yer( d 2 0 ) ;


f i gh te r.a t tack ( stro n gOrc );
a s se rt Fal s e("T h e or c sho uld n o t h a v e d i e d . " , s t r o n g O r c . i s D e a d ( ) ) ;
m o ck Ga me. v erif y ();

p ub l ic v oid test K ill( ) {


M o ck Ga me m ockG a me = new Mock G a m e ( ) ;
O r c we akO r c = n ew O r c(mo ckGa m e , 1 0 ) ;
m o ck Ga me. e xpec t HasD i ed(w eakO r c ) ;
M o ck Di e d 2 0 = n ew M o ckDi e();
d 2 0. ad dRo l l(18 ) ;
d 2 0. ad dRo l l(15 ) ;

P l ay er fi g hter = ne w Pla yer( d 2 0 ) ;


f i gh te r.a t tack ( weak O rc);
a s se rt Tru e ("Th e orc shou ld b e d e a d . " , w e a k O r c . i s D e a d ( ) ) ;
m o ck Ga me. v erif y ();

The one thing that is missing is Mo ckGa m e . Here it is:


p ub l ic c las s Moc k Game impl emen t s G a m e
{
p r iv at e O r c de a dOrc = nu ll;
p r iv at e O r c or c Expe c tedT oDie = n u l l ;
p u bl ic vo i d ha s Died ( Orc orc) {
if ( orc != o r cExp e cted ToDi e ) {
As ser t .fai l ("Un e xpec ted o r c d i e d . " ) ;
}
if ( dea d Orc ! = nu l l) {
As ser t .fai l ("On l y ex pect e d o n e d e a d o r c . " ) ;
}
}

de ad Orc = or c ;

p u bl ic vo i d ex p ectH a sDie d(Or c d o o m e d O r c ) {


or cE xpe c tedT o Die = doo medO r c ;
}

p u bl ic vo i d ve r ify ( ) {
As se rt. a sser t Equa l s("D oome d O r c d i d n ' t d i e . " , o r c E x p e c t e d T o D i e , d e a d O r c ) ;
}

When an Or c dies it reports the fact to the game. In the case of the tests, this is a M o c k G a m e that
checks that the dead orc is the one that was expected and that this is the only dead orc so far. If either
of these checks fail, then the mock causes the test to fail immediately by calling A s e r t . f a i l ( ) .When
the mock is set up by the test it is told what orc it should expect to die. At the end of the test the
M oc k Ga me can be asked to verify that the expected O r c died.
This is a trivial mock, but it should give you a taste of what is possible. Figure 7.2 shows this part of
the class diagram, while Figure 7.3 shows the sequence diagram for t e s t K i l l ( ) .

Figure 7.2. Class diagram of the Orc and Game aspects of the example.

Figure 7.3. Sequence diagram of the


[View full size image]

t e ss tt KK ii ll ll (( ))

test.

You can now easily see that as our mocks get more complex, they get harder to write and maintain, if
we write them from scratch each time. The folks that evolved the mock objects concept found this out
quickly, and as happens when you find yourself writing similar code a lot, they developed a framework
for creating mocks and tools to make the job easier. Again, we'll have more on that later.

Amazon

Prev

don't be afraid of buying books

Next

USES FOR MOCKOBJECTS


Using mocks would seem to have several advantages beyond keeping test fixtures light-weight and fast
to build and cleanup. There are several reasons to use mocks. We'll look at them in turn:

To help keep the design decoupled


Using mocks helps to enforce interface-centric design. Programming against a mock removes the
possibility of depending on the implementation of the objects being used.

To check your code's usage of another object


By setting expectations in a mock, we can verify that the code we are working on properly uses the
mocked interface.

To test-drive your code from the inside out


By setting return values in the mocks, we can provide specific information to the code under
development, then test that the resulting behavior is correct.

To make your tests run faster


By mocking things like communications or database subsystems, we can avoid the overhead involved in
setting up and tearing down connections, etc.

To make it easier to develop code that interacts with hardware devices,


remote systems, and other problematic resources
If the code we are writing needs to interact with some tricky resource, we can create a proxy layer to
isolate us from the real resource. Then we can use a mock of that layer. This allows us to develop code
without needing access to the actual device or system.

To defer having to implement a class


We can use mocks for classes that we haven't written yet but that the code we are working on needs to
interact with. This lets us defer implementing those classes, letting us focus on the interface between
them and the code we are writing. This allows us to leave implementation decisions until we know more.
If all you need for your test is to simulate the behavior, a mock will suffce.

To let us test-drive components in isolation from the rest of the system


By mocking the component(s) that the code being written has to interact with, we can focus on it in
isolation. This lets us go faster, because complex interactions with other components are fully under our
control.

To promote interface-based design


Mocks are easiest to use when you adopt an interface-heavy style of programming. [2]
[2] This is simply a better way of programming. By using interfaces, you make it easier to decouple
your classes. In their book on Java design techniques[12], Coad et al. dedicate more pages to
interface- centric design than any of the other techniques they discuss. They boil down the reasons to
use interfaces to three characteristics that an interface- rich system has: flexibility, extensibility, and
plugability. Using mocks is made easier if you freely use interfaces, because if methods expect an
interface, you can easily create a mock implementation and use that in place of a real
implementation. It's no coincidence that Extract Interface is a very important refactoring.

To encourage composition over inheritance


This is related closely to the previous point. In general, inheritance is overused. As a result, monolithic
functionality builds up in an inheritance hierarchy. A class is and always will be whatever it inherits.
Mocking any aspect of a class in an inheritance hierarchy is diffcult because of all of the baggage that
the class inherits. The desire to mock certain aspects (e.g., persistence) tends to yield smaller classes
that get smart by collaborating with other classes. Then different implementations of these classes,
including mocks, can be easily interchanged.

To refine interfaces
Using a mock for a class you will eventually need to implement gives you an early chance to think about
and refine the interface. This is especially true when using test-first design. You have to think about the
interface from the view of a real class that will be using it because you begin with a real test class
that uses it.

To test unusual, unlikely, and exceptional situations


You can easily create a mock that will return values that don't usually occur, or that will throw
exceptions on demand. This allows you to easily test exception handling. For example, you can mock a
Fil e Outpu t S tr eam and have it throw an IOE x c ept i on upon writing the 10th byte. In general, you
can write a mock to replicate any situation that might be hard (or even impossible) to arrange to
happen for the purposes of a test.

Amazon

Prev

don't be afraid of buying books

Next

WOULDN'T IT BE NICE?
Wouldn't it be nice
if the mock could be told what method calls to expect from the object(s) being tested? Then you could
test the object behavior from the other side as well: not just the response to a call (i.e., the return
value and state change), but also what methods in the mock were called from the class being developed.

Wouldn't it be nice
if the mock could be told what value(s) to return from those calls? That would let you verify more
complex interactions.

Wouldn't it be nice
if it could be told to return different values each time a method is called? Again, this would let you test
even more complex interactions.

Wouldn't it be nice
if you could ask it to verify that everything that was expected to happen did, indeed, happen, and to
throw an exception if any expectation was violated? Some conditions could be detected immediately
(such as a method being called too many times, or with the wrong arguments), but some (such as a
method not being called enough times) couldn't until the mock was asked to check what really happened
against what was expected.

Wouldn't it be nice
if the mock threw an Exception as soon as it knew it was being used in a way that wasn't desired? That
would report the failure as close to the point of failure as possible, making it easier and faster to find
and fix the problem.

Wouldn't it be nice
if you had tools that made this easy and painless?

Wouldn't it be nice
if you could do all that? Well, (as you might have guessed), you can indeed do all of that with the tools
we'll be talking about in the remainder of this chapter.

Amazon

Prev

don't be afraid of buying books

Next

ACOMMON EXAMPLE
In order to compare the different tools that we will explore in the remainder of this chapter we will adopt a
common example that will be used in each section. The example we will use is derived from code in the
MockObjects distribution. Specifically, we'll be using parts of the calculator example in the package
c om . mo c ko bjec t s. e xa m pl e s.ca lcs erv er.
We will assume that the calculation engine has been developed and implements the following interface:
p ub l ic in terf a ce In t Ca l cula tor {
in t c alcu l at e (i n t v alue 1, int value2, String operation) throws CalculatorExcept i o n ;
}

The code we want to write takes an XML representation of a calculation and evaluates it using a concrete
C al c ul a to r. The initial test case will pass the following XML string to an instance of XmlCalculator and verify
the result (i.e., 2 ):

< ca l cu l at e op e ra t io n =" / add"


>
< ar g um e nt
>1
< /a r gu m en t
>
< ar g um e nt
>1
< /a r gu m en t
>
< /c a lc u la te
>

There would, of course, need to be various other tests but this will suffce for the purpose of this chapter.

Amazon

Prev

don't be afraid of buying books

Next

THE MOCKOBJECTS FRAMEWORK


The MockObjects project[URL 23] is a generic framework whose goal is to facilitate developing programmer
tests in the mock object style. Figure 7.4 shows a high-level view of the core of the framework. As we did
with JUnit (see Chapter 4), we will look at each of these classes in turn, then show how to use them to
create mocks.
Verifiable provides an interface for classes that can be asked at the end of a test to
confirm that the correct behavior occurred. As shown in the diagram, it is the root of the
core of the framework.

MockObject is extended by mocks created with the framework. Its purpose is primarily to
provide some convenience methods.

ReturnObjectList is used to contain an ordered sequence of objects that can be


sequenced through. When the next object is requested, an assertion error will be thrown if
there are no more objects. Retur n O b j e c t L i s t implements V e r i f i a b l e and its
ve rif y () method throws an assertion error if there are any objects remaining.
This class can be used to manage a list of return values that are to be returned by
successive calls to a given method in a mock.

Expectation is another interface that extends V e r i f i a b l e . Its purpose is to provide an


interface for classes that have the notion of expecting certain things to happen. The
ve rif y () method of an Ex pect a t i o n will check that the expectation was fulfilled. The
following classes are concrete implementations that embody specific types of expectations.

AbstractExpectation provides a superclass for specific kinds of expectations. Expectations


can be named. There is also support for choosing when failure due to expectation violation
occurs: either as soon as the actual situation is known (this is the default behavior) or
when it is asked to verify.

ExpectationValue is used to set up a value expectation. When created, you set an


expected value, and later (in response to calls by the class being tested) the actual value is
set. Verification consists of checking for equality (using a s s e r t E q u a l s ( ) )of the expected
and actual values.
This class is often used to capture expectations of argument values.

ExpectationDoubleValue is similar in purpose to E x p e c t a t i o n V a l u e , except that it


works with do u ble values. The notable difference is that s e t E x p e c t e d ( ) takes an
additional doub l e argument that specifies the acceptable error when comparing the

expected and actual doub le values.

ExpectationSegment is used for substring expectations. That is, you expect that the
actual value contains a certain string.
Verification for this expectation checks that the expected string is a substring of the actual
string.

ExpectationCounter lets you set up an expectation that something (e.g., a method call)
should happen a specific number of times. You set it up with the expected count, increment
it as required, and verify that the count was, in fact, as expected. If this expectation is set
to fail immediately upon violation of the expectation, it will fail when incremented past the
expected value.

ExpectationCollection provides an interface for classes that support multiple expectedactual value pairs.
Methods are defined to add expected and actual values, singly and in multiples as arrays,
enumerations, and iterators.

AbstractExpectationCollection provides default implementations for the required methods


(from the implemented interfaces). It also provides abstract accessors for the collections of
expected and actual values.

ExpectationList is an implementation of A b s t r a c t E x p e c t a t i o n C o l l e c t i o n that uses


two instances of Arr a yLis t for storing expected and actual values. It is used when the
order of the pairs is relevant.

ExpectationSet is another implementation of A b s t r a c t E x p e c t a t i o n C o l l e c t i o n that


uses two instances of Has hSet for storing expected and actual values. It is used when the
order of the pairs is irrelevant.

ExpectationMap is used to manage expectations involving named or indexed values. It


uses an E xp ect a tion S et to manage the expected keys.

Figure 7.4. High-level class diagram of

c o mm .. mm oo cc kk oo bb jj ee cc tt ss .

[View full size image]

In addition to the mock framework, MockObject provides an extension to JUnit's A s s e r t and T e s t C a s e


classes. These were discussed in Chapter 5.
When you use mocks in your tests you will notice yourself working in a common pattern:
create the mocks
set state and expectations
execute the code you are test-driving using the mocks
have the mocks verify that expectations were met
If we look ahead at the example we can see this pattern. First, we create the mock:
M oc k C a lc ul ato r moc k Calc u lato r = n e w M o c k C a l c u l a t o r ( ) ;

Then we set expectations and state:


m oc k C a lc ul ato r. set E xpec t edCa lcu l a t i o n ( 1 , 1 , " / a d d " ) ;
m oc k C a lc ul ato r. set u pRes u lt(2 );

Then we execute our code using the mock (details omitted):


X ml C a l cu la tor x mlC a lcul a tor = n e w X m l C a l c u l a t o r ( m o c k C a l c u l a t o r ) ;
a ss e r t Eq ua ls( "B ad s impl e add re s u l t " , 2 ,

x ml C a l cu la tor .c alc u late ( xmlS tri n g ) ) ;

And finally have the mock verify its expectations:


m oc k C a lc ul ato r. ver i fy() ;

Let's return to our example. We need to use the MockObjects framework to write a mock for the calculator
engine. Here it is, as it appears copied exactly from the examples package of the MockObjects distribution:
p ac k a g e co m.s ao rsa . tddb o ok.s amp l e s . m o c k o b j e c t s ;
i mp o r t c om .mo ck obj e cts. E xpec tat i o n V a l u e ;
i mp o r t c om .mo ck obj e cts. M ockO bje c t ;
p ub l i c c la ss Mo ckC a lcul a tor ext e n d s M o c k O b j e c t i m p l e m e n t s I n t C a l c u l a t o r
{
p r i v at e int m yRe s ult;
p r i v at e Exp ec tat i onVa l ue m yVa l u e 1 = n e w E x p e c t a t i o n V a l u e ( " v a l u e 1 " ) ;
p r i v at e Exp ec tat i onVa l ue m yVa l u e 2 = n e w E x p e c t a t i o n V a l u e ( " v a l u e 2 " ) ;
p r i v at e Exp ec tat i onVa l ue m yOp e r a t i o n = n e w E x p e c t a t i o n V a l u e ( " o p e r a t i o n " ) ;
p u b l ic M ock Ca lcu l ator ( ) {
s u pe r( );
}

p u b l ic i nt ca lcu l ate( i nt v alu e 1 , i n t v a l u e 2 , S t r i n g o p e r a t i o n )


t hro ws Ca l cula t orEx cep t i o n {
m y Va lu e1. se tAc t ual( v alue 1); m y V a l u e 2 . s e t A c t u a l ( v a l u e 2 ) ;
m y Op er ati on .se t Actu a l(op era t i o n ) ;
r e tu rn my Re sul t ;
}
p u b l ic v oid s etE x pect e dCal cul a t i o n ( i n t v a l u e 1 , i n t v a l u e 2 , S t r i n g o p e r a t i o n ) {
m y Va lu e1. se tEx p ecte d (val ue1 ) ;
m y Va lu e2. se tEx p ecte d (val ue2 ) ;
m y Op er ati on .se t Expe c ted( ope r a t i o n ) ;
}
p u b l ic v oid s etu p Resu l t(in t r e s u l t ) {
m y Re su lt = res u lt;
}

p u b l ic v oid v eri f y() {


m y Va lu e1. ve rif y ();
m y Va lu e2. ve rif y ();
m y Op er ati on .ve r ify( ) ;
}

This mock used simple Expe c tati onVal u e instances to manage the pairs of expected and actual values.
As such, it only supports a single call of the c a l c u l a t e ( ) method.[3]
[3] When we look at MockMaker in the next section we'll see a mock that can support multiple calls
using the Expe c tati o nCou nter , R e t u r n V a l u e s , and E x p e c t a t i o n L i s t classes.

Notice the general form:


create an Exp e ctat i onVa lue for each parameter of the c a l c u l a t e ( ) method

have a variable to hold the return value


have methods to set parameter expectations and return value
have an implementation of the interface's method to set actual values and return the preset return
value
have a v e rify ( ) method that has each E x p e c t a t i o n verify itself
Now we can write our test case using this mock to drive the development of X m l C a l c u l a t o r :
p ac k a g e co m.s ao rsa . tddb o ok.s amp l e s . m o c k o b j e c t s ;
i mp o r t j un it. fr ame w ork. T estC ase ;
p ub l i c c la ss Te stX m lCal c ulat or e x t e n d s T e s t C a s e {
p u b l ic v oid t est S impl e Add( ) {
M o ck Ca lcu la tor mock C alcu lat o r = n e w M o c k C a l c u l a t o r ( ) ;
m o ck Ca lcu la tor . setE x pect edC a l c u l a t i o n ( 1 , 1 , " / a d d " ) ;
m o ck Ca lcu la tor . setu p Resu lt( 2 ) ;

X m lC al cul at or x mlCa l cula tor = n e w X m l C a l c u l a t o r ( m o c k C a l c u l a t o r ) ;


S t ri ng xm lS tri n g = " <cal cul a t e o p e r a t i o n = \ " / a d d \ " > " +
" <ar gum e n t > 1 < / a r g u m e n t > " +
" <ar gum e n t > 1 < / a r g u m e n t > " +
" </ca lcu l a t e > " ;

a s se rt Equ al s(" B ad s i mple ad d r e s u l t " , 2 , x m l C a l c u l a t o r . c a l c u l a t e ( x m l S t r i n g ) ) ;


m o ck Ca lcu la tor . veri f y();

Amazon

Prev

don't be afraid of buying books

Next

MOCKMAKER
MockMaker[URL 24] is a tool for creating mock classes from a given interface or class. MockMaker builds mock
classes using the MockObjects framework.
Building mocks by hand using MockObjects is certainly better and easier than building them from scratch each
time. It's still a fair bit of work. After you've built a few that way, you'll notice that they all look very much
alike. They let a method be called some number of times, with specific parameters each time, returning
specific values each time. The same folks who created the MockObjects framework have written a tool that
automates this drudgery: MockMaker.
MockMaker takes an interface or class and creates a mock for it. We will take the example shown earlier and
use MockMaker to create the mock. To keep things simple, we'll use MockMaker from the command line, as
so:
$
>
>
>
>

j ava \
- clas sp at h b i n:/ u sr/ l oc al /ja va /l ib /M o ck M ak er . ja r \
m ockm ak er .Mo c kMa k er \
c om.s ao rs a.t d dbo o k.s a mp le s.m oc ko bj ec t s. I nt Ca l cu l a to r \
> src/ co m/ sao r sa/ t ddb o ok /s amp le s/ mo ck o bj e ct s/ M oc k I nt C a lc u l at o r .j a v a

This command results in the following in M oc k In t Ca lc u la t o r. j a va : [4]


[4] After adding a package statement, shortening the expectation names, and reformatting to make it fit on
the page better.

pac kage c om .sa o rsa . tdd b oo k. sam pl es .m oc k ob j ec ts ;


imp ort
imp ort
imp ort
imp ort

mo ck mak e r.R e tur n Va lu es;


co m. moc k obj e cts . *;
co m. sao r sa. t ddb o ok .s amp le s. mo ck o bj e ct s. I nt C a lc u l at o r ;
co m. sao r sa. t ddb o ok .s amp le s. mo ck o bj e ct s. C al c u la t o rE x c ep t i on ;

pub lic cl as s M o ckI n tCa l cu la tor i mp le me n ts In tC a lc u l at o r {

pri vate E xp ect a tio n Cou n te r myC al cu la te C al l s =


n ew Ex p ect a tio n Co un ter (" Ca lc ul a te C al ls " );
pri vate R et urn V alu e s m y Ac tu alC al cu la te R et u rn Va l ue s =
n ew Re t urn V alu e s( fa lse );
pri vate E xp ect a tio n Lis t m yC alc ul at eP ar a me t er 0V a lu e s =
n ew Ex p ect a tio n Li st ("C al cu la te P ar a me te r 0V a l ue s " );
pri vate E xp ect a tio n Lis t m yC alc ul at eP ar a me t er 1V a lu e s =
n ew Ex p ect a tio n Li st ("C al cu la te P ar a me te r 1V a l ue s " );
pri vate E xp ect a tio n Lis t m yC alc ul at eP ar a me t er 2V a lu e s =
n ew Ex p ect a tio n Li st ("C al cu la te P ar a me te r 2V a l ue s " );
pub lic vo id se t Exp e cte d Ca lc ula te Ca ll s( i nt ca ll s )
{
m yCal cu la teC a lls . set E xp ec ted (c al ls );
}
pub lic vo id ad d Exp e cte d Ca lc ula te Va lu es ( in t a rg 0 , i n t a r g1 , St r i ng a rg 2 ) {
m yCal cu la teP a ram e ter 0 Va lu es. ad dE xp ec t ed ( ne w I nt e g er ( a rg 0 ) );
m yCal cu la teP a ram e ter 1 Va lu es. ad dE xp ec t ed ( ne w I nt e g er ( a rg 1 ) );
m yCal cu la teP a ram e ter 2 Va lu es. ad dE xp ec t ed ( ar g2 ) ;
}

p ubli c in t c a lcu l ate ( in t arg 0, i nt a r g1 , S tr i ng a rg 2 ) t h r ow s Ca l c ul a t or E x ce p t io n


m yCal cu la teC a lls . inc ( );
m yCal cu la teP a ram e ter 0 Va lu es. ad dA ct ua l (n e w In t eg e r (a r g 0) ) ;
m yCal cu la teP a ram e ter 1 Va lu es. ad dA ct ua l (n e w In t eg e r (a r g 1) ) ;
m yCal cu la teP a ram e ter 2 Va lu es. ad dA ct ua l (a r g2 );
O bjec t ne xtR e tur n Val u e = myA ct ua lC al c ul a te Re t ur n V al u e s. g e tN e x t( ) ;
r etur n (( Int e ger ) ne x tR et urn Va lu e) .i n tV a lu e( ) ;

pub lic vo id se t upC a lcu l at e( int a rg )


{
m yAct ua lC alc u lat e Ret u rn Va lue s. ad d( ne w I n te ge r (a r g )) ;
}
pub lic vo id ve r ify ( )
{
m yCal cu la teC a lls . ver i fy () ;
m yCal cu la teP a ram e ter 0 Va lu es. ve ri fy () ;
m yCal cu la teP a ram e ter 1 Va lu es. ve ri fy () ;
m yCal cu la teP a ram e ter 2 Va lu es. ve ri fy () ;
}
}

Notice the differences between this and the mock in the previous section. This one supports an arbitrary
number of method calls rather than a single call. This is more complex and uses a different approach to
manage expectations about parameters and return values. Specifically:
create an Ex pec t ati o nC ou nt er to count the number of method calls
create an Ex pec t ati o nL is t for each parameter
create a R e tur n Val ue s to hold preset return values
As before, we need methods to set expected parameter values and return values. An additional
method is required to set the expected number of calls.
We have a similar implementation of the interface's method to set the actuals and return an
appropriate value.
Also, we have a very similar ve ri fy () method.
Using this generated mock, our test would be written as so:
pac kage c om .sa o rsa . tdd b oo k. sam pl es .m oc k ob j ec ts ;
imp ort ju ni t.f r ame w ork . Te st Cas e;
pub lic cl as s T e stM o ckM a ke rX mlC al cu la to r e x te nd s T e s tC a s e {

p ubli c vo id t est S imp l eA dd () th ro ws C a lc u la to r Ex c e pt i o n {


Moc kI nt Cal c ula t or m oc kC alc ul at or = ne w M oc k In t C al c u la t o r( ) ;
moc kC al cul a tor . add E xp ec ted Ca lc ul at e Va l ue s( 1 , 1 , " / a dd " ) ;
moc kC al cul a tor . set u pC al cul at e( 2) ;
moc kC al cul a tor . set E xp ec ted Ca lc ul at e Ca l ls (1 ) ;
Xml Ca lc ula t or x mlC a lc ul ato r = ne w X ml C al cu l at o r (m o c kC a l cu l a to r ) ;
Str in g xml S tri n g = "< ca lcu la te o pe r at i on =\ " /a d d \" > "

+ " <a rgu me nt >1 </ a rg u me nt > "


+ " <a rgu me nt >1 </ a rg u me nt > "
+ "< /c alc ul at e> ";

ass er tE qua l s(" B ad s im pl e a dd r es ul t ", 2 ,x ml C al c u la t o r. c a lc u l at e ( xm l S tr i n g) ) ;


moc kC al cul a tor . ver i fy () ;

Since MockMaker generates a fairly general mock we can write more involved tests such as:
pub lic vo id te s tCo m ple x Ad d( ) t hr ow s Ca l cu l at or E xc e p ti o n {
M ockI nt Ca lcu l ato r mo c kC al cul at or = n e w M oc kI n tC a l cu l a to r ( );
m ockC al cu lat o r.a d dEx p ec te dCa lc ul at eV a lu e s( 1, 1, " /a d d ") ;
m ockC al cu lat o r.s e tup C al cu lat e( 2) ;
m ockC al cu lat o r.a d dEx p ec te dCa lc ul at eV a lu e s( 2, 2, " /a d d ") ;
m ockC al cu lat o r.s e tup C al cu lat e( 4) ;
m ockC al cu lat o r.s e tEx p ec te dCa lc ul at eC a ll s (2 );

X mlCa lc ul ato r xm l Cal c ul at or = ne w Xm l Ca l cu la t or ( m oc k C al c u la t o r) ;


S trin g xm lSt r ing = " < ca lc ula te o pe ra t io n =\ "/ a dd \ " >"
+ " <a rg ume nt >"
+ "
< ca lcu la te o pe r at i on =\ " /a d d \" > "
+ "
<a rgu me nt >1 </ a rg u me nt > "
+ "
<a rgu me nt >1 </ a rg u me nt > "
+ "
< /c alc ul at e> "
+ " </ ar gum en t> "
+ " <a rg ume nt >2 </ ar g um e nt >"
+ " < /c al cul at e> ";
a sser tE qu als ( "Ba d ne s te d add r es ul t" , 4, x ml Ca l cu l a to r . ca l c ul a t e( x m lS t r in g ) );
m ockC al cu lat o r.v e rif y () ;

One potential drawback of generating mocks by hand or with MockMaker is that you have another class that
has to be managed. If the interface that is being mocked changes, the mock has to be changed or
regenerated. The classes have to be stored, version controlled, packaged, etc. Next, we'll look at a
MockObjects-based framework that gets around all of this in exchange for a few cycles at runtime.

Amazon

Prev

don't be afraid of buying books

Next

EASYMOCK
EasyMock [URL 25] is a framework for creating mock objects dynamically at runtime. It was written and
is maintained by Tammo Freese, who presented it in a paper at XP2002[18].
Using EasyMock does, indeed, make things easier. We take the following test case from the MockMaker
example in the previous section and rewrite it to use EasyMock. Then we'll look at how EasyMock is used
in the example as well as some of its other capabilities.
p ub l i c c la ss T e stE a syMoc kXml Ca l c u l a t o r e x t e n d s T e s t C a s e {
p r i vat e Mock C ont r ol co ntro l;
p r i vat e IntC a lcu l ator mock Ca l c u l a t o r ;
p r o tec te d vo i d s e tUp() {
co ntro l = E asyMo ck.c on t r o l F o r ( I n t C a l c u l a t o r . c l a s s ) ;
mo ckCa l cul a tor = (In tC a l c u l a t o r ) c o n t r o l . g e t M o c k ( ) ;
}

p u b lic v oid t est S imple Add( ) t h r o w s C a l c u l a t o r E x c e p t i o n

m ock Ca lcul a tor . calcu late (1 , 1 , " / a d d " ) ;


c ont ro l.se t Ret u rnVal ue(2 );
c ont ro l.ac t iva t e();
X mlC al cula t or x mlCal cula to r = n e w X m l C a l c u l a t o r ( m o c k C a l c u l a t o r ) ;
S tri ng xml S tri n g = " <cal cu l a t e o p e r a t i o n = \ " / a d d \ " > "
+ " <ar gu m e n t > 1 < / a r g u m e n t > "
+ " <ar gu m e n t > 1 < / a r g u m e n t > "
+ " </ca lc u l a t e > " ;
a sse rt Equa l s(" B ad si mple a d d r e s u l t " , 2 , x m l C a l c u l a t o r . c a l c u l a t e ( x m l S t r i n g ) ) ;
c ont ro l.ve r ify ( );

p u b lic v oid t est C omple xAdd () t h r o w s C a l c u l a t o r E x c e p t i o n {


m ock Ca lcul a tor . calcu late (1 , 1 , " / a d d " ) ;
c ont ro l.se t Ret u rnVal ue(2 );
m ock Ca lcul a tor . calcu late (2 , 2 , " / a d d " ) ;
c ont ro l.se t Ret u rnVal ue(4 );
c ont ro l.ac t iva t e();

X mlC al cula t or x mlCal cula to r = n e w X m l C a l c u l a t o r ( m o c k C a l c u l a t o r ) ;


S tri ng xml S tri n g = " <cal cu l a t e o p e r a t i o n = \ " / a d d \ " > "
+ " <ar gu m e n t > "
+ "
<c al c u l a t e o p e r a t i o n = \ " / a d d \ " > "
+ "
< ar g u m e n t > 1 < / a r g u m e n t > "
+ "
< ar g u m e n t > 1 < / a r g u m e n t > "
+ "
</ ca l c u l a t e > "
+ " </a rg u m e n t > "
+ " <ar gu m e n t > 2 < / a r g u m e n t > "
+ " </ca lc u l a t e > " ;
a sse rt Equa l s(" B ad ne sted a d d r e s u l t " , 4 , x m l C a l c u l a t o r . c a l c u l a t e ( x m l S t r i n g ) ) ;
c ont ro l.ve r ify ( );

Basic Features
When creating a mock with EasyMock, you start by creating an instance of M o c k C o n t r o l for the
interface you want to mock. This is done with a call to a static factory method in the E a s y M o c k class:
M oc k C ont ro l co n tro l = Ea syMo ck . c o n t r o l F o r ( I n t C a l c u l a t o r . c l a s s ) ;

The next step is to ask the control for a mock:


I nt C a lcu la tor m ock C alcul ator = ( I n t C a l c u l a t o r ) c o n t r o l . g e t M o c k ( ) ;

Now we can specify our expectations by using the newly created mock and the control. You set method
call expectations by making the expected method call on the mock, with the expected arguments. For
example:
m oc k C alc ul ator . cal c ulate (1, 1, " / a d d " ) ;

We can specify the result to be returned from this method call by using the control:
c on t r ol. se tRet u rnV a lue(2 );

That's pretty much it. We continue this until our expectations have been specified. Then we make one
more call to the control to switch the mock from specification mode to usage mode:
c on t r ol. ac tiva t e() ;

We then use the mock in our test as usual. At the end we ask the control to verify:
c on t r ol. ve rify ( );

Remember that we need to ask the control to verify the expectations,


not the mock. This is a significant difference from working with handcoded or MockMaker-generated mocks. Be sure to keep it in mind,
especially if you're working with a mixture of the two.

That's the basic idea. There are some additional capabilities we can take advantage of. If we set an
expectation that a v o id method is to be called, then we can skip the step of setting the return value
and it will be assumed to be void. If, however, we prefer to make that explicit, we can do so with the
following instead of calling s etRe turnV a l u e ( ) :
c on t r ol. se tVoi d Cal l able( );

Advanced Features
So far, all of the expectations we've looked at simply specify the methods to be called, the expected
arguments, and the value to return (or none if it's v o i d ). There are no expectations about how many
times the method should be called. The only thing we set an expectation for is that it will be called. We
can set an expectation about how many times a method is to be called by passing that number to the
return-value-setting method of the control. For example, if we expected c a l c u l a t e ( 1 , 1 , " / a d d " ) to
be called three times, returning 2 each time, we could use:
c on t r ol. se tRet u rnV a lue(2 , 3) ;

The general form for a method that returns something is:


s et R e tur nV alue (
< ty p e
> v a l ue, i nt c o unt )

For a void method (i.e., no return value), we can use:


c on t r ol. se tVoi d Cal l able( int co u n t )

If more than the expected number of calls is made, an exception will be thrown on the first call that
exceeds the expectation. If fewer than the expected number of calls are made, an exception will be
thrown by the control when it is asked to verify the expectations. The following two examples show this.
First, an example of setting an expectation for two calls, and making three.
p ub l i c v oi d te s tTo o ManyC alls () t h r o w s C a l c u l a t o r E x c e p t i o n {
M o c kCo nt rol c ont r ol = Easy Mo c k . c o n t r o l F o r ( I n t C a l c u l a t o r . c l a s s ) ;
I n t Cal cu lato r mo c kCalc ulat or = ( I n t C a l c u l a t o r ) c o n t r o l . g e t M o c k ( ) ;
m o c kCa lc ulat o r.c a lcula te(1 , 1 , " / a d d " ) ;
c o n tro l. setR e tur n Value (2, 2) ;
c o n tro l. acti v ate ( );
m o c kCa lc ulat o r.c a lcula te(1 , 1 , " / a d d " ) ;
m o c kCa lc ulat o r.c a lcula te(1 , 1 , " / a d d " ) ;
m o c kCa lc ulat o r.c a lcula te(1 , 1 , " / a d d " ) ;
}

c o n tro l. veri f y() ;

We get the following exception trace on the third call to c a l c u l a t e ( ) (trimmed for clarity and space):
j un i t .fr am ewor k .As s ertio nFai le d E r r o r :
E a s yMo ck for int e rface
c om . s aor sa .tdd b ook . sampl es.m oc k o b j e c t s . I n t C a l c u l a t o r :
m et h o d c al l ca l cul a te(1, 1, "/ a d d " ) : c a l l s e x p e c t e d : 2 , r e c e i v e d : 3
a t org .e asym o ck. A bstra ctMo ck C o n t r o l . i n v o k e ( A b s t r a c t M o c k C o n t r o l . j a v a : 5 5 )
a t $Pr ox y0.c a lcu l ate(U nkno wn S o u r c e )
a t com .s aors a .td d book. samp le s . m o c k o b j e c t s . T e s t E a s y M o c k X m l C a l c u l a t o r
.te st TooM a nyC a lls(T estE as y M o c k X m l C a l c u l a t o r . j a v a : 7 8 )

The second example is of setting the same expectation (two calls) and only making one.
p ub l i c v oi d te s tTo o ManyC alls () t h r o w s C a l c u l a t o r E x c e p t i o n {
M o c kCo nt rol c ont r ol = Easy Mo c k . c o n t r o l F o r ( I n t C a l c u l a t o r . c l a s s ) ;
I n t Cal cu lato r mo c kCalc ulat or = ( I n t C a l c u l a t o r ) c o n t r o l . g e t M o c k ( ) ;
m o c kCa lc ulat o r.c a lcula te(1 , 1 , " / a d d " ) ;
c o n tro l. setR e tur n Value (2, 2) ;
c o n tro l. acti v ate ( );
m o c kCa lc ulat o r.c a lcula te(1 , 1 , " / a d d " ) ;
}

c o n tro l. veri f y() ;

In this case we don't get the expectation until v e r i f y ( ) is called:


j un i t .fr am ewor k .As s ertio nFai le d E r r o r :
E asy Mo ck f o r i n terfa ce
c om. sa orsa . tdd b ook.s ampl es . m o c k o b j e c t s . I n t C a l c u l a t o r :
Ex pe ctat i on f ailur e on v e r i f y :

Ex pe ctat i on f ailur e on v e r i f y :
m et h o d c al l ca l cul a te(1, 1, "/ a d d " ) : c a l l s e x p e c t e d : 2 , r e c e i v e d : 1
a t org .e asym o ck. A bstra ctMo ck C o n t r o l . v e r i f y ( A b s t r a c t M o c k C o n t r o l . j a v a : 4 5 )
a t com .s aors a .td d book. samp le s . m o c k o b j e c t s . T e s t E a s y M o c k X m l C a l c u l a t o r
.te st NotE n oug h Calls (Tes tE a s y M o c k X m l C a l c u l a t o r . j a v a : 1 1 4 )

Sometimes we will want a mock method to throw an exception when called rather than return a value.
To do this, use setT h rowa b le() rather than s e t V o i d C a l l a b l e ( ) or s e t R e t u r n V a l u e ( ) .Here's an
example:
c on t r ol. se tThr o wab l e(new Run ti m e E x c e p t i o n ( ) ) ;

Note that you can specify the number of expected calls as with s e t V o i d C a l l a b l e ( ) and
s et R e tur nV alue ( ).
One nice feature of EasyMock is that after making the method call to set the expected arguments, you
can make repeated calls to setV oidCa l l a b l e ( ) , s e t R e t u r n V a l u e ( ) , and s e t T h r o w a b l e ( ) (with
the count parameter) to specify behavior for each subsequent call. I don't have a good example from our
Calculator code, but here's one from the EasyMock documentation:
m oc k R epo si tory . get T ext(" Text ") ;
c on t r ol. se tRet u rnV a lue(" The te x t y o u w a n t e d " , 2 ) ;
c on t r ol. se tThr o wab l e(new Run ti m e E x c e p t i o n ( ) , 3 ) ;
c on t r ol. se tRet u rnV a lue(" The te x t y o u r e a l l y w a n t e d " ) ;

Throwing an A s sert i onFa i ledE rror is the default behavior formethods in the mocked interface for
which you haven't set an expectation, or methods called with arguments other than those set in an
expectation. You can change the default for methods called with arguments other than those specified by
using one of the following, after the expectation-setting call to the mock:

< di v >
[ Vi e w fu ll wid t h]< / div>
c on t r ol. se tDef a ult R eturn Valu e(
< ty p e
> t o B eRe tu rned ) ; c o ntrol .set De f a u l t T h r o w a b l e ( T h r o w a b l e
to B e Thr ow n); c ont r ol.se tDef au l t V o i d C a l l a b l e ( )

To change the default behavior for methods for which you set no expectation, you can use a nice control
for which the default behavior is to return the appropriate null value (i.e., 0 , n u l l , or f a l s e ). We get a
nice control by using the following factory method in EasyMock:
M oc k C ont ro l co n tro l = Ea syMo ck . n i c e C o n t r o l F o r ( A n I n t e r f a c e ) ;

Amazon

Prev

don't be afraid of buying books

Next

EasyMock Summary
One very important advantage of EasyMock is that the mock is specified in the test. This means that the
mock is tightly bound to the test it serves.
There are some downsides to using EasyMock, though:
1. There's a performance hit that you take at runtime. It takes some time to build the mock on the
fly. As you specify the mock, you not only specify what methods are mocked, but also the
expectations. This rolls two steps into one. This makes the mocks easier to build and more
localized, but means that unique mocks need to be built for each test, since each test will have
different expectations to verify.
2. You are limited to the mocks as EasyMock creates them. When you handcraft mocks or use
MockMaker you can tweak the mock as required.
3. Not being able to tune expectations may lead to over-specification in tests. There may be cases
where you only really want to test one parameter. If you are hand-coding you just write an
expectation-setting method that does just that. If you are using MockMaker you can tweak the
generated methods, or add what you need. With EasyMock you are stuck with expectations fully
specifying exactly the methods in the interface being mocked.
4. EasyMock-generated mocks are passive: you specify expected calls and provide return values.
There is no way to have the mock make calls into your objects.
This generally isn't a problem. When it is, use MockMaker or build it by hand.
5. You can only mock interfaces with EasyMock whereas MockMaker will generate mocks for classes
as well. This can be an issue if you are creating mocks for legacy code that wasn't designed in
an interface-centric way.
6. EasyMock only works with Java 1.3.1 or later due to its reliance on new classes in the
java .la ng .re fl ect package, specifically I n voc a ti o nH an d le r and P ro x y.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
Mock objects can, indeed, provide a way to achieve the three goals that were defined at the beginning
of this chapter, namely:
focused tests
independent tests, and
fast tests.
We looked at several reasons for using mocks, and the tradeoffs of four different mocking techniques:
coding them from scratch by hand, coding them by hand using the MockObjects framework, stubbing
them out using MockMaker, and finally auto-matically creating them (most easily, but with the biggest
performance hit and most constraints) using EasyMock.
Mock objects are a fairly young concept. New tools and techniques are continually being developed.
EasyMock is one of the recent developments in this area. It makes it fast, easy, and cheap to take full
advantage of mocks. There are no extra classes to create and maintain, and the mock-related code is
part of the test which uses it. We will use EasyMock extensively in our approach to test-driving user
interfaces later in this book.
There are still cases where you might want to build a mock class by hand (likely using the MockObjects
framework). Even then you can get a head start by using a tool like MockMaker to generate the skeletal
mock class for you. You can then add custom behavior.
I'll close this chapter with a slight warning: Mocks are great, and can be invaluable at times, but don't
overuse them or become overly reliant on them. They are but one tool in our bag of tricks, and we have
many others.

Stubs, Fakes, and Mocks


I've talked about Mocks in this chapter. Technically, there are three concepts that are
commonly referred to as mocks:
Stubs A class with methods that do nothing. They simply are there to allow
the system to compile and run.

Fakes A class with methods that return a fixed value or values that can
either be hardcoded or set programmatically.

Mocks A class in which you can set expectations regarding what methods
are calls, with what parameters, how often, etc. You can also set return
values for various calling situations. A mock will also provide a way to verify
that the expectations were met.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 8. DEVELOPING A GUI TEST-FIRST


Using TDD to develop the Graphical User Interface (GUI) of an application is tricky. Part of the problem
is that GUIs are, by definition, graphical. In this chapter we look at ways to test GUI components
programmatically, and ways to minimize what is required.
There are several approaches that can be taken when developing the GUI test-first. We will illustrate
them using the same approach we used in Chapter 7: define a common example and apply each
approach to it.

Amazon

Prev

don't be afraid of buying books

Next

THE EXAMPLE
The example we will be working with in this chapter will be the first bit of GUI for the project we'll be
working through later in the book. Specifically, we need a GUI to a movie list with the ability to add
new movies.
First, we'll brainstorm and decide what it should look like. We need the following components:
1. a list of movies
2. a text field in which to enter the name of a new movie
3. a button to indicate that we would like to add the movie we entered in the text field.
The result of this brainstorming is the sketch shown in Figure 8.1.

Figure 8.1. GUI sketch.

We're intentionally limiting ourselves to a very simple GUI example in this chapteronly what suffices to
explore techniques for developing GUIs test-first. In later chapters we will see these techniques applied
to the development of more elaborate interfaces.
For the purposes of the examples we will assume that this is the interface that we use to create a mock
to which the GUI class talks:

pub l ic in ter fa ce Mo vieL istE dito r {


V e ctor get Mo vie s( );
v o id ad d(S tr ing s trin g);
v o id de let e( int i );
}

We'll examine several alternative approaches, starting with the simpler ones and moving to the more
elegant, flexible ones. We'll end the survey by considering the alternative which we'll make use of later
in the book.

Amazon

Prev

don't be afraid of buying books

Next

THE AWT ROBOT


Though for our purposes it is unworkable, the AWT Robot approach does deserve mention, if for no other
reason than it's often mentioned when people talk about testing GUIs.
The Robo t is for programmatically feeding key and mouse events to an application. The problem is that
it deals with pixel locations, not components. This is at the wrong level for automated programmer tests.
Future changes to the production GUI code could too easily break our test code. R ob ot could be used
for automating customer tests, though. It could be used either programmatically or using a capture and
replay approach.
That's it for Ro bot .

Amazon

Prev

don't be afraid of buying books

Next

BRUTE FORCE
The first approach we will explore is to give our tests direct access to the visual components in the GUI.
We can do this in one of several ways:
1. make them public instance variables so that the test can access them directly
2. provide an accessor for each component so that the test can get hold of them
3. make the test class an inner class of the class that creates the GUI
4. use reflection to gain access to the components
All of these approaches have the drawback that the components cannot be local to a method; they must
be instance variables. Also, all but the last require extra production code that is used only for testing.
Since I learned Object Oriented Programming in a Smalltalk environment, I find the idea of making
instance variables public unthinkable. This rules out option one. Also, I like to keep my test code
separate from production code, so I avoid option three as well. The last option is very overhead
intensive to do manually. We'll explore some frameworks later in this chapter that make use of
reflection, but hide the details.
That leaves the second approach: adding accessors for the components.
Keep in mind that the code was developed incrementally: enough new test to fail, followed by enough
new code to pass. During development, both the code and the tests were refactored as needed. Notice
how the tests are in two Te s tCas e classes. Notice the different s e t U p ( ) methods.
As we discuss more fully in the JUnit chapter, TestCase is a
mechanism to group tests that rely on the same fixture, not tests that
happen to use the same class. When we need a different fixture, we
should create a new TestCase class. As you add tests, add them to the
TestCase that maintains the fixture that they require.

Given that preamble, here's the test code. First we have a T e s t C a s e that verifies that the required
components are present. Notice how we've used EasyMock. For this test we aren't concerned with the
interaction of the GUI with the underlying object (i.e., the mock) so we've used n i c e C o n t r o l F o r ( )
which provides default implementations for all of the methods in M o v i e L i s t E d i t o r .
p ub l ic c las s Tes t Widg e ts e xten d s T e s t C a s e {
p r iv at e M o ckCo n trol cont rol;
p r iv at e M o vieL i stEd i tor mock E d i t o r ;
p r iv at e M o vieL i stWi n dow wind o w ;
p r ot ec ted void setU p () {
co nt rol = Ea s yMoc k .nic eCon t r o l F o r ( M o v i e L i s t E d i t o r . c l a s s ) ;
mo ck Edi t or = (Mov i eLis tEdi t o r ) c o n t r o l . g e t M o c k ( ) ;
co nt rol . acti v ate( ) ;
wi nd ow = new Movi e List Wind o w ( m o c k E d i t o r ) ;
wi nd ow. i nit( ) ;
wi nd ow. s how( ) ;
}
p u bl ic vo i d te s tLis t () {
JL is t m o vieL i st = wind ow.g e t M o v i e L i s t ( ) ;
as se rtN o tNul l ("Mo v ie l ist s h o u l d b e n o n n u l l " , m o v i e L i s t ) ;
as se rtT r ue(" M ovie list sho u l d b e s h o w i n g " , m o v i e L i s t . i s S h o w i n g ( ) ) ;
}
p u bl ic vo i d te s tFie l d() {
JT ex tFi e ld m o vieF i eld = wi n d o w . g e t M o v i e F i e l d ( ) ;

JT ex tFi e ld m o vieF i eld = wi n d o w . g e t M o v i e F i e l d ( ) ;


as se rtN o tNul l ("Mo v ie f ield s h o u l d b e n o n n u l l " , m o v i e F i e l d ) ;
as se rtT r ue(" M ovie fiel d sh o u l d b e s h o w i n g " , m o v i e F i e l d . i s S h o w i n g ( ) ) ;

p u bl ic vo i d te s tAdd B utto n() {


JB ut ton addB u tton = wi ndow . g e t A d d B u t t o n ( ) ;
as se rtN o tNul l ("Ad d but ton s h o u l d b e n o n n u l l " , a d d B u t t o n ) ;
as se rtT r ue(" A dd b u tton sho u l d b e s h o w i n g " , a d d B u t t o n . i s S h o w i n g ( ) ) ;
as se rtE q uals ( "Add butt on s h o u l d b e l a b e l e d \ " A d d \ " " ,
"Add " ,
addB u tton .get T e x t ( ) ) ;
}

p u bl ic vo i d te s tDel e teBu tton ( ) {


JB ut ton dele t eBut t on = win d o w . g e t D e l e t e B u t t o n ( ) ;
as se rtN o tNul l ("De l ete butt o n s h o u l d b e n o n n u l l " , d e l e t e B u t t o n ) ;
as se rtT r ue(" D elet e but ton s h o u l d b e s h o w i n g " , d e l e t e B u t t o n . i s S h o w i n g ( ) ) ;
as se rtE q uals ( "Del e te b utto n s h o u l d b e l a b e l e d \ " D e l e t e \ " " ,
"Del e te",
dele t eBut ton. g e t T e x t ( ) ) ;
}

The other Te stCa s e tests for correct operation of the GUI and uses a more involved mock. Here we
build the mock in each test method, setting different method call expectations and return values in each.
Note, however, the common mock creation code in s e t U p ( ) .
p ub l ic c las s Tes t Oper a tion ext e n d s T e s t C a s e {
p r iv at e s t atic fina l Str ing L O S T I N S P A C E = " L o s t I n S p a c e " ;
p r iv at e V e ctor movi e Name s;
p r iv at e M o vieL i stWi n dow wind o w ;
p r iv at e M o ckCo n trol cont rol= n u l l ;
p r iv at e M o vieL i stEd i tor mock E d i t o r = n u l l ;
p r ot ec ted void setU p () {
mo vi eNa m es = new V ecto r() {
{ add ( "Sta r War s "); add( " S t a r T r e k " ) ; a d d ( " S t a r g a t e " ) ; }
};
wi nd ow = nul l ;

Mo ck Con t rol c ontr o l = Easy M o c k . c o n t r o l F o r ( M o v i e L i s t E d i t o r . c l a s s ) ;


Mo vi eLi s tEdi t or m o ckEd itor = ( M o v i e L i s t E d i t o r ) c o n t r o l . g e t M o c k ( ) ;

p u bl ic vo i d te s tMov i eLis t() {


mo ck Edi t or.g e tMov i es() ;
co nt rol . setR e turn V alue (mov i e N a m e s , 1 ) ;
co nt rol . acti v ate( ) ;
Mo vi eLi s tWin d ow w i ndow = n e w M o v i e L i s t W i n d o w ( m o c k E d i t o r ) ;
wi nd ow. i nit( ) ;
wi nd ow. s how( ) ;

J L is t mov i eLis t = w i ndow .get M o v i e L i s t ( ) ;


L i st Mo del movi e List M odel = m o v i e L i s t . g e t M o d e l ( ) ;
a s se rt Equ a ls(" M ovie list is t h e w r o n g s i z e " ,
m o vieN a mes. size ( ) ,
m o vieL i stMo del. g e t S i z e ( ) ) ;
f o r (i nt i = 0 ; i
< m o vi eN ame s .siz e (); i ++) {
as se rtE q uals ( "Mov i e li st c o n t a i n s b a d n a m e " ,
movi e Name s.ge t ( i ) ,
movi e List Mode l . g e t E l e m e n t A t ( i ) ) ;
}
c o nt ro l.v e rify ( );

}
p ub l ic v oid test A dd() {
V e ct or mo v ieNa m esWi t hAdd itio n = n e w V e c t o r ( m o v i e N a m e s ) ;
m o vi eN ame s With A ddit i on.a dd(L O S T I N S P A C E ) ;
m o ck Ed ito r .get M ovie s ();
c o nt ro l.s e tRet u rnVa l ue(m ovie N a m e s , 1 ) ;
m o ck Ed ito r .add ( LOST IN S PACE ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Ed ito r .get M ovie s ();
c o nt ro l.s e tRet u rnVa l ue(m ovie N a m e s W i t h A d d i t i o n , 1 ) ;
c o nt ro l.a c tiva t e();
M o vi eL ist W indo w win d ow = new M o v i e L i s t W i n d o w ( m o c k E d i t o r ) ;
w i nd ow .in i t();
w i nd ow .sh o w();
J T ex tF iel d mov i eFie l d = wind o w . g e t M o v i e F i e l d ( ) ;
m o vi eF iel d .set T ext( L OST IN S P A C E ) ;
J B ut to n a d dBut t on = wind ow.g e t A d d B u t t o n ( ) ;
a d dB ut ton . doCl i ck() ;
J L is t mov i eLis t = w i ndow .get M o v i e L i s t ( ) ;
L i st Mo del movi e List M odel = m o v i e L i s t . g e t M o d e l ( ) ;
a s se rt Equ a ls(" M ovie list is t h e w r o n g s i z e a f t e r a d d " ,
m o vieN a mesW ithA d d i t i o n . s i z e ( ) ,
m o vieL i stMo del. g e t S i z e ( ) ) ;
a s se rt Equ a ls(" M ovie list doe s n ' t c o n t a i n n e w n a m e " ,
L O ST I N SPA CE,
m o vieL i stMo del. g e t E l e m e n t A t ( m o v i e N a m e s . s i z e ( ) ) ) ;
}

c o nt ro l.v e rify ( );

p ub l ic v oid test D elet e () {


V e ct or mo v ieNa m esWi t hDel etio n = n e w V e c t o r ( m o v i e N a m e s ) ;
m o vi eN ame s With D elet i on.r emov e ( 1 ) ;

mo ck Edi t or.g e tMov i es() ;


co nt rol . setR e turn V alue (mov i e N a m e s , 1 ) ;
mo ck Edi t or.d e lete ( 1);
co nt rol . setV o idCa l labl e(1) ;
mo ck Edi t or.g e tMov i es() ;
co nt rol . setR e turn V alue (mov i e N a m e s W i t h D e l e t i o n , 1 ) ;
co nt rol . acti v ate( ) ;
Mo vi eLi s tWin d ow w i ndow = n e w M o v i e L i s t W i n d o w ( m o c k E d i t o r ) ;
wi nd ow. i nit( ) ;
wi nd ow. s how( ) ;
JL is t m o vieL i st = wind ow.g e t M o v i e L i s t ( ) ;
mo vi eLi s t.se t Sele c tedI ndex ( 1 ) ;
JB ut ton dele t eBut t on = win d o w . g e t D e l e t e B u t t o n ( ) ;
de le teB u tton . doCl i ck() ;
Li st Mod e l mo v ieLi s tMod el = m o v i e L i s t . g e t M o d e l ( ) ;
as se rtE q uals ( "Mov i e li st i s t h e w r o n g s i z e a f t e r d e l e t e " ,
movi e Name sWit h D e l e t i o n . s i z e ( ) ,
movi e List Mode l . g e t S i z e ( ) ) ;

co nt rol . veri f y();

The GUI class is below, with a screen shot of the resulting window shown in Figure 8.2.
p ub l ic c las s Mov i eLis t Wind ow e x t e n d s J F r a m e {
p r iv at e J L ist m ovie L ist;
p r iv at e J B utto n add B utto n;
p r iv at e M o vieL i stEd i tor myEd i t o r ;
p r iv at e J T extF i eld m ovie Fiel d ;
p r iv at e J B utto n del e teBu tton ;
p u bl ic Mo v ieLi s tWin d ow(M ovie L i s t E d i t o r a n E d i t o r ) {
su pe r() ;
my Ed ito r = a n Edit o r;
}
p u bl ic JL i st g e tMov i eLis t() {
re tu rn m ovie L ist;
}
p u bl ic JT e xtFi e ld g e tMov ieFi e l d ( ) {
re tu rn m ovie F ield ;
}
p u bl ic JB u tton getA d dBut ton( ) {
re tu rn a ddBu t ton;
}

p u bl ic JB u tton getD e lete Butt o n ( ) {


re tu rn d elet e Butt o n;
}
p u bl ic vo i d in i t() {
se tL ayo u t();
in it Mov i eLis t ();
in it Mov i eFie l d();
in it Add B utto n ();
in it Del e teBu t ton( ) ;
pa ck ();
}
p r iv at e v o id s e tLay o ut() {
ge tC ont e ntPa n e(). s etLa yout ( n e w F l o w L a y o u t ( ) ) ;
}
p r iv at e v o id i n itMo v ieLi st() {
mo vi eLi s t = n ew J L ist( getM o v i e s ( ) ) ;
JS cr oll P ane s crol l er = new J S c r o l l P a n e ( m o v i e L i s t ) ;
ge tC ont e ntPa n e(). a dd(s crol l e r ) ;
}
p r iv at e v o id i n itMo v ieFi eld( ) {
mo vi eFi e ld = new J Text Fiel d ( 1 6 ) ;
ge tC ont e ntPa n e(). a dd(m ovie F i e l d ) ;
}
p r iv at e v o id i n itAd d Butt on() {
ad dB utt o n = n ew J B utto n("A d d " ) ;
ad dB utt o n.ad d Acti o nLis tene r ( n e w A c t i o n L i s t e n e r ( ) {
pu bli c voi d act i onPe rfor m e d ( A c t i o n E v e n t e ) {
myE d itor . add( m ovie Fiel d . g e t T e x t ( ) ) ;
mov i eLis t .set L istD ata( g e t M o v i e s ( ) ) ;
}
}) ;
ge tC ont e ntPa n e(). a dd(a ddBu t t o n ) ;
}
p r iv at e v o id i n itDe l eteB utto n ( ) {
de le teB u tton = ne w JBu tton ( " D e l e t e " ) ;
de le teB u tton . addA c tion List e n e r ( n e w A c t i o n L i s t e n e r ( ) {
pu bli c voi d act i onPe rfor m e d ( A c t i o n E v e n t e ) {
myE d itor . dele t e(mo vieL i s t . g e t S e l e c t e d I n d e x ( ) ) ;
mov i eLis t .set L istD ata( g e t M o v i e s ( ) ) ;

}
}) ;
ge tC ont e ntPa n e(). a dd(d elet e B u t t o n ) ;

p r iv at e V e ctor getM o vies () {


Ve ct or m ovie s = m y Edit or.g e t M o v i e s ( ) ;
re tu rn ( movi e s == null ) ? n e w V e c t o r ( ) : m o v i e s ;
}

Figure 8.2. The resulting GUI.

Amazon

Prev

don't be afraid of buying books

Next

JFCUNIT
Now let's try a similar but more elegant solution that doesn't require us to explicitly make the GUI class
internals (components) accessible. JFCUnit also takes care of the threading issues involved with testing
Swing code that is driven by events that are processed by the AWT thread. It automatically blocks the
AWT thread while each test is run and unblocks it afterwards. As well, by calling a wt S le ep ( ),JFCUnit
gives you the ability to unblock the AWT thread within a test and allows it to process any events that
the test has placed in the queue. Once all events have been processed, the AWT thread is again blocked
and your test continues. One way or another, control is given back to the test (and the AWT thread is
blocked) after a specified period of time. You can set this period of time using the s e tS le e pT im e ()
method.
JFCUnit also provides a helper class named, aptly, J F C Tes t He l pe r. This class provides a variety of
methods you can use to find various parts of the GUI being tested, to track what windows are open, and
to clean up any open/showing windows.

The JFCUnit API


Let's have a look at the APIs for these classes before tackling the example. These are the two main
classes that provide you with the functionality required to test Swing GUIs. There are also an assortment
of support classes for encapsulating event data that we will use in conjunction with the methods below.

junit.extensions.jfcunit.JFCTestCase
Here's the API that you have access to by virtue of extending J FC T es t Ca se .
awtSleep() suspends the test, for no longer than the specified sleep time (the default or
that set by s et Slee pTim e()), allowing the AWT thread to run and process UI events
that are in its queue.

awtSleep(long sleep Time) is equivalent to calling s e tS le e p T im e( s le ep Ti me )


followed by awt Sl eep( ).This means that subsequent calls to a wt S le ep ( ) will use
slee pTi me as the maximum sleep time.

resetSleep Time() resets the a w tSl e e p() maximum sleep time to the default.

setSleep Time(long time) sets the maximum time (in milliseconds) for which
awtS lee p( ) will sleep.

sleep(long delay) suspends the test for at least de l ay milliseconds, allowing the AWT
thread to run.

junit.extensions.jfcunit.JFCTestHelper
We generally want to create an instance of JF C T est H e lp er in the J FC T es tC a se 's se tU p () method
since it will typically be needed in all tests. The major methods of J FC T es t H el p er are listed below.
Consult the JFCUnit documentation for details.
cleanUp() can be used in the t e arD o w n() method to close and dispose of any windows
left open when a test fails partway through.

disposeWindow() can be used to destroy a window.

enterClickAndLeave() generates appropriate events to click on a component.

enterDragAndLeave() generates appropriate events to drag and drop a component.

enterDropDownClickAndLeave() generates appropriate events to make a selection


from a combo box.

enterJListClickAndLeave() generates appropriate events to make a selection from a


list.

findComponent() finds a specific type of component.

findNamedComponent() finds a component with a specific name.

getMessageFromJDialog() gets the message displayed in a dialog box.

getShowingDialogs() gets all visible dialogs, using various selection criteria.

getShowingJFileChooser() finds a Fi l e Cho o s er being shown by a specific window.

getShowingJFileChoosers() finds all F ile C h oos e rs being shown by a specific


window.

getWindow() gets a window that is showing and has a specific title.

getWindows() gets all currently visible windows, with various selection criteria.

sendKeyAction() sends K eyPre s sed , K e y Typ e d, and K ey R el e as ed events to a


component.

sendString() sends a series of key events to a component to simulate the typing in of a


string.

Tackling the Example


Our test classes aren't too different with JFCUnit than without it. The main differences are in how we
access the GUI components. Using JFCUnit we don't need direct access to them. Instead, we use the
JFC T estHe lpe r class to find them for us. Since the GUI is simple, we have only a single instance of
some classes of components (i.e., one list, and one field) so we can find them by class and not worry
about naming them. To find the buttons we can either request all the buttons and search the resulting
collection for the instance we want, or we can name them. Here, we'll do the latter. Note that the
search-based approach can be a better solution if we are adding tests to an existing GUI for which we

don't have (or can't change)the source code (and hence, don't know or can't set the widget names).
pub l ic cl ass T est Wi dget s ex tend s JFC T e stC a s e {
p r ivate JF CT est He lper hel per;
p r ivate Mo ck Con tr ol c ontr ol;
p r ivate Mo vi eLi st Edit or m ockE d i tor ;
p r ivate Wi nd ow wi ndow ;
p r otect ed vo id se tUp( ) th rows E xce p t ion {
super .se tU p() ;
helpe r = n ew JF CTes tHel per( ) ;
contr ol = Eas yM ock. nice Cont r o lFo r ( Mov i e Li st Ed i to r .c la s s) ;
mockE dit or = (M ovie List Edit o r ) c o n tro l . ge tM oc k () ;
contr ol. ac tiv at e();
Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
windo w = h elp er .get Wind ow(" M o vie L ist " ) ;
}
p r otect ed vo id te arDo wn() thr o w s E x c ept i o n {
super .te ar Dow n( );
helpe r.c le anU p( this );
}
p u blic voi d tes tL ist( ) {
JList mo vi eLi st = ( JLis t)he l p er. f i ndC o m po ne nt ( JL i st .c l as s, wi nd o w, 0 ) ;
asser tNo tN ull (" Movi e li st s h o uld b e n o n n ul l" , mo v ie Li s t) ;
asser tTr ue ("M ov ie l ist shou l d be s how i n g" ,m ov i eL i st .i s Sh ow i ng () ) ;
}
publi c v oi d t es tFie ld() {
JText Fie ld mo vi eFie ld =
( J TextF iel d) hel pe r.fi ndCo mpon e n t(J T e xtF i e ld .c la s s,
w ind o w ,
0 );
asser tNo tN ull (" Movi e fi eld s h oul d be n o n nu ll " ,m o vi eF i el d) ;
asser tTr ue ("M ov ie f ield sho u l d b e sho w i ng ", mo v ie F ie ld . is Sh o wi ng ( )) ;
}

p u blic voi d tes tA ddBu tton () {


JButt on ad dBu tt on =
( J Butto n)h el per .f indN amed Comp o n ent ( " add B u tt on ",
w ind o w ,
0 );
asser tNo tN ull (" Add butt on s h o uld b e n o n n ul l" , ad d Bu tt o n) ;
asser tTr ue ("A dd but ton shou l d be s how i n g" ,a dd B ut t on .i s Sh ow i ng () ) ;
asser tEq ua ls( "A dd b utto n sh o u ld b e la b e le d \" A dd \ "" ,
"A dd",
ad dBut ton. getT e x t() ) ;
}
p u blic voi d tes tD elet eBut ton( ) {
JButt on de let eB utto n =
(JBut ton )h elp er .fin dNam edCo m p one n t ("d e l et eB ut t on " ,
wi n d ow,
0 );
asser tNo tN ull (" Dele te b utto n sho u l d b e no n nu l l" , de le t eB ut t on );
asser tTr ue ("D el ete butt on s h o uld b e s h o wi ng ", d el e te Bu t to n. i sS ho w in g( ) );

asser tEq ua ls( "D elet e bu tton s hou l d be l ab el ed \" D el et e \" ",
"D elet e",
de lete Butt on.g e t Tex t ( ));
}

There are more differences in the TestO p era t i on class. Notice how we are now manipulating the GUI
components at a higher level by providing events to them, rather than directly manipulating their
internal state.
pub l ic cl ass T est Op erat ion exte n d s J F C Tes t C as e {
p r ivate st at ic fi nal Stri ng L O S T I N SPA C E = " Lo s t I n Sp a ce ";
p r ivate Ve ct or mo vieN ames = n u l l;
p r ivate Wi nd ow wi ndow = n ull;
p r ivate JF CT est He lper hel per;
p r otect ed vo id se tUp( ) th rows E xce p t ion {
super .se tU p() ;
helpe r = n ew JF CTes tHel per( ) ;
movie Nam es = ne w Ve ctor () {
{ a dd( "S tar W ars" ); a dd(" S t ar T r ek" ) ; a dd (" S ta r ga te " ); }
};
}
p r otect ed vo id te arDo wn() thr o w s E x c ept i o n {
super .te ar Dow n( );
helpe r.c le anU p( this );
}

p u blic voi d tes tM ovie List () t h r ows J FCT e s tE xc ep t io n {


MockC ont ro l c on trol = E asyM o c k.c o n tro l F or (M ov i eL i st Ed i to r. c la ss ) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s , 1) ;
contr ol. ac tiv at e();
Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
windo w = h elp er .get Wind ow(" M o vie L ist " ) ;
JList mo vi eLi st = ( JLis t)he l p er. f i ndC o m po ne nt ( JL i st .c l as s, wi nd o w, 0 ) ;
ListM ode l mov ie List Mode l = m o vie L i st. g e tM od el ( );
asser tEq ua ls( "M ovie lis t is t he w r ong s iz e" ,
mo vieN ames .siz e ( ),
mo vieL istM odel . g etS i z e() ) ;
for ( int i = 0; i
< m o vieNa mes .s ize () ; i+ +) {
ass ert Eq ual s( "Mov ie l ist c o nta i n s b a d n am e" ,
mo vieN ames .get ( i ),
mo vieL istM odel . g etE l e men t A t( i) );
}
}

contr ol. ve rif y( );

p u blic voi d tes tA dd() thr ows J F CTe s t Exc e p ti on , E xc e pt io n {


Vecto r m ov ieN am esWi thAd diti o n = n e w V e c to r( mo v ie N am es ) ;
movie Nam es Wit hA ddit ion. add( L O ST I N SP A C E) ;
MockC ont ro l c on trol = E asyM o c k.c o n tro l F or (M ov i eL i st Ed i to r. c la ss ) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s , 1) ;
mockE dit or .ad d( LOST IN SPAC E ) ;
contr ol. se tVo id Call able (1);
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s W ith A d di ti on , 1 ) ;
contr ol. ac tiv at e();
Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
windo w = h elp er .get Wind ow(" M o vie L ist " ) ;
JText Fie ld mo vi eFie ld =
(JTex tFi el d)h el per. find Comp o n ent ( J Tex t F ie ld .c l as s ,
wi n d ow,
0) ;
movie Fie ld .re qu estF ocus ();
helpe r.s en dSt ri ng(t his, mov i e Fie l d , L O S T IN S P AC E );

JButt on ad dBu tt on =
(JBut ton )h elp er .fin dNam edCo m p one n t ("a d d Bu tt on " ,
wi n d ow,
0) ;
addBu tto n. doC li ck() ;
JList mo vi eLi st = ( JLis t)he l p er. f i ndC o m po ne nt ( JL i st .c l as s, wi nd o w, 0 ) ;
ListM ode l mov ie List Mode l = m o vie L i st. g e tM od el ( );
asser tEq ua ls( "M ovie lis t is t he w r ong s iz e af t er ad d" ,
m ov ieNa mesW ithA d d iti o n .si z e () ,
m ov ieLi stMo del. g e tSi z e ()) ;
asser tEq ua ls( "M ovie lis t do e s n't c ont a i n ne w n am e ",
L OS T IN SPA CE,
m ov ieLi stMo del. g e tEl e m ent A t (m ov ie N am e s. si z e( )) ) ;
}

contr ol. ve rif y( );

p u blic voi d tes tD elet e() thro w s JF C T est E x ce pt io n , E xc ep t io n {


Vecto r m ov ieN am esWi thDe leti o n = n e w V e c to r( mo v ie N am es ) ;
movie Nam es Wit hD elet ion. remo v e (1) ;
MockC ont ro l c on trol = E asyM o c k.c o n tro l F or (M ov i eL i st Ed i to r. c la ss ) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s , 1) ;
mockE dit or .de le te(1 );
contr ol. se tVo id Call able (1);
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s W ith D e le ti on , 1 ) ;

contr ol. ac tiv at e();


Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
windo w = h elp er .get Wind ow(" M o vie L ist " ) ;
JList mo vi eLi st = ( JLis t)he l p er. f i ndC o m po ne nt ( JL i st .c l as s, wi nd o w, 0 ) ;
movie Lis t. set Se lect edIn dex( 1 ) ;
JButt on de let eB utto n =
(JBut ton )h elp er .fin dNam edCo m p one n t ("d e l et eB ut t on " ,
wi n d ow,
0) ;
delet eBu tt on. do Clic k();
ListM ode l mov ie List Mode l = m o vie L i st. g e tM od el ( );
asser tEq ua ls( "M ovie lis t is t he w r ong s iz e af t er de le t e" ,
m ov ieNa mesW ithD e l eti o n .si z e () ,
m ov ieLi stMo del. g e tSi z e ()) ;

contr ol. ve rif y( );

Finally, here's the GUIclass. It is almost identical to what we had before, except for the component
access methods that we don't need.
pac k age c om. sa ors a. tddb ook. samp l e s.g u i .jf c u ni t;
imp o rt
imp o rt
imp o rt
imp o rt
imp o rt

ja va. aw t.F lo wLay out;


ja va. aw t.e ve nt.A ctio nEve n t ;
ja va. aw t.e ve nt.A ctio nLis t e ner ;
ja va. ut il. Ve ctor ;
ja vax .s win g. *;

pub l ic cl ass M ovi eL istW indo w ex t e nds J Fra m e {


p r ivate Mo vi eLi st Edit or m yEdi t o r = n ull ;
p r ivate JL is t m ov ieLi st=n ull;
p r ivate JT ex tFi el dmov ieFi eld = nul l ;
p u blic Mov ie Lis tW indo w(Mo vieL i s tEd i t or a n Ed it or ) {
super ();
myEdi tor = an Ed itor ;
}
p u blic voi d ini t( ) {
setTi tle (" Mov ie Lis t");
setLa you t( );
initM ovi eL ist () ;
initM ovi eF iel d( );
initA ddB ut ton () ;
initD ele te But to n();
pack( );
}

p r ivate vo id se tL ayou t() {


getCo nte nt Pan e( ).se tLay out( n e w F l o wLa y o ut () );
}
p r ivate vo id in it Movi eLis t() {
movie Lis t = n ew JLi st(g etMo v i es( ) ) ;
JScro llP an e s cr olle r = new J S cro l l Pan e ( mo vi eL i st ) ;
getCo nte nt Pan e( ).ad d(sc roll e r );
}
p r ivate vo id in it Movi eFie ld() {
movie Fie ld = ne w JT extF ield ( 1 6);
getCo nte nt Pan e( ).ad d(mo vieF i e ld) ;
}

p r ivate vo id in it AddB utto n() {


JButt on ad dBu tt on = new JBu t t on( " A dd" ) ;
addBu tto n. set Na me(" addB utto n " );
addBu tto n. add Ac tion List ener ( n ew A c tio n L is te ne r () {
pub lic v oid a ctio nPer form e d (Ac t i onE v e nt e ) {
m yEd it or. ad d(mo vieF ield . g etT e x t() ) ;
m ovi eL ist .s etLi stDa ta(g e t Mov i e s() ) ;
}
});
getCo nte nt Pan e( ).ad d(ad dBut t o n);
}
p r ivate vo id in it Dele teBu tton ( ) {
JButt on de let eB utto n = new J B utt o n ("D e l et e" );
delet eBu tt on. se tNam e("d elet e B utt o n ");
delet eBu tt on. ad dAct ionL iste n e r(n e w Ac t i on Li st e ne r () {
pub lic v oid a ctio nPer form e d (Ac t i onE v e nt e ) {
m yEd it or. de lete (mov ieLi s t .ge t S ele c t ed In de x () ) ;
m ovi eL ist .s etLi stDa ta(g e t Mov i e s() ) ;
}
});
getCo nte nt Pan e( ).ad d(de lete B u tto n ) ;
}
p r ivate Ve ct or ge tMov ies( ) {
Vecto r m ov ies = myE dito r.ge t M ovi e s ();
retur n ( mo vie s == n ull) ? n e w Ve c t or( ) : mo vi e s;
}

p u blic sta ti c v oi d st art( Movi e L ist E d ito r an Ed it o r) {


Movie Lis tW ind ow win dow = ne w Mov i e Lis t W in do w( a nE d it or ) ;
windo w.i ni t() ;
windo w.s ho w() ;
}

Amazon

Prev

don't be afraid of buying books

Next

JEMMY
Jemmy is from the NetBeans world where it takes the form of a module that plugs into the NetBeans
platform. The developers of Jemmy made a wise decision and made Jemmy capable of being used in a
stand-alone fashion, independent of NetBeans. In fact, Jemmy can be used directly from a standard
JUnit T e stC as e. We'll look at how Jemmy is structured and used, then put it into practice in our
example.

How it works
Jemmy works a bit differently than JFCUnit. Using Jemmy, you create operators that wrap a
corresponding Swing object.
Each component in the Swing API has a corresponding O pe ra to r class in the Jemmy API.
Before creating operators for individual components, you should create one for the main window of the
GUI you are testing, for example:
JFr a meOpe rat or ma in Wind ow = new J Fra m e Ope r a to r( "M o vi e L is t ") ;

Now, to find the Add button from the example, we create a J B ut to n Op e ra to r in the window we
found with a specific label text:
JBu t tonOp era to r a dd Butt on = new J But t o nOp e r at or (m a in W in do w , "A d d" );

For most operators there are a variety of constructors. Using J B ut to n Op e ra to r as an example, we'll
look at them below. With the exception of the first case, all constructors have an initial parameter that is
the operator for a parent (recursively) container (e.g., the main window J F ra me ).
JButtonOperator(JButton b) creates an operator that wraps the given JB u tt on .

JButtonOperator(ContainerOperator cont, ComponentChooser chooser, int index)


searches through all J Butt ons contained by c ont , filtering them using c ho os e r, and
creates an operator for the index th one (zero based) that is found.

JButtonOperator(ContainerOperator cont, ComponentChooser chooser) searches


through JB utt on s contained by c ont , filtering them using ch o os e r, and creates an
operator for the first one found.

JButtonOperator(ContainerOperator cont, String text, int index) searches through


all J But to ns contained by cont and creates an operator for the in d ex th one (zero

based) that has t ext as its label text.

JButtonOperator(ContainerOperator cont, String text) searches through all


JBut ton s contained by co nt and creates an operator for the first one that has t ex t as
its label text.

JButtonOperator(ContainerOperator cont, int index) creates an operator for the


inde x th one (zero based) J Butt o n contained by co nt

JButtonOperator(ContainerOperator cont) creates an operator for the first J Bu tt o n


contained by co nt

One approach that's useful when we are test-driving a GUI is to give every component a unique name
(using s etN am e() ). Then we can use a C o m pon e n tCh o os e r that searches for a given name. The
chooser will look something like:
pub l ic cl ass N ame Ba sedC hoos er i m p lem e n ts C o mp on en t Ch o os er {
p r ivate St ri ng na me;
p u blic Nam eB ase dC hoos er(S trin g com p o nen t N am e) {
name = c om pon en tNam e;
}
p u blic boo le an ch eckC ompo nent ( C omp o n ent a Co mp on e nt ) {
Strin g t he Nam e = aC ompo nent . g etN a m e() ;
retur n ( th eNa me != null ) && t heN a m e.e q u al s( na m e) ;
}

p u blic Str in g g et Desc ript ion( ) {


retur n " Ma tch es Com pone nts n a med \ "" + n am e+ " \ "" ;
}

Here's an example of how we can use it:


JLi s tOper ato r mov ie List =
new JLi st Ope ra tor( main Wind o w , n e w Na m e Ba se dC h oo s er (" m ov ie L is t" ) );

As you can see, the work of finding components is done in the Jemmy operator constructors. A result of
this is that our test code is very clean...a single line of overhead for each component we want to use.
Additionally, the operator constructors throw T i m eou t E xp ir ed E xc ep t io n if the requested component
cannot be found in a reasonable period of time. JUnit will catch these and fail if they are thrown. This
means that we don't need to verify that the desired component was found as we did in JFCUnit. This
simplifies our tests even more.
Once we have operators for the components we need to use, we can use those operators to access and
manipulate those components. Each operator provides methods that match those of the wrapped
component and forwards calls as appropriate.
Although simple to use, the Jemmy API is large and involved. To make it easier to learn and use, there
are a few simple ideas to keep in mind:
Find components by constructing an appropriate operator with the information required to find
what you need.
Operator constructors throw an exception that will cause the test to fail if a required component
isn't present, so you don't need to check the result.
Most component methods are present in the operators and are forwarded through to the
component.
The application being accessed through Jemmy has to be run in the
same JVM as the code that uses Jemmy. One way to accomplish this is
to launch the GUI being tested in the s e tU p () method.

The Example
Rather than go through the same exercise again with Jemmy, since it is quite similar to the JFCUnit
example, I will simply present the Jemmy-based tests that result in the comparable UI code.
pub l ic cl ass T est Op erat ion exte n d s T e s tCa s e {
p r ivate st at ic fi nal Stri ng L O S T I N SPA C E = " Lo s t I n Sp a ce ";
p r ivate Ve ct or mo vieN ames = n u l l;
p r ivate JF ra meO pe rato r ma inWi n d ow = nul l ;

p r otect ed vo id se tUp( ) th rows E xce p t ion {


super .se tU p() ;
movie Nam es = ne w Ve ctor ();
movie Nam es .ad d( "Sta r Wa rs") ;
movie Nam es .ad d( "Sta r Tr ek") ;
movie Nam es .ad d( "Sta rgat e");
}
p r otect ed vo id te arDo wn() thr o w s E x c ept i o n {
super .te ar Dow n( );

mainW ind ow .di sp ose( );

p u blic voi d tes tM ovie List () {


MockC ont ro l c on trol = E asyM o c k.c o n tro l F or (M ov i eL i st Ed i to r. c la ss ) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s , 1) ;
contr ol. ac tiv at e();
Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
mainW ind ow = ne w JF rame Oper a t or( " M ovi e Li st ") ;
JList Ope ra tor m ovie List = n e w JL i s tOp e r at or (m a in W in do w );
ListM ode l mov ie List Mode l = m o vie L i st. g e tM od el ( );
asser tEq ua ls( "M ovie lis t is t he w r ong s iz e" ,
m ovie Name s.si z e (),
m ovie List Mode l . get S i ze( ) ) ;
for ( int i = 0; i
< m o vieNa mes .s ize () ; i+ +) {
ass ert Eq ual s( "Mov ie l ist c o nta i n s b a d n am e" ,
mov ieNa mes. g e t(i ) ,
mov ieLi stMo d e l.g e t Ele m e nt At (i ) );
}
}

contr ol. ve rif y( );

p u blic voi d tes tA dd() {


Vecto r m ov ieN am esWi thAd diti o n = n e w V e c to r( mo v ie N am es ) ;
movie Nam es Wit hA ddit ion. add( L O ST I N SP A C E) ;
MockC ont ro l c on trol = E asyM o c k.c o n tro l F or (M ov i eL i st Ed i to r. c la ss ) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s , 1) ;
mockE dit or .ad d( LOST IN SPAC E ) ;
contr ol. se tVo id Call able (1);
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s W ith A d di ti on , 1 ) ;
contr ol. ac tiv at e();
Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
mainW ind ow = ne w JF rame Oper a t or( " M ovi e Li st ") ;

JText Fie ld Ope ra tor movi eFie l d = n e w J T e xt Fi el d Op e ra to r (m ai n Wi nd o w) ;


movie Fie ld .en te rTex t(LO ST I N SPA C E );
JButt onO pe rat or addB utto n = n e w J B u tto n O pe ra to r (m a in Wi n do w, "A dd " );
addBu tto n. doC li ck() ;
JList Ope ra tor m ovie List = n e w JL i s tOp e r at or (m a in W in do w );
ListM ode l mov ie List Mode l = m o vie L i st. g e tM od el ( );
asser tEq ua ls( "M ovie lis t is t he w r ong s iz e af t er ad d" ,
m ovie Name sWit h A ddi t i on. s i ze () ,
m ovie List Mode l . get S i ze( ) ) ;
asser tEq ua ls( "M ovie lis t do e s n't c ont a i n ne w n am e ",

L OST IN S PACE ,
m ovie List Mode l . get E l eme n t At (m ov i eN a me s. s iz e( ) )) ;
}

contr ol. ve rif y( );

p u blic voi d tes tD elet e() {


Vecto r m ov ieN am esWi thDe leti o n = n e w V e c to r( mo v ie N am es ) ;
movie Nam es Wit hD elet ion. remo v e (1) ;
MockC ont ro l c on trol = E asyM o c k.c o n tro l F or (M ov i eL i st Ed i to r. c la ss ) ;
Movie Lis tE dit or moc kEdi tor = (Mo v i eLi s t Ed it or ) c o nt ro l .g et M oc k( ) ;
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s , 1) ;
mockE dit or .de le te(1 );
contr ol. se tVo id Call able (1);
mockE dit or .ge tM ovie s();
contr ol. se tRe tu rnVa lue( movi e N ame s W ith D e le ti on , 1 ) ;
contr ol. ac tiv at e();
Movie Lis tW ind ow .sta rt(m ockE d i tor ) ;
mainW ind ow = ne w JF rame Oper a t or( " M ovi e Li st ") ;
JList Ope ra tor m ovie List = n e w JL i s tOp e r at or (m a in W in do w );
movie Lis t. cli ck OnIt em(1 , 1) ;
JButt onO pe rat or del eteB utto n = n e w JB u t to nO pe r at o r( ma i nW in d ow , " De le t e" );
delet eBu tt on. do Clic k();
ListM ode l mov ie List Mode l = m o vie L i st. g e tM od el ( );
asser tEq ua ls( "M ovie lis t is t he w r ong s iz e af t er de le t e" ,
m ovie Name sWit h D ele t i on. s i ze () ,
m ovie List Mode l . get S i ze( ) ) ;

contr ol. ve rif y( );

Let's have a look at Figure 8.3: t estA dd () in operation. It shows the typical sequence of a Jemmybased test:
1. Open the application window being tested.
2. Create the top level JFr ameOp e rat o r .
3. Create operators for the fields involved in the test, manipulating them as required (entering
text, making selections, etc.) to set the required state.
4. Create operators for the required command component(s), in this case, a J B ut to n .
5. Perform the action, e.g., clicking on the button.
6. Check the results.

Figure 8.3. Sequence diagram for the Jemmy part of testing


[View full size image]

aa dd dd ( )) .

Amazon

Prev

don't be afraid of buying books

Next

ULTRA-THIN GUI
When you practice TDD, you generally end up with your model well-encapsulated. When you get around
to adding a GUI, you find that all you need are components that make calls into the model. . . exactly
what the rules of good OO design tell you you should have. This makes test-driven development of the
GUI either trivial or beside the point.
This is the traditional approach to dealing with the GUI in a TDD context. It is still a fairly valid option.
If you make the GUI as thin as possible, then user interaction simply delegates to a domain object and
other components simply provide a getter/setter to get/set their value. All the action happens in the
domain objects. There is no logic in the GUI and so nothing that needs testing. The domain class can be
tested (through TDD) as any other. In fact, it should be already.
We can combine this approach with one of the previous ones to put functional tests in place for the GUI
as well. You could mock the domain class that responds to and controls the GUI to behave in a specific
way, although you will need to use more than EasyMock since the user action handlers will almost
always need to query or control the GUI components. Recall that mocks generated using EasyMock are
passive. They can not do anything other than maintain and verify expectations and provide return values.
To mock the domain classes you will need to have the mock query and/or manipulate the appropriate
components in response to user action (e.g., clicking the Add button calls a d d ( ) in the model which
should query the data fields, and update the list box).
One way to approach this is to use what Mike Feathers has called making a humble dialog[14].
We'll have a closer look at this method, for several reasons:
It is more in line with the philosophy of Test-Driven Development.
It results in an extremely thin GUI that is essentially just a view.
No special tools are required since all processing and computation is done in a non-GUI class
where it can easily be test-driven using standard tools.
I prefer it.
OK, we start with one simple test: Given a collection of movie names, fill in the listbox in the GUI.
p ub l ic c las s Tes t GUI e xten ds T e s t C a s e {
p u bl ic vo i d te s tLis t () {
Ve ct or m ovie N ames = ne w Ve c t o r ( ) {
{ add ( "Sta r War s "); add( " S t a r T r e k " ) ; a d d ( " S t a r g a t e " ) ; }
};
Mo ck Con t rol c ontr o l = Easy M o c k . c o n t r o l F o r ( M o v i e E d i t o r V i e w . c l a s s ) ;
Mo vi eEd i torV i ew m o ckVi ew = ( M o v i e E d i t o r V i e w ) c o n t r o l . g e t M o c k ( ) ;
mo ck Vie w .set M ovie N ames (mov i e N a m e s ) ;
co nt rol . setV o idCa l labl e();
co nt rol . acti v ate( ) ;
Mo vi eLi s tEdi t or e d itor = n e w M o v i e L i s t E d i t o r ( m o c k V i e w ) ;
ed it or. s etMo v ieNa m es(m ovie N a m e s ) ;

co nt rol . veri f y();

This is another good summary view of the TDD process. We write one simple test, with nothing else
written. That test, while simple, drives the creation of an interface and a class. To support the above
test, we need to create a couple of things.
First, the view interface that we are mocking:

p ub l ic i nte r face Movi e List Edit o r V i e w {


v o id s etM o vieN a mes( V ecto r mo v i e N a m e s ) ;
}

Next, the class that does the work:


p ub l ic c las s Mov i eLis t Edit or {
p r iv at e M o vieL i stEd i torV iew m y V i e w ;
p u bl ic Mo v ieLi s tEdi t or(M ovie L i s t E d i t o r V i e w a V i e w ) {
my Vi ew = aVi e w;
}

p u bl ic vo i d se t Movi e Name s(Ve c t o r m o v i e N a m e s ) {


my Vi ew. s etMo v ieNa m es(m ovie N a m e s ) ;
}

Notice how we do the simplest thing. The s e t M o v i e N a m e s ( ) method simply sends its argument on to
the view. It doesn't save it anywhere. That isn't needed to pass the test, so we don't do it.
Now we add another test, this one for the adding a movie operation. In the process, we noticed the
need for a common fixture, so we refactored that into a s e t U p ( ) method. Here's the test case now:
p ub l ic c las s Tes t GUI e xten ds T e s t C a s e {
p r iv at e V e ctor movi e Name s = n u l l ;
p r iv at e M o ckCo n trol cont rol= n u l l ;
p r iv at e M o vieL i stEd i torV iew m o c k V i e w = n u l l ;
p r ot ec ted void setU p () {
mo vi eNa m es = new V ecto r() {
{ add ( "Sta r War s "); add( " S t a r T r e k " ) ; a d d ( " S t a r g a t e " ) ; }
};
co nt rol = Ea s yMoc k .con trol F o r ( M o v i e L i s t E d i t o r V i e w . c l a s s ) ;
mo ck Vie w = ( M ovie L istE dito r V i e w ) c o n t r o l . g e t M o c k ( ) ;
}
p u bl ic vo i d te s tLis t () {
mo ck Vie w .set M ovie N ames (mov i e N a m e s ) ;
co nt rol . setV o idCa l labl e(1) ;
co nt rol . acti v ate( ) ;
Mo vi eLi s tEdi t or e d itor = n e w M o v i e L i s t E d i t o r ( m o c k V i e w ) ;
ed it or. s etMo v ieNa m es(m ovie N a m e s ) ;
}

co nt rol . veri f y();

p u bl ic vo i d te s tAdd i ng() {
St ri ng L OST I N SP A CE = "Lo s t I n S p a c e " ;
Ve ct or m ovie N ames W ithA ddit i o n = n e w V e c t o r ( m o v i e N a m e s ) ;
mo ck Vie w .set M ovie N ames (mov i e N a m e s ) ;
co nt rol . setV o idCa l labl e(1) ;

mo ck Vie w .get N ewNa m e();


co nt rol . setR e turn V alue (LOS T I N S P A C E , 1 ) ;
mo ck Vie w .set M ovie N ames (mov i e N a m e s W i t h A d d i t i o n ) ;
co nt rol . setV o idCa l labl e(1) ;

co nt rol . acti v ate( ) ;


Mo vi eLi s tEdi t or e d itor = n e w M o v i e L i s t E d i t o r ( m o c k V i e w ) ;
ed it or. s etMo v ieNa m es(m ovie N a m e s ) ;
ed it or. a dd() ;

co nt rol . veri f y();

We need to add a method to the interface (S t r i n g g e t N e w N a m e ( ) ), and an a d d ( ) method to the


editor class. We also need to save the list of movie names.
Here's the new version of the class:
p ub l ic c las s Mov i eLis t Edit or {
p r iv at e M o vieL i stEd i torV iew m y V i e w ;
p r iv at e V e ctor myNa m es = nul l ;
p u bl ic Mo v ieLi s tEdi t or(M ovie L i s t E d i t o r V i e w a V i e w ) {
my Vi ew = aVi e w;
}
p u bl ic vo i d se t Movi e Name s(Ve c t o r m o v i e N a m e s ) {
my Na mes = mo v ieNa m es;
my Vi ew. s etMo v ieNa m es(m ovie N a m e s ) ;
}

p u bl ic vo i d ad d () {
St ri ng n ewMo v ieNa m e = myVi e w . g e t N e w N a m e ( ) ;
my Na mes . add( n ewMo v ieNa me);
my Vi ew. s etMo v ieNa m es(m yNam e s ) ;
}

Figure 8.4 shows the sequence diagram for the a d d ( ) method we just wrote. While this is a very
simple example, we can see the clean separation of the interface and the application. The user clicks on
the add button, which is part of the interface, which simply delegates to the M o v i e L i s t E d i t o r . The
interaction with the interface is very simple: get the contents of a field and set the contents of a list.

Figure 8.4. Sequence diagram for

MM oo vv ii ee LL ii ss tt EE dd ii tt oo rr .. aa dd d ( ) .

[View full size image]

In making the new test (testA ddin g( ) ) work, notice how we simply added code without regard for
the already existing code. Also notice the duplication between s e t - M o v i e N a m e s ( ) and a d d ( ) . How
can we refactor that away?
First, look at s etMo v ieNa m es() . See how we store the argument in m y N a m e s , then send the argument
to the view? Recall that in getting test A d d i n g ( ) working we added the assignment to save the
names. Now we clean up. We start by noting that by the time we sent the argument to the view, we've
already saved it in m y Name s . We've just found a smaller bit of duplication. To get rid of it, we can send
the saved value of the argument rather than the argument itself. Compile and run the tests. Green bar!
Now s etM ov ieNa m es( ) is:
p ub l ic v oid setM o vieN a mes( Vect o r m o v i e N a m e s ) {
m y Na me s = movi e Name s ;
m y Vi ew .se t Movi e Name s (myN ames ) ;
}

Now, look at a dd() .See how it sends m y N a m e s to the view as well? Let's extract that line into a
separate method:
p ri v at e voi d upda t eNam e sInV iew( ) {
m y Vi ew .se t Movi e Name s (myN ames ) ;
}

Notice how we made the extracted method private? That's a good


habit to develop. The rationale is that extracted methods aren't part of
the external behavior, they support it. Therefore, they shouldn't be
made public. If it turns out later that they are useful to another class
or a subclass, then they can be changed to public or protected,
respectively. Until then, leave them private.

The a dd () method is also changed to use the extracted method:


p ub l ic v oid add( ) {
S t ri ng ne w Movi e Name = my View . g e t N e w N a m e ( ) ;
m y Na me s.a d d(ne w Movi e Name );
u p da te Nam e sInV i ew() ;
}

Compile and test. Green bar. The next step is to use the new method in s e t M o v i e N a m e s ( ) :
p ub l ic v oid setM o vieN a mes( Vect o r m o v i e N a m e s ) {
m y Na me s = movi e Name s ;
u p da te Nam e sInV i ew() ;
}

Compile and test. Green bar. Now on to deleting.


First, a test:
p ub l ic v oid test D elet i ng() {
V e ct or mo v ieNa m esWi t hDel etio n = n e w V e c t o r ( m o v i e N a m e s ) ;
m o vi eN ame s With D elet i on.r emov e ( 1 ) ;

m o ck Vi ew. s etMo v ieNa m es(m ovie N a m e s ) ;


c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. g etSe l ecti o n();
c o nt ro l.s e tRet u rnVa l ue(1 , 1) ;
m o ck Vi ew. s etMo v ieNa m es(m ovie N a m e s W i t h D e l e t i o n ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o c k V i e w ) ;
e d it or .se t Movi e Name s (mov ieNa m e s ) ;
e d it or .de l ete( ) ;
}

c o nt ro l.v e rify ( );

Notice how we are making the operations of the editor very simple:
add ( ) and del ete() ...no parameters, returning nothing. These are
just actions forwarded directly from the GUI, which is then queried for
its state as necessary. This, coupled with using an interface to define
the GUI, gives us very good separation between the domain code and
the GUI. This separation is worth striving for, and this approach helps
a great deal in achieving it.

Next, make the test pass as simply as possible:


p ub l ic v oid dele t e() {
i n t in dex = my V iew. g etSe lect i o n ( ) ;
m y Na me s.r e move ( inde x );
u p da te Nam e sInV i ew() ;
}

Time to clean up. OK, nothing to do this time. Since the u p d a t e N a m e s I n V i e w ( ) method was already
there, we just used it when writing del e t e ( ) .We could have explicitly written the call to update the
view, then refactored to replace it with the update method, but why? We've done that duplication
elimination already, and we learned something in the process.
Now we have to create a real GUI to use in place of the mock. Notice that with this degree of
separation it will be very thin, and very simple, since it is almost completely passive.
In implementing the GUI we discover an oversight. It needs to know about the editor so that it can
delegate to it. We haven't done this yet since we were driving the editor from the tests, not the mock
view. But this is simple enough to add. We just need a method to set the editor. For the test with the
mock it doesn't really matter since the calls that the GUI would be making in response to button clicks
are coming from the test itself. Here's the new method in the interface:
v oi d s et Edi t or(M o vieL i stEd itor a n E d i t o r ) ;

Where is the best place to call this? The constructor of M o v i e L i s t E d i t o r is a good choice:

p ub l ic M ovi e List E dito r (Mov ieLi s t E d i t o r V i e w a V i e w ) {


m y Vi ew = a View ;
m y Vi ew .se t Edit o r(th i s);
}

To accommodate this call being made to the mock in the tests, we need to set up the mock to accept it
with any argument value. We can do this using the s e t D e f a u l t - V o i d C a l l a b l e ( ) method on the
mock control. And since this requirement applies to all of the tests, we can put it in s e t U p ( ) after
creating the mock. Here's the new version of s e t U p ( ) ; notice the last two lines:
p ro t ec te d v o id s e tUp( ) {
m o vi eN ame s = n e w Ve c tor( ) {
{ ad d(" S tar W ars" ) ; ad d("S t a r T r e k " ) ; a d d ( " S t a r g a t e " ) ; }
};

c o nt ro l = Easy M ock. c ontr olFo r ( M o v i e L i s t E d i t o r V i e w . c l a s s ) ;


m o ck Vi ew = (Mo v ieLi s tEdi torV i e w ) c o n t r o l . g e t M o c k ( ) ;
m o ck Vi ew. s etEd i tor( n ull) ;
c o nt ro l.s e tDef a ultV o idCa llab l e ( ) ;

Now, with these tweaks made, we can write the GUI code. It is very much like the previous version,
except simpler. Notice that it is mostly just building the GUI. Other than that there are just accessors
and mutators for component contents, and enough code to delegate user actions to the associated
editor.
p ub l ic c las s Swi n gMov i eLis tEdi t o r V i e w e x t e n d s J F r a m e
implements MovieListEditorView {
p r iv at e J T extF i eldm o vieF ield = n u l l ;
p r iv at e J L ist m ovie L ist= null ;
p r iv at e M o vieL i stEd i tor myEd i t o r ;
p u bl ic Sw i ngMo v ieLi s tEdi torV i e w ( ) {
su pe r() ;
}
p u bl ic vo i d se t Edit o r(Mo vieL i s t E d i t o r a n E d i t o r ) {
my Ed ito r = a n Edit o r;
}
p u bl ic vo i d se t Movi e Name s(Ve c t o r m o v i e N a m e s ) {
mo vi eLi s t.se t List D ata( movi e N a m e s ) ;
}
p u bl ic St r ing g etNe w Name () {
re tu rn m ovie F ield . getT ext( ) ;
}
p u bl ic in t get S elec t ion( ) {
re tu rn m ovie L ist. g etSe lect e d I n d e x ( ) ;
}
p u bl ic vo i d in i t() {
se tL ayo u t();
in it Mov i eLis t ();
in it Mov i eFie l d();
in it Add B utto n ();
in it Del e teBu t ton( ) ;
pa ck ();
}
p r iv at e v o id s e tLay o ut() {
ge tC ont e ntPa n e(). s etLa yout ( n e w F l o w L a y o u t ( ) ) ;
}
p r iv at e v o id i n itMo v ieLi st() {
mo vi eLi s t = n ew J L ist( new V e c t o r ( ) ) ;
JS cr oll P ane s crol l er = new J S c r o l l P a n e ( m o v i e L i s t ) ;
ge tC ont e ntPa n e(). a dd(s crol l e r ) ;
}

p r iv at e v o id i n itMo v ieFi eld( ) {


mo vi eFi e ld = new J Text Fiel d ( 1 6 ) ;
ge tC ont e ntPa n e(). a dd(m ovie F i e l d ) ;
}
p r iv at e v o id i n itAd d Butt on() {
JB ut ton addB u tton = ne w JB u t t o n ( " A d d " ) ;
ad dB utt o n.ad d Acti o nLis tene r ( n e w A c t i o n L i s t e n e r ( ) {
pu bli c voi d act i onPe rfor m e d ( A c t i o n E v e n t e ) {
myE d itor . add( ) ;
}
}) ;
ge tC ont e ntPa n e(). a dd(a ddBu t t o n ) ;
}

p r iv at e v o id i n itDe l eteB utto n ( ) {


JB ut ton dele t eBut t on = new J B u t t o n ( " D e l e t e " ) ;
de le teB u tton . addA c tion List e n e r ( n e w A c t i o n L i s t e n e r ( ) {
pu bli c voi d act i onPe rfor m e d ( A c t i o n E v e n t e ) {
myE d itor . dele t e();
}
}) ;
ge tC ont e ntPa n e(). a dd(d elet e B u t t o n ) ;
}

Note that we could use any of the earlier GUI-oriented techniques to test-drive the creation of the GUI
in this execution as well. For this simple example it would have been overkill. In Part III we'll see how
that approach works.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
In this chapter we've explored approaches to test-driving the development of a graphical user interface.
At one extreme was a brute force approach, which required that the tests have direct access to the
components making up the GUI. This is very coupled, and requires a significant amount of overhead in
the GUI classes. At the other end of the spectrum was an elegant, flexible, two-stage approach. In that
we separated the user interface into two layers: one containing the logic of the user interaction, and one
containing the presentation code.
Using the latter technique, we can test-drive very simple, well decoupled interfaces. Using a mocked
view in place of a real GUI component we can design and develop the logical layer of the user interface,
which can be used as the basis of any sort of interface: Swing, Web, or whatever is the fad of the day.
In fact, it can easily, and cheaply, support multiple presentation layers.
When using JFCUnit or Jemmy we can drive the actual Swing-based GUI component. This approach
(using Jemmy) will be put into practice in Part III of this book. Jemmy has several advantages, including
not needing any extra helper utilities and being useful for adding tests to existing GUI components.

Amazon

Prev

don't be afraid of buying books

Next

Part III: A JAVA PROJECT: TEST-DRIVEN END


TO END
This is a practical book. With that in mind, we'll now explore and learn TDD in the
context of a real project. By the time you get to this point in the book you should know
enough back ground and be familiar enough with the tools and techniques that you can
undertake test-driving an application.
We start off by defining the project in XP fashion:
an overall vision,
a set of user stories, and
a set of tasks into which the stories have been decomposed.
That is the purpose of Chapter 9. Each of the remaining chapters in this part of the book
will cover the development of a single story. Sections within each chapter will focus on a
specific task within the story.
I've written this part of the book as though I were sitting with you, the reader, and
working side-by-side on the project. The style is intentionally a little more casual and
conversational. I've tried to make you feel as though you are here with me as I write.
You have to provide your own coffee, though.
The code shown here is real. It was copied directly from the IDE. Sometimes we make
mistakes or oversights that we then catch, sometimes they get caught later. It's not
perfect, but it doesn't have to be. It does have to be understandable and malleable,
though. The final code is available at the companion Web site [URL 65].
So, let's get to it!

Amazon

Prev

don't be afraid of buying books

Chapter 9. THE PROJECT


Give wings to my propension and cut off
All fears attending on so dire a project.
Troiles and Cressida, Act 2 Scene 2 William Shakespeare

Next

Amazon

Prev

don't be afraid of buying books

Next

OVERVIEW
This part of the book is written around the Test-Driven Development of a single project: an application
to help keep track of movies you would like to see and help in choosing a movie to see.
The following chapters are an annotated walk through the development of the application. It's real code;
how it was really written. I wrote this part of the book as the code was being written. Code was copied
and pasted directly from Eclipse into Emacs in real time. So sometimes you'll see things that aren't quite
right. In the last chapter of this part I'll talk a bit about what might have been done differently.
This project was done using eXtreme Programming, so requirements are written as user stories.
However, the stories are ordered with a linear path through the project in mind, rather than by some
measure of business value of importance to a customer. The stories are then decomposed into tasks.
Once you are at the task level, it makes little difference how you got there. I feel very strongly that,
while it is a cornerstone of XP, TDD can be used in many development processes. The caveat is that Big
Design Up Front (BDUF) conflicts with TDD, so you do need to be using a fairly agile process that lets
you evolve the design organically. In particular, you need to be practicing an agile approach to modeling
and design (see Appendix B).
As we work on each task, we'll start by sketching out what tests we'll want to see passing. These tests
are identified at the beginning of the task as we tackle each one. For your convenience, a full list of
these tests is provided in Chapter 20, with references to where each is defined and implemented. At
times we'll discover more tests as we work. These are taken as they come and no extra effort is made
to make them stand out.
First, we start with a vision statement for the project.

Project Vision
Make it easy to keep track of movies you want to see. Provide support for ratings, reviews,
recommendations, etc., to help in deciding what to watch.

Amazon

Prev

don't be afraid of buying books

Next

USER STORIES AND TASKS


Now we'll look at the user stories that specify what the system has to do. At the same time we'll also
look at how the stories break down into tasks. Since this isn't a book about XP per se, we won't examine
this process. If you want to learn more about XP, see [6], [8], [URL 11], and [URL 12]

Story 1. Movie List


Provide a way to keep a list of movies and a way to add movies to it. Ordering of the list isn't a
concern.
Task 1-1. Make a container for movies with a way to add to it. Doesn't have to be
ordered or sorted.

Task 1-2. Make a GUI that shows a list of movies. List order is just what is in the
underlying collection. List should scroll as required.

Task 1-3. Provide a text field and "Add" button for adding movies to the list via the GUI.

Story 2. Movies Can Be Renamed


Provide a way to rename movies that are in the list.
Task 2-1. Allow the movie name to be changed.

Task 2-2. On the GUI, selecting a movie fills in the text field with its name. An "Update"
button will update the movie name with the contents of the field.

Story 3. Movies Are Unique


A movie appears only once in the list; the names are unique and case-insensitive. Attempts to add a
duplicate should fail and result in an error.
Task 3-1. Enforce uniqueness of movies during add and update operations. Attempts to
add or rename with the same name as an existing movie should raise an exception.

Task 3-2. The GUI provides an error message dialog when a non-unique add or rename
is attempted.

Story 4. Ratings
Movies each have a single, editable rating.
Task 4-1. Add support for a single rating to Movie. Support the concept of an unrated
movie.

Task 4-2. Show the rating in the GUI, next to the movie name.

Task 4-3. Allow the rating to be edited.

Story 5. Categories
Movies belong to a category; categories such as Horror, Comedy, etc.
Task 5-1. Add support for a movie to have a single category, from a fixed list of
alternatives.

Task 5-2. Show the category in the GUI. There should be a field that gets filled in when
a movie is selected.

Task 5-3. Allow the category of a movie to be changed by selecting it from a fixed list
of possibilities.

Story 6. Filter on Category


Filter the movies in the list by category. There needs to be a way to select the category to show,
including ALL.
Task 6-1. Given a category, ask the movie list for a list of its movies that belong to that
category.

Task 6-2. Get the entire list using an ALL category.

Task 6-3. Add a combo box to the GUI to select a category. When a category is
selected, update the list to include only movies of that category.

Task 6-4. When the list is filtered, changing a movie to a different category should
remove it from the list.

Story 7. Persistence
Movie data is persistent across application sessions.
Task 7-1. Write movie collection to a flat text file.

Task 7-2. Provide, in the GUI, the capability to save the movie collection to a specific
file.

Task 7-3. Provide, in the GUI, the capability to save the movie collection to the same
file it was previously saved to.

Task 7-4. Read a movie collection from a flat text file.

Task 7-5. Provide, in the GUI, the capability to load the movie collection from a file.

Story 8. Sorting
In the GUI, allow the movie list to be sorted by selecting from a set of options on a menu. Two
orderings are required: ascending by name, and descending by rating.
Task 8-1. Create the ability to compare two movies based on either name (ascending

order) or rating (descending order). This is needed for sorting.

Task 8-2. Add the capability to ask a M o v i e L i s t to provide its list sorted on a specific
attribute, for the moment this is one of name or rating.

Task 8-3. Add sorting capabilities to M o v i e L i s t E d i t o r .

Task 8-4. Add a View menu to the GUI with an option for each attribute that can be
sorted on. Selecting one of them results in the visible movie list being sorted by that
attribute.

Story 9. Multiple Ratings


Movies can have more than one rating, each with an identified source. Selecting a movie should show all
of its ratings. There should also be a way to add a rating. A movie's displayed rating should now be the
average of all of its individual ratings.
Task 9-1. Add support for multiple ratings rather than just one. Provide access to the
average of all the ratings.

Task 9-2. Create a class to represent the source for a rating. Keep in mind that a
single source can have many ratings, but not the reverse.

Task 9-3. Revamp the persistence capabilities to handle the changes to the rating
structure.

Task 9-4. In the GUI, show a list of all ratings for the selected movie.

Task 9-5. In the GUI, provide a way to add a rating to the selected movie. Be sure to
handle an empty source and map it to Anonymous.

Task 9-6. The single-rating field no longer serves any purpose. Remove it and update
the system appropriately.

Story 10. Reviews


Movies can have a review attached to each rating. If there is a review there must be a source.
Task 10-1. Add support for a single review to Rating.

Task 10-2. Add support to persistence for saving reviews.

Task 10-3. Add support to persistence for loading reviews.

Task 10-4. Add support to the GUI to display reviews.

Task 10-5. Add support to the GUI to enter a review as part of adding a rating.

Amazon

Prev

don't be afraid of buying books

Chapter 10. MOVIE LIST


Provide a way to keep a list of movies and a way to add movies to it. Ordering of the
list isn't a concern.

Next

Amazon

Prev

don't be afraid of buying books

Next

MAKE A MOVIE CONTAINER


Make a container for movies with a way to add to it. Doesn't have to be ordered or
sorted.

The First Test


We begin, of course, with a single test. What should that test be? Well, it needs to be something simple
since there is absolutely no code yet. How do we figure out what the first test should be? Look at the
task. We need to keep a list of movies. Should we write a test for a movie? Let's not get ahead of
ourselves. The task doesn't say much about movies. It just says that we have some and we want some
place to keep them. The some place to keep them is the focus of the task, and should be our focus as
well.
OK, so we want a test of something that holds movies. What should we test? Look back at the tips at
the end of Chapter 4. Can we get ideas from that? You bet! How about Test the simple stuff first,
specifically, empty collection or null object behavior. OK, let's start with a test for an empty list of
movies. Now that we've gotten a start we can come up with more tests that we'll need for this task:
Test 1. An empty list should have a size of zero.

Test 2. Adding a movie to an empty list should result in a list with a size of one.

Test 3. Adding two movies to an empty list should result in a list with a size of two.

Test 4. If we add a movie to a list, we should be able to ask if it's there and receive a
positive response. Conversely, we should receive a negative response when we ask about
a movie that we haven't added.

Test 1: An empty list should have a size of zero


Here it is, with the basic overhead. Notice that we've paid attention to another tip: we've included a
m ain ( ) method.
Eclipse provides a wizard for creating test cases. One of the options is
to automatically include exactly the main method we would like to
have.

p ubl i c cla ss Te stM ov ieLi st e xten d s Te s t Cas e {


pu b lic v oid t est Em ptyL istS ize( ) {
M ovieL ist e mpt yL ist = ne w Mo v i eLi s t ();
a ssert Equ al s(" Si ze o f em pty m o vie l ist sh o ul d b e 0 ." , 0 , em p ty Li s t. si z e( )) ;
}

pu b lic s tat ic vo id mai n(St ring [ ] ar g s ) {


j unit. tex tu i.T es tRun ner. run( T e stM o v ieL i st . cl as s );
}

This is about as simple as it gets. We create a new instance of MovieList, and assert that it contains zero
movies.
Try compiling...it won't. We need to add some code stubs to make it compile. Let's look at what we've
written and get a list of assumptions we've made.
Another feature of Eclipse is the flagging of situations which preve

Amazon

Prev

don't be afraid of buying books

Next

MAKE A MOVIE LIST GUI


Make a GUI that shows a list of movies. List order is just what is in the underlying
collection. List should scroll as required.
Now we'll add a user interface to what we have so far. For this task all we need is to display a list of
movies. We are going to tackle this in two phases: the logic, and the presentation.
Here's a list of tests we think we'll need:
Test 5. The logical layer should send the appropriate list of movies to the view for
display.

Test 6. The GUI should have a listbox and should display a list of movies in it as
requested.

Test 5: The logical layer should send the appropriate list of movies to
the view for display
We will use the humble dialog approach described in Chapter 8 to drive the development of the GUI.
This task is pretty simple: we just need to display the list.
Here's the test for the interface model class's ability to display a list:
p ub l ic c las s Tes t GUI e xten ds T e s t C a s e {
p r iv at e M o ckCo n trol cont rol = n u l l ;
p r iv at e M o vieL i stEd i torV iew m o c k V i e w = n u l l ;
p r iv at e V e ctor movi e s = null ;
p r iv at e M o vie s tarW a rs = nul l ;
p r iv at e M o vie s tarT r ek = nul l ;
p r iv at e M o vie s targ a te = nul l ;
p r iv at e M o vieL i st m o vieL ist;
p r ot ec ted void setU p () {
st ar War s = n e w Mo v ie() ;
st ar Tre k = n e w Mo v ie() ;
st ar gat e = n e w Mo v ie() ;
mo vi es = new Vect o r();
mo vi es. a dd(s t arWa r s);
mo vi es. a dd(s t arTr e k);
mo vi es. a dd(s t arga t e);

mo vi eLi s t = n ew M o vieL ist( ) ;


mo vi eLi s t.ad d (sta r Wars );
mo vi eLi s t.ad d (sta r Trek );
mo vi eLi s t.ad d (sta r gate );

p u bl ic vo i d te s tLis t () {
co nt rol = Ea s yMoc k .con trol F o r ( M o v i e L i s t E d i t o r V i e w . c l a s s ) ;
mo ck Vie w = ( M ovie L istE dito r V i e w ) c o n t r o l . g e t M o c k ( ) ;
mo ck Vie w .set M ovie s (mov ies) ;
co nt rol . setV o idCa l labl e(1) ;
co nt rol . acti v ate( ) ;
Mo vi eLi s tEdi t or e d itor = n e w M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
co nt rol . veri f y();
}

What are we testing here? We want a class that will sit between a M o v i e L i s t and the interface
(whatever that might be) for the purposes of presenting and editing the movie list. We'll call this new
class M ov ie Lis tE dit o r. We'll use EasyMock to mock the interface so that we can easily check that
the editor object is using it properly. This test just checks that when a M o v i e L i s t E d i t o r is asked to
manage the display and editing of a Mo v i e L i s t , it sends the expected collection of movies to the
interface for display.
The s et Up( ) method creates a few movies, and uses them to build a V e c t o r and a M o v i e L i s t . The
V ec t or is equal to the one that should be sent to the display by the M o v i e L i s t E d i t o r being tested.
Once the mock is set up, the test creates a M o v i e L i s t E d i t o r . We pass the constructor everything it
needs to do its job, specifically, the Mov i e L i s t to be edited and the interface object (in this case, a
mock).
Try to compile. As expected, it doesn't. We need to create the class M o v i e L i s t E d i t o r :
p ub l ic c las s Mov i eLis t Edit or {
p u bl ic Mo v ieLi s tEdi t or(M ovie L i s t m o v i e L i s t , M o v i e L i s t E d i t o r V i e w a V i e w ) {
}
}

We also need to create the interface being mocked:


p ub l ic i nte r face Movi e List Edit o r V i e w {
v o id s etM o vies ( Vect o r mo vies ) ;
}

This interface is fine the way it is, but M o v i e L i s t E d i t o r needs some work before the test will pass.
Fortunately, it doesn't take much to get the test to pass:
p ub l ic M ovi e List E dito r (Mov ieLi s t m o v i e L i s t , M o v i e L i s t E d i t o r V i e w a V i e w ) {
a V ie w. set M ovie s (new Vect or(m o v i e L i s t . g e t M o v i e s ( ) ) ) ;
}

Now we can build a real interface to use in place of the mock in order to actually run the application.

Test 6: The GUI should have a listbox and should display a list of
movies in it as requested
We will use Jemmy to drive the development of the actual GUI layer. Mostly, we will be concerned with
functional issues, not aesthetic ones. For this task all we need is a window with a list box that shows
the names of the movies in the underlying M o v i e L i s t .
Here's our Jemmy-based test case (most of it is setup, the actual test just grabs the list elements and
checks them):
p ub l ic c las s Tes t Swin g Movi eLis t E d i t o r V i e w e x t e n d s T e s t C a s e {
p r iv at e
p r iv at e
p r iv at e
p r iv at e
p r iv at e
p r iv at e

J F rame O pera t or m ainW i n d o w ;


V e ctor movi e s = null ;
M o vie s tarW a rs = nul l ;
M o vie s tarT r ek = nul l ;
M o vie s targ a te = nul l ;
M o vieL i st m o vieL ist = n u l l ;

p r ot ec ted void setU p () t hrow s E x c e p t i o n {


su pe r.s e tUp( ) ;
Sw in gMo v ieLi s tEdi t orVi ew.s t a r t ( ) ;
st ar War s = n e w Mo v ie(" Star W a r s " ) ;
st ar Tre k = n e w Mo v ie(" Star T r e k " ) ;

st ar gat e = n e w Mo v ie(" Star g a t e " ) ;

mo vi es = new Vect o r();


mo vi es. a dd(s t arWa r s);
mo vi es. a dd(s t arTr e k);
mo vi es. a dd(s t arga t e);

mo vi eLi s t = n ew M o vieL ist( ) ;


mo vi eLi s t.ad d (sta r Wars );
mo vi eLi s t.ad d (sta r Trek );
mo vi eLi s t.ad d (sta r gate );

p r ot ec ted void tear D own( ) th r o w s E x c e p t i o n {


su pe r.t e arDo w n();
ma in Win d ow.d i spos e ();
}
p u bl ic vo i d te s tLis t Cont ents ( ) {
ma in Win d ow = new J Fram eOpe r a t o r ( " M o v i e L i s t " ) ;
Mo vi eLi s tEdi t or e d itor =
ne w M o vieL i stEd i tor( movi e L i s t ,
(S wing M o v i e L i s t E d i t o r V i e w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;

JL is tOp e rato r mov i eLis t = n e w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;


Li st Mod e l li s tMod e l = movi e L i s t . g e t M o d e l ( ) ;
as se rtE q uals ( "Mov i e li st i s t h e w r o n g s i z e " ,
movi e s.si ze() ,
list M odel .get S i z e ( ) ) ;
fo r (in t i = 0; i < mo vies . s i z e ( ) ; i + + ) {
as ser t Equa l s("M o vie list c o n t a i n s b a d m o v i e a t i n d e x " + i ,
mo v ies. get( i ) ,
li s tMod el.g e t E l e m e n t A t ( i ) ) ;
}

Using this to drive the creation of the GUI we end up with:


p ub l ic c las s Swi n gMov i eLis tEdi t o r V i e w e x t e n d s J F r a m e
implements MovieListEditorView {
p r iv at e J L ist m ovie L ist = nu l l ;
p u bl ic Sw i ngMo v ieLi s tEdi torV i e w ( ) {
su pe r() ;
}
p u bl ic vo i d se t Movi e s(Ve ctor m o v i e s ) {
mo vi eLi s t.se t List D ata( movi e s ) ;
}

p u bl ic vo i d in i t() {
se tT itl e ("Mo v ie L i st") ;
ge tC ont e ntPa n e(). s etLa yout ( n e w F l o w L a y o u t ( ) ) ;
mo vi eLi s t = n ew J L ist( new V e c t o r ( ) ) ;

JS cr oll P ane s crol l er = new J S c r o l l P a n e ( m o v i e L i s t ,


S crol l Pane C onst ants . V E R T I C A L S C R O L L B A R A L W A Y S ,
S crol l Pane C onst ants . H O R I Z O N T A L S C R O L L B A R N E V E R ) ;
ge tC ont e ntPa n e(). a dd(s crol l e r ) ;
pa ck ();

p u bl ic st a tic v oid s tart () {


Sw in gMo v ieLi s tEdi t orVi ew w i n d o w = n e w S w i n g M o v i e L i s t E d i t o r V i e w ( ) ;
wi nd ow. i nit( ) ;
wi nd ow. s how( ) ;
}

Visual Inspection
Now that we have a real GUI we have something to look at. Notice how quickly we've gotten to the
point of having something on the screen. Not much, mind you, but something. Something to look at.
Something to show the customer. Something real.
So let's have a look (see Figure 10.1). Oh dear! There's the window. There's the scrollpane. Instead of
the list of movies we'd expect, there's list of object identifiers. The test tells us that the movies that
should be in the list box are, indeed, there. So, what's wrong?

Figure 10.1. Initial movie list application GUI.

Look at the code for M o vie:


p ub l ic c las s Movi e {
}

Well, that's the problem. There's nothing to be displayed. The items are in the list but there's nothing to
render. We need to give Mov i e something to identify it that can be placed in the list box. Now it's time
for you to sit back smugly and say that you said it should have had a name from the start. I thought
so, too, but it wasn't needed until now. We both knew that it would be eventually. Now it is, so now
we'll do something about it. But, we add no code without a failing test that requires it. So. . .
p ub l ic c las s Test M ovie exte nds T e s t C a s e {
p u bl ic vo i d te s tMov i eNam e() {
Mo vi e s t arWa r s = n ew M ovie ( " S t a r W a r s " ) ;
a ss ert E qual s ("st a rWar s sh o u l d h a v e n a m e \ " S t a r W a r s \ " . " ,
"St a r Wa rs",

sta r Wars .get N a m e ( ) ) ;

p u bl ic st a tic v oid m ain( Stri n g [ ] a r g s ) {


ju ni t.t e xtui . Test R unne r.ru n ( T e s t M o v i e . c l a s s ) ;
}

That looks good, but it causes our previous tests to fail because they used the default constructor (e.g.,
s ta r Wa rs = n ew M o vie( ) ;). We can either add a default constructor, or we can fix the other tests.
My opinion is that we fix the other tests rather than incur code debt. Why is this code debt? Because
we've just made the default constructor obsolete. Now that each M o v i e has a name that is set in the
constructor, can you imagine a use for a M o v i e without a name? Me neither. Do we do it now or after
we get the new test running? Now. Those tests will fail with errors if we don't. That will violate our goal
of only having one test failing at a time.
So, we go back and bring the tests up to date. (We won't include the code here, as the change is so
simple: you just need to change the default constructor calls to ones taking a movie name.) All of the
code in the book is available online anyway [URL 65].
Now we're back to t e stMo v ieNa me(). We need a g e t N a m e ( ) method that returns the name. The
simplest thing that will make this test pass is:
p ub l ic S tri n g ge t Name ( ) {
r e tu rn "S t ar W a rs";
}

Green bar. Now we have to clean up by refactoring to remove duplication. The duplication is the string
literal" S ta r W ar s". It's being passed into the constructor and returned by g e t N a m e ( ) . To remove
the duplication we can store the value that is passed in and return that:
p ub l ic c las s Movi e {
p r iv at e S t ring name ;
p u bl ic Mo v ie(S t ring aNam e) {
na me = a Name ;
}

p u bl ic St r ing g etNa m e() {


re tu rn n ame;
}

Green bar, still. We're finished with the tweak to M o v i e . Let's have another look at the GUI again.
Same thing. We need a way for the list box to get the name from each M o v i e . We could write a
customer renderer, or we could add a t o S t r i n g ( ) method. Guess what's simpler? You got it, we'll go
with t oSt ri ng() . But first a test:
p ub l ic v oid test T oStr i ng() {
M o vi e sta r Wars = ne w Mov ie(" S t a r W a r s " ) ;
a s se rt Equ a ls(" s tarW a rs s houl d h a v e t o S t r i n g o f \ " S t a r W a r s \ " . " ,
" S tar W ars" ,
s t arWa r s.to Stri n g ( ) ) ;
}

Sniff, sniff. I smell some duplication. Take a moment to refactor the s t a r W a r s creation into s e t U p ( ) .
The result is:
p ub l ic c las s Test M ovie exte nds T e s t C a s e {
p r iv at e M o vie s tarW a rs = nul l ;
p r ot ec ted void setU p () {
st ar War s = n e w Mo v ie(" Star W a r s " ) ;
}
p u bl ic vo i d te s tMov i eNam e() {
as se rtE q uals ( "sta r Wars sho u l d h a v e n a m e \ " S t a r W a r s \ " . " ,
"Sta r War s",
star W ars. getN a m e ( ) ) ;
}
p u bl ic vo i d te s tToS t ring () {
as se rtE q uals ( "sta r Wars sho u l d h a v e t o S t r i n g o f \ " S t a r W a r s \ " . " ,
"Sta r War s",
star W ars. toSt r i n g ( ) ) ;
}
p u bl ic st a tic v oid m ain( Stri n g [ ] a r g s ) {
ju ni t.t e xtui . Test R unne r.ru n ( T e s t M o v i e . c l a s s ) ;
}
}

The two tests look a lot alike, but they do test different methods, and t o S t r i n g ( ) may not always be
the same as getN a me( ) . YAGNI (You Ain't Gonna Need It)? My choice is to leave it as is.
Anyway, we go on to implement Mov ie . t o S t r i n g ( ) to get to the green bar:
p ub l ic S tri n g to S trin g () {
r e tu rn na m e;
}

This is very similar to g e tName . Should we refactor and have t o S t r i n g ( ) delegate to g e t N a m e ( ) ?


Maybe. For now I won't. In time we may not even need t o S t r i n g ( ) .
How's the GUI look now? Have a look at Figure 10.2. That's more like it. Now we're done. And that
finishes off the task. High five! Snack time!

Figure 10.2. Working movie list application GUI.

Amazon

Prev

don't be afraid of buying books

Next

ADD A MOVIE IN THE GUI


Provide a text field and "Add" button for adding movies to the list via the GUI.
Test 7. When the logical layer is asked to add a movie, it should request the required data
from the view and update the movie list to include a new movie based on the data provided.

Test 8. The GUI should have a field for the movie name and an add button. It should answer
the contents of the name field when asked, and request that the logical layer add a movie
when the add button is pushed.

To add a movie to the list, we go back to our humble dialog approach and revisit T es t GU I.

Test 7: When the logical layer is asked to add a movie, it should request
the required data from the view and update the movie list to include a new
movie based on the data provided
Here's the test for adding a movie:
pu bl ic v oid t est Ad din g( ) {
St rin g LO ST IN S PAC E = "L ost In S p a ce" ;
Mo vie los tI nSp ac e = n ew M ovie (L OS T IN S P ACE ) ;
Ve cto r mo vi esW it hAd di tion = n ew V e c tor ( m ovi e s) ;
mo vie s Wit hA ddi ti on. ad d(lo stIn Sp ac e ) ;
co ntr o l = E asy Mo ck. co ntro lFor (M ov i e Lis t E dit o rV i ew .c l as s );
mo ckV i ew = (Mo vi eLi st Edit orVi ew )c o n tro l . get M oc k () ;
mo ckV i ew. se tMo vi es( mo vies );
co ntr o l.s et Voi dC all ab le(1 );
mo ckV i ew. ge tNe wN ame () ;
co ntr o l.s et Ret ur nVa lu e(LO ST I N SP A C E, 1 ) ;

mo ckV i ew. se tMo vi es( mo vies With Ad di t i on) ;


co ntr o l.s et Voi dC all ab le(1 );

co ntr o l.a ct iva te ();


Mo vie L ist Ed ito r edi to r = new Mo vi e L ist E d ito r (m o vi eL i st , mo ck V ie w) ;
ed ito r .ad d( );
}

co ntr o l.v er ify () ;

Before this will compile we need to add g et Ne w N ame ( ) to M o vi e Li st E di t or Vi e w:


pu bl ic i nte rf ace M ovi eL istE dito rV ie w {
vo id s etM ov ies (V ect or mov ies) ;
St rin g ge tN ewN am e() ;
}

Compile, run, red bar.


Before we turn to making the test work, notice the duplication in setting up the control and mock between
this and our previous test. Let's start by refactoring that into se t Up () . We won't show the updated code
here just for that change... watch for it later.
Now let's get back to our green bar. The failure we're getting is an EasyMock expectation failure on verify
(i.e., found by the v er ify () call):
me th od c all g etN ew Nam e( ):
c all s exp ec ted: 1,
r ece iv ed: 0
me th od c all s etM ov ies ([ Star War s,
Star Tre k,
Star gate ,
Lost In Sp ac e ] ):
c all s exp ec ted: 1,
r ece iv ed: 0

Looking at the te st Add in g() method we can see that the first expected call to set the initial movie list
was satisfied, but that the expectations to get the name of the movie to add and to set the larger movie list
were not satisfied. Let's take them one at a time.
We'll start with the expected call to g etNew N ame . The task says to add a text field in which the user can
enter the name of the movie to be added. So in the a d d () method of M o vi e Li st E di to r we need to get
the contents of that field using getN ewNam e . To do this we need access to the M o vi eL i st Ed i to rV i ew
that was passed into the constructor. This means we need to store it in an instance variable. So
Mo vi eLi s tEd it or is now:
pu bl ic c las s Mov ie Lis tE dito r {
pr iva t e M ov ieL is tEd it orVi ew v ie w;
pu bli c Mo vi eLi st Edi to r(Mo vieL is t m o vie L i st, Mo v ie Li s tE d it or V ie w a Vi ew ) {
vie w = aV iew ;
vie w .se tM ovi es (ne w Vect or(m ov ie L i st. g e tMo v ie s () )) ;
}

pu bli c vo id ad d( ) {
vie w .ge tN ewN am e() ;
}

That satisfies the expected call to getN ewNa m e (). Now to set the new list value. Notice that
ge tN ewN a me( ) is set up to return the name of the movie to be added. If we use that returned value to
create a new Mo vie which we add to the list, which we then pass to the view with s e tM ov i es () , we just
might get to the green bar again! Let's try it:
pu bl ic v oid a dd( ) {
Mo vie n ewM ov ie = new M ovie (vie w. ge t N ewN a m e() ) ;
}

OK, we need the Mo vie Li st that was passed to the constructor. We'll have to save it somewhere:
pu bl ic M ovi eL ist Ed ito r( Movi eLis t mo v i eLi s t , M o vi e Li st E di t or Vi e w aV i ew ) {
mo vie s = mo vie Li st;
vi ew = aV ie w;
vi ew. s etM ov ies (n ew Ve ctor (mov ie s. g e tMo v i es( ) )) ;
}

Now we can use m ovi es in a dd() :


pu bl ic v oid a dd( ) {
Mo vie n ewM ov ie = new M ovie (vie w. ge t N ewN a m e() ) ;
mo vie s .ad d( new Mo vie );
vi ew. s etM ov ies (n ew Ve ctor (mov ie s. g e tMo v i es( ) )) ;
}

That should do it. Let's see. Red bar!?! OK, what's the failure?
Un ex pec t ed me tho d cal l setM ovie s( [S t a r W a r s,
St a r Tr e k ,
St a r gat e ,
Lo s t In S pac e ])

Hmm. . . wasn't it just failing because that call wasn't being made? Now we're making it and it's complaining
that the call wasn't expected. Consider what's happening under the covers. If we dig through the EasyMock
and MockObjects code, we find that the expected and actual arguments to the expected method calls are
compared using e qu als () . Our argument is an instance of Ve ct or and Ve c to r. e qu al s () compares
elements pairwise using eq uals (). What does e q ual s ( ) mean for M ov i e? We haven't defined
Mo vi e.e q ual s( ) so it's using what it inherits from O b j ect , which considers two objects equal if they are
the same physical object.
Looking at our code we see that we created a Mov i e in the test and another in M ov ie L is tE d it or . ad d( )
which are getting compared. So, there's the problem. We just need to add an eq ua l s( ) method to M ov ie .
As always, we start with a test, this one in T e stM o v ie:
pu bl ic v oid t est Eq ual s( ) {

fi nal Mov ie a = new


fi nal Mov ie b = new
fi nal Mov ie c = new
fi nal Mov ie d = new
ne w E q ual sT est er (a,

M ovie ("St ar
M ovie ("St ar
M ovie ("St ar
M ovie ("St ar
b ,c,d );

W a r s") ;
W a r s") ;
T r e k") ;
W a r s") { };

Here are the e qu als () and has hCod e() methods:


pu bl ic b ool ea n e qu als (O bjec t o) {
if (o == th is) r etu rn tru e;
if (o == nu ll) r etu rn fal se;
if (o . get Cl ass () != t his. getC la ss ( ) ) r e t urn fa l se ;
Mo vie oth er Mov ie = (M ovie )o;
re tur n na me .eq ua ls( ot herM ovie .n am e ) ;
}
pu bl ic i nt ha shC od e() {
re tur n na me .ha sh Cod e( );
}

OK. That finishes off the logic part of the interface change. Now we'll turn our attention to the Swing-based
view and Te s tSw in gMo vi eLis tEdi torVi e w. Before we start we need to add a stub for the method we
added to the interface:
pu bl ic S tri ng ge tN ewN am e() {
re tur n "" ;
}

Test 8: The GUI should have a field for the movie name and an add button.
It should answer the contents of the name field when asked, and request
that the logical layer add a movie when the add button is pushed
We begin by writing a test for the add operation:
pu bl ic v oid t est Ad din g( ) {
St rin g LO ST _IN _S PAC E = "L ost In S p a ce" ;
Mo vie los tI nSp ac e = n ew M ovie (L OS T _ IN_ S P ACE ) ;
mo vie s .ad d( los tI nSp ac e);
ma inW i ndo w = n ew JF ra meOp erat or (" M o vie L ist " );
Mo vie L ist Ed ito r edi to r =
new Mov ie Lis tE dit or (mov ieLi st ,
(Swi ngMo vi eL i s tEd i t orV i ew ) ma in W in d ow .g e tW in d ow () ) ;

JT ext F iel dO per at or ne wMov ieFi el d = new J Tex t Fi e ld Op e ra t or (m a in Wi n do w) ;


ne wMo v ieF ie ld. en ter Te xt(L OST_ IN _S P A CE) ;
JB utt o nOp er ato ra ddB ut ton = ne w JB u t ton O p era t or ( ma in W in d ow , " Ad d" ) ;
ad dBu t ton .d oCl ic k() ;

JL ist O per at or mo vie Li st = new J Li s t Ope r a tor ( ma i nW in d ow ) ;


Li stM o del l ist Mo del = mov ieLi st .g e t Mod e l ();
as ser t Equ al s(" Mo vie l ist is t he w r o ng s i ze" , mo v ie s. s iz e () , l is tM o de l. g et Si z e( )) ;
fo r ( i nt i = 0 ; i < m ovie s.si ze () ; i++ ) {
ass e rtE qu als (" Mov ie lis t co nt ai n s ba d mov i e a t in d ex " +i ,
mov ie s.ge t(i) ,
lis tM odel .get El em e n tAt ( i ));
}

When we run this we get a red bar. There's no JT e x tFi e l d component in the interface yet, so ne w
JT ex tFi e ldO pe rat or (ma in Wind ow) is failing. We need to add the text field in the i ni t( ) method:
JT ex tFi e ld mo vie Fi eld = new JTe xt Fi e l d(1 6 ) ;
ge tC ont e ntP an e() .a dd( mo vieF ield );

Now our test is failing because the button can't be found. We need to add that to the interface (don't forget,
it's found by name):
JB ut ton add Bu tto n = n ew JBu tton (" Ad d " );
ge tC ont e ntP an e() .a dd( ad dBut ton) ;

Compile and run again. Still a red bar:


Mo vi e l i st is th e wro ng siz e ex pe ct e d :<4 > but wa s :< 3>

Structurally, the components we need are there. Now we just need to hook the add button to the a dd ( )
method of M o vie Li stE di tor. Adding an Ac t i onL i s ten e r to the button is easy enough. The problem is
what should it do? We don't have an instance of Mo v i eLi s tE d it or . The way the code is now, we create a
view and hand that in to the M ovie List Ed it o r constuctor. We can add a call there to the supplied view in
order to tell it what editor instance it's hooked up to. To support that we'll need several things:
a method (let's call it setE dito r( )) in the Swing-based view
a matching method declaration in the view interface
an instance variable in the Swing-based view to store the editor
Here's the updated interface:
pu bl ic i nte rf ace M ovi eL istE dito rV ie w {
vo id s etM ov ies (V ect or mov ies) ;
St rin g ge tN ewN am e() ;
vo id s etE di tor (M ovi eL istE dito r an E d ito r ) ;
}

And here are the additions to Swi ngMo vieL i s tEd i t orV i ew :
pr iv ate Mov ie Lis tE dit or myE dito r = n u ll;
pu bl ic v oid s etE di tor (M ovie List Ed it o r an E d ito r ) {
my Edi t or = anE di tor ;
}

Now we can add the call to se tEdi tor( ) to the Mov i e Lis t Ed i to r constructor:
pu bl ic M ovi eL ist Ed ito r( Movi eLis t mo v i eLi s t , M o vi e Li st E di t or Vi e w aV i ew ) {
mo vie s = mo vie Li st;
vi ew = aV ie w;
vi ew. s etM ov ies (n ew Ve ctor (mov ie s. g e tMo v i es( ) )) ;
vi ew. s etE di tor (t his );
}

Now we can add the Act io nLis tene r to our add button:
ad dB utt o n.a dd Act io nLi st ener (new A ct i o nLi s t ene r () {
pu bli c vo id ac ti onP er form ed(A ct io n E ven t e) {
myE d ito r. add () ;
}
}) ;

One more thing remaining to get to the green bar. Recall that we had to stub ge t Ne wN a me () because we
had added it to the interface to support adding in M o v ieL i st E di to r . Now we need to make it real. In the
process we need to store the field component in an instance variable so that it is accessible:
pu bl ic S tri ng ge tN ewN am e() {
re tur n mo vi eFi el d.g et Text ();
}

Green bar! Time to clean up. The first thing that I see is that the Sw i ng M ov ie L is tE d it or V ie w. i ni t( )
method has gotten rather large. What do you think? It's doing several things:
setting the title
setting the layout manager
creating all the components

Let's use Extract Method to pull those different functions out, compiling and rerunning the tests after each
extraction. Here's the result:
pu bl ic v oid i nit () {
se tTi t le( );
se tLa y out () ;
in itL i st( );
in itF i eld () ;
in itA d dBu tt on( );
pa ck( ) ;
}
pr iv ate voi d set Ti tle () {
se tTi t le( "M ovi e Lis t" );
}
pr iv ate voi d set La you t( ) {
ge tCo n ten tP ane () .se tL ayou t(ne w Fl o w Lay o u t() ) ;
}
pr iv ate voi d ini tL ist () {
mo vie L ist = ne w JLi st (new Vec to r( ) ) ;
JS cro l lPa ne sc ro lle r = ne w JS cr ol l P ane ( m ovi e Li s t,
Sc ro llP an eCo ns tant s.VE RT IC A L SC R O LLB A R A LW AY S ,
Sc ro llP an eCo ns tant s.HO RI ZO N T AL S C ROL L BA R N EV E R) ;
ge tCo n ten tP ane () .ad d( scro ller );
}
pr iv ate voi d ini tF iel d( ) {
mo vie F iel d = n ew JT ex tFie ld(1 6) ;
ge tCo n ten tP ane () .ad d( movi eFie ld );
}
pr iv ate voi d ini tA ddB ut ton( ) {
JB utt o n a dd But to n = n ew J Butt on (" A d d") ;
ad dBu t ton .a ddA ct ion Li sten er(n ew A c t ion L i ste n er ( ) {
pub l ic vo id ac tio nP erfo rmed (A ct i o nEv e n t e ) {
m y Edi to r.a dd ();
}
}) ;
ge tCo n ten tP ane () .ad d( addB utto n) ;
}

Now that we have the Swing GUI working and refactored, we need to run all of our tests. Uh oh! The tests in
Te st GUI are failing! A quick look at the trace tells us that the mock view is getting an unexpected call to
se tE dit o r() . We need to add an expectation for this call that doesn't care what the argument is. We can
do this by adding the following to the end of T e s tGU I . set U p( ) :
mo ck Vie w .se tE dit or (nu ll );
co nt rol . set De fau lt Voi dC alla ble( );

There, rerunning the tests gives us a green bar again.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
In this chapter we've implemented the first storytaking the system from nothing to a list of named
movies with a way to add new movies, as well as a GUI. Some things we designed and built from the
start, while other things we didn't do until we had a need. There was no requirement to have an
equ a ls() method in M ov ie, but we needed it to support an argument expectation. No doubt we'll need
it at some point, but we didn't bother to consider it until we had an immediate, concrete need.
In addition to test-driving the core logic of the growing application, we implemented a simple GUI to
expose the functionality we built. This is an important point. It's imperative to develop the system in as
many wide-ranging slices as possible. Even after the first story you should have something to show.

Amazon

Prev

don't be afraid of buying books

Chapter 11. MOVIES CAN BE RENAMED


Provide a way to rename movies that are in the list.

Next

Amazon

Prev

don't be afraid of buying books

Next

SUPPORT MOVIE NAME EDITING


Allow the movie name to be changed.
So far we have the class Movi e that has a name. When we construct an instance, we supply a name
and we can retrieve the name with the g etN a m e() method. For this task we need a way to set the
name of a Mov ie after it has been constructed.
Now, a question comes to mind: "Can we have a movie without a name?" Ask the customer... "No,
every movie has a name." We need to capture that in a test!
Test 9. Changing the name of a movie results in it using the new name hereafter.

Test 10. A movie can't be constructed with a null name.

Test 11. A movie can't be constructed with an empty name.

Test 12. A movie can't be renamed to a null name.

Test 13. A movie can't be renamed to an empty name.

Before starting, run the tests to give us confidence. Green bar. Then let's go.

Test 9: Changing the name of a movie results in it using the new name
hereafter
As always, we start with a test, which we will add to Tes t Mo v ie :
pub l ic vo id te stR en amin g() {
S t ring new Na me = "Sta r Tr ek";
M o vie a Mov ie = ne w Mo vie( "Sta r War s " );
a M ovie. ren am e(n ew Name );
a s sertE qua ls ("R en amin g sh ould c han g e th e na me ." ,
new Na me,
aMo vi e.ge tNam e()) ;
}

Next, we get it to compile. We need to add a r e nam e ( ) method to Mo v ie :


pub l ic vo id re nam e( Stri ng n ewNa m e ) {
}

Everything compiles. Run the tests (we can just run Te s t Mo vi e at the moment). Red bar. Our new test
failed. Good. Now we add enough code to r e nam e ( ) to pass the test. Easy enough:
pub l ic vo id re nam e( Stri ng n ewNa m e ) {
n a me = new Na me;
}

Green bar.

Test 10: A movie can't be constructed with a null name


First, we'll add tests for the constructor to verify that it won't accept empty or null names. One test at a
time, first let's test with nul l. Constructing a M o v ie with a null name should throw an
Ill e galAr gum en tEx ce ptio n. If nothing is thrown, the test should fail. If an unexpected exception is
thrown, JUnit will notice and the test will fail. Here's the test:
pub l ic vo id te stN ul lNam e() {
try {
new M ovi e( nul l) ;
fail( "nu ll na me sho uld have t hro w n Il l e ga lA rg u me n tE xc e pt io n ." );
} catch (I ll ega lA rgum entE xcep t i on e x ) {
}
}

As expected, it fails. We need to add a check for a n u l l argument to the constructor:


pub l ic Mo vie (S tri ng aNa me) {
if (aNam e = = nul l) {
t hrow new I lle ga lArg umen tExc e p tio n ( "nu l l M ov ie na m e" );
}
na m e = a Nam e;
}

Test 11: A movie can't be constructed with an empty name

The next test is for constructing with an empty name. As before, we want an
Ill e galAr gum en tEx ce ptio n to be thrown:
pub l ic vo id te stE mp tyNa me() {
try {
new M ovi e( "") ;
fail( "em pt y n am e sh ould hav e thr o w n I l l eg al Ar g um e nt Ex c ep ti o n. ") ;
} catch (I ll ega lA rgum entE xcep t i on e x ) {
}
}

Compile, run, red. Add an empty name check:


pub l ic Mo vie (S tri ng aNa me) {
i f (aNa me == nu ll ) {
t hrow new I lle ga lArg umen tExc e p tio n ( "nu l l M ov ie na m e" );
}
i f (aNa me. le ngt h( ) == 0) {
t hrow new I lle ga lArg umen tExc e p tio n ( "em p t y Mo vi e n a me ") ;
}
n a me = aNa me ;
}

That takes care of the constructor.

Test 12: A movie can't be renamed to a null name


Now for our newly added renaming capability. Again, one test at a time:
pub l ic vo id te stN ul lRen ame( ) {
M o vie a Mov ie = ne w Mo vie( "Sta r War s " );
try {
aMovi e.r en ame (n ull) ;
fail( "nu ll re na me s houl d ha v e th r o wn I l le ga lA r gu m en tE x ce pt i on ." ) ;
} catch (I ll ega lA rgum entE xcep t i on e x ) {
}
}

Now get it to pass:


pub l ic vo id re nam e( Stri ng n ewNa m e ) {
i f (new Nam e == nu ll) {
throw ne w Ill eg alAr gume ntEx c e pti o n ("n u l l Mo vi e n a me ") ;
}
n a me = new Na me;
}

Green bar. Compare what we just did to what we did earlier to the constructor. The duplication jumps
out. Let's clean that up by extracting the common text for n ul l into a separate method and calling it
from both the constructor and re name( ) :
pub l ic Mo vie (S tri ng aNa me) {
c h eckNu ll( aN ame );
i f (aNa me. le ngt h( ) == 0) {
thro w n ew Il le galA rgum entE x c ept i o n(" e m pt y Mo v ie na me " );
}
n a me = aNa me ;
}

pub l ic vo id re nam e( Stri ng n ewNa m e ) {


c h eckNu ll( ne wNa me );
n a me = new Na me;
}
pri v ate v oid c hec kN ull( Stri ng n e w Nam e ) th r o ws I ll e ga l Ar gu m en tE x ce pt i on {
i f (new Nam e == nu ll) {
throw ne w Ill eg alAr gume ntEx c e pti o n ("n u l l Mo vi e n a me ") ;
}
}

Compile, test, green. Excellent! Next, the "renaming with an empty name" test:

Test 13: A movie can't be renamed to an empty name


pub l ic vo id te stE mp tyRe name () {
Mo v ie aM ovi e = n ew Mov ie(" Star W ars " ) ;
tr y {
a Movie .re na me( "" );
f ail(" emp ty re na me s houl d ha v e th r o wn I l le ga lA r gu m en tE x ce pt i on ." ) ;
} c atch (Il le gal Ar gume ntEx cept i o n e x ) {
}
}

And make it work:


pub l ic vo id re nam e( Stri ng n ewNa m e ) {
c h eckNu ll( ne wNa me );
i f (new Nam e. len gt h() == 0 ) {
thro w n ew Il le galA rgum entE x c ept i o n(" e m pt y Mo v ie na me " );
}
n a me = new Na me;
}

Again, we have duplication in the empty name check between the constructor and r e na me ( ).As we did
before, we will extract the empty name check into a separate method:
pub l ic Mo vie (S tri ng aNa me) {
c h eckNu ll( aN ame );

c h eckEm pty (a Nam e) ;


n a me = aNa me ;

pub l ic vo id re nam e( Stri ng n ewNa m e ) {


c h eckNu ll( ne wNa me );
c h eckEm pty (n ewN am e);
n a me = new Na me;
}

pri v ate v oid c hec kE mpty (Str ing n e wNa m e ) t h r ow s Il l eg a lA rg u me nt E xc ep t io n {


i f (new Nam e. len gt h() == 0 ) {
thro w n ew Il le galA rgum entE x c ept i o n(" e m pt y Mo v ie na me " );
}
}

OK. We're finished with that, and with this task.

Amazon

Prev

don't be afraid of buying books

Next

MOVIE RENAME GUI


On the GUI, selecting a movie fills in the text field with its name. An "Update" button will
update the movie name with the contents of the field.
Now we must make the newly added rename capability available on the GUI. The task calls for two
things:
1. selecting a movie will put its name in the text field
2. clicking an Update button that will cause the selected movie to be renamed with what is in the
text field.
Here are the required tests:
Test 14. Indicating, to the logical layer, that a selection is made from the list causes the
view to be given a value for the name field, that is, the selected movie's name.

Test 15. Selecting from the list causes the name field to be filled in with the selected
movie's name.

Test 16. When an update is requested, the selected movie is renamed to whatever is
answered by the view as the new name.

Test 17. When the update button is pushed, the selected movie is renamed to whatever
is in the name field.

Let's tackle them one at a time, in the order above. First, we need a test for filling in the text field with
the name of the selected movie. As before, we will work at the logic level first, then the GUI.

Test 14: Indicating, to the logical layer, that a selection is made from the
list causes the view to be given a value for the name field, that is, the
selected movie's name
p ub l ic v oid test S elec t ing( ) {
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Name ( "Sta r Tr e k " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
e d it or .se l ect( 1 );
}

c o nt ro l.v e rify ( );

We need to stub two methods to get this to compile. First, we need to add s e t N e w N a m e ( ) to
M ov i eL is tEd i torV i ew:
v oi d s et New N ame( S trin g str ing) ;

Second, we need to add selec t() to M o v i e L i s t E d i t o r :


p ub l ic v oid sele c t(in t i) {
}

Now it compiles and fails: the expected call to s e t N e w N a m e ( ) didn't happen. That needs to happen in
response to calling se l ect( ) on the M o v i e L i s t E d i t o r . So we add enough to s e l e c t ( ) to get the
test to pass:
p ub l ic v oid sele c t(in t i) {
v i ew .s etN e wNam e ("St a r Tr ek") ;
}

Green bar! But look at that string literal. Yuck! That duplicates the name of the second movie in the list
we set up. Let's get it from there:
p ub l ic v oid sele c t(in t i) {
v i ew .s etN e wNam e (mov i es.g etMo v i e ( i ) . g e t N a m e ( ) ) ;
}

In order to get this to compile we need to add g e t M o v i e ( ) to M o v i e L i s t :


p ub l ic M ovi e get M ovie ( int i) {
r e tu rn nu l l;
}

Oops! That breaks our test. But it's the n u l l that g e t M o v i e ( ) is returning that is causing the problem.
We'll have to expand on that:
p ub l ic M ovi e get M ovie ( int i) {
r e tu rn (M o vie) movi e s.ge t(i) ;
}

OK, our test is working again. Just to be sure, let's add to our test. We'll make it select another movie
in the list and make sure that that name is sent to the view:
p ub l ic v oid test S elec t ing( ) {
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Name ( "Sta r Tr e k " ) ;

c o nt ro l.s e tVoi d Call a ble( 1);

m o ck Vi ew. s etNe w Name ( "Sta r Wa r s " ) ;


c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
e d it or .se l ect( 1 );
e d it or .se l ect( 0 );
}

c o nt ro l.v e rify ( );

Compile, run, green bar. That verifies that the work we just did really does work for the general case.
Now we're done with the logical layer; time to turn our attention to the GUI.

Test 15: Selecting from the list causes the name field to be filled in with
the selected movie's name
This will be fairly simple since it doesn't involve adding additional components. Here's the test (in
T es t GU I):
p ub l ic v oid test S elec t ing( ) {
m a in Wi ndo w = n e w JF r ameO pera t o r ( " M o v i e L i s t " ) ;
M o vi eL ist E dito r edi t or =
ne w Mov i eLis t Edit o r(mo vieL i s t ,
( S wing Movi e L i s t E d i t o r V i e w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
m o vi eL ist . clic k OnIt e m(1, 1);

J T ex tF iel d Oper a tor n ewMo vieF i e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ) ;


a s se rt Equ a ls(" w rong text fro m s e l e c t i o n . " ,
" S tar T rek" ,
n e wMov i eFie ld.g e t T e x t ( ) ) ;

First, we need to add s etNe w Name () to S w i n g M o v i e L i s t E d i t o r V i e w to conform to the change in


the interface M ovie L istE d itor View:
p ub l ic v oid setN e wNam e (Str ing n e w N a m e ) {
}

Now it compiles, and fails. We need to add some code to hook up list selection to the underlying logic
(while we're there and dealing with selection, we'll set the selection mode):
p ri v at e voi d ini t List ( ) {
m o vi eL ist = ne w JLi s t(ne w Ve c t o r ( ) ) ;
m o vi eL ist . setS e lect i onMo de(L i s t S e l e c t i o n M o d e l . S I N G L E S E L E C T I O N ) ;
m o vi eL ist . addL i stSe l ecti onLi s t e n e r ( n e w L i s t S e l e c t i o n L i s t e n e r ( ) {
pu bl ic v oid v alue C hang ed(L i s t S e l e c t i o n E v e n t e ) {
my Edi t or.s e lect ( movi eLis t . g e t S e l e c t e d I n d e x ( ) ) ;
}
});

J S cr ol lPa n e sc r olle r = n ew J S c r o l l P a n e ( m o v i e L i s t ,
Sc r ollP a neCo n stan ts.V E R T I C A L S C R O L L B A R A L W A Y S ,
Sc r ollP a neCo n stan ts.H O R I Z O N T A L S C R O L L B A R N E V E R ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;

Compile, run, red bar. One more thing to do. We need to fill in the s e t N e w N a m e method so that it puts
its argument into the text field:
p ub l ic v oid setN e wNam e (Str ing n e w N a m e ) {
m o vi eF iel d .set T ext( n ewNa me);
}

Compile, run, green bar! Now we need to add the Update button.

Test 16: When an update is requested, the selected movie is renamed to


whatever is answered by the view as the new name
We will start back at the logic layer with a test:
p ub l ic v oid test U pdat i ng() {
V e ct or ne w Movi e s = n ew V ecto r ( ) ;
n e wM ov ies . add( s tarW a rs);
n e wM ov ies . add( n ew M o vie( "Sta r T r e k I " ) ) ;
n e wM ov ies . add( s targ a te);
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Name ( "Sta r Tr e k " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. g etNe w Name ( );
c o nt ro l.s e tRet u rnVa l ue(" Star T r e k I " , 1 ) ;
m o ck Vi ew. s etMo v ies( n ewMo vies ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();

M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;


e d it or .se l ect( 1 );
e d it or .up d ate( ) ;
}

c o nt ro l.v e rify ( );

To compile we need to add a stub to M o v i e L i s t E d i t o r :


p ub l ic v oid upda t e() {
}

The test fails due to the expected calls to g e t N e w N a m e ( ) and s e t M o v i e s ( ) not happening. This is
hardly surprising since u pdat e () does nothing. Let's add some code to it:

p ub l ic v oid upda t e() {


i f ( se lec t edMo v ie ! = nul l) {
se le cte d Movi e .ren a me(v iew. g e t N e w N a m e ( ) ) ;
vi ew .se t Movi e s(ne w Vec tor( m o v i e s . g e t M o v i e s ( ) ) ) ;
}
}

We haven't defined sel e ctedM ovie yet. We can just grab the M o v i e whose name we fetch in s e l e c t
(keeping in mind that we will get calls of s e l e c t ( - 1 ) due to the way J L i s t works):
p ri v at e Mov i e se l ecte d Movi e;
p ub l ic v oid sele c t(in t i) {
i f ( i == - 1) {
se le cte d Movi e = n u ll;
} el se {
s el ec ted M ovie = mo v ies. getM o v i e ( i ) ;
v ie w. set N ewNa m e(se l ecte dMov i e . g e t N a m e ( ) ) ;
}
}

You will find that there is a significant amount of code that you will
write in GUI classes that isn't strictly test-driven. This is, in my
opinion, an unavoidable aspect of developing a GUI. The code I'm
referring to is the framework-related details. An example here is
handling list selections with index -1 denoting the clearing of all
selection, dealing with layout managers, and other visual aspects of
the interface. These things are the subject of other types of testing
that don't relate strictly to the functional aspects of the application. At
the functional level, all we really care about is that the required
components are present and that they operate as required. Once the
tests are in place, you can tweak the visual aspects, confident that
you are not breaking the functionality. It's a lot like optimization: get
it right, then make it look good.

Now it compiles, and we get a green bar!

Test 17: When the update button is pushed, the selected movie is
renamed to whatever is in the name field
Now we can move on to the Swing layer. This will involve adding a new button for updating. Here's the
test (notice the click on the 0th list item to reset the text field):
p ub l ic v oid test U pdat i ng() {
m a in Wi ndo w = n e w JF r ameO pera t o r ( " M o v i e L i s t " ) ;
M o vi eL ist E dito r edi t or =
n ew Mo v ieLi s tEdi t or(m ovie L i s t ,
(Sw i ngMo v ieLi s tEdi torV i e w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
m o vi eL ist . clic k OnIt e m(1, 1);
J T ex tF iel d Oper a tor n ewMo vieF i e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ) ;
n e wM ov ieF i eld. e nter T ext( "Sta r T r e k I " ) ;
J B ut to nOp e rato r upd a teBu tton = n e w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " U p d a t e " ) ;
u p da te But t on.d o Clic k ();
m o vi eL ist . clic k OnIt e m(0, 1);

m o vi eL ist . clic k OnIt e m(1, 1);


a s se rt Equ a ls(" M ovie shou ld h a v e b e e n r e n a m e d . " ,
" S tar T rek I",
n e wMov i eFie ld.g e t T e x t ( ) ) ;

This compiles, but fails, because there isn't an update button in the GUI yet. We add it:
p ub l ic v oid init ( ) {
s e tT it le( ) ;
s e tL ay out ( );
i n it Li st( ) ;
i n it Fi eld ( );
i n it Ad dBu t ton( ) ;
i n it Up dat e Butt o n();
p a ck () ;
}
p ri v at e voi d ini t Upda t eBut ton( ) {
J B ut to n u p date B utto n = n ew J B u t t o n ( " U p d a t e " ) ;
g e tC on ten t Pane ( ).ad d (upd ateB u t t o n ) ;
}

Now the test fails due to an assertion failure: the name isn't being changed. We next need to hook up
the update button to the underlying editor:
u pd a te Bu tto n .add A ctio n List ener ( n e w A c t i o n L i s t e n e r ( ) {
p u bl ic vo i d ac t ionP e rfor med( A c t i o n E v e n t e ) {
my Ed ito r .upd a te() ;
}
} );

Green bar! Now have a look. Is there anything that needs to be cleaned up?
In M o v ie Li stE di tor there is some duplication between methods that update the movie list on the
interface, specifically, the line:
v ie w .s et Mov i es(n e w Ve c tor( movi e s . g e t M o v i e s ( ) ) ) ;

This appears in three different methods; time to refactor, to be sure. We'll use Extract Method to put it
in its own method:
p ri v at e voi d upd a teMo v ieLi st() {
v i ew .s etM o vies ( new V ecto r(mo v i e s . g e t M o v i e s ( ) ) ) ;
}

In each method where that line occurred we'll replace it by a call to u p d a t e - M o v i e L i s t ( ) ;for
example:
p ub l ic v oid upda t e() {
i f ( se lec t edMo v ie ! = nul l) {
se le cte d Movi e .ren a me(v iew. g e t N e w N a m e ( ) ) ;
up da teM o vieL i st() ;
}
}

One last thing we should do is add a ma i n ( ) method to our Swing GUI class so that we can run our
application stand-alone:
p ub l ic s tat i c vo i d ma i n(St ring [ ] a r g s ) {

S w in gM ovi e List E dito r View win d o w = n e w S w i n g M o v i e L i s t E d i t o r V i e w ( ) ;


w i nd ow .in i t();
w i nd ow .sh o w();
M o vi eL ist list = ne w Mov ieLi s t ( ) ;
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( l i s t , w i n d o w ) ;

Now there's some duplication between this method and s t a r t ( ) in the same class:
p ub l ic s tat i c vo i d st a rt() {
S w in gM ovi e List E dito r View win d o w = n e w S w i n g M o v i e L i s t E d i t o r V i e w ( ) ;
w i nd ow .in i t();
w i nd ow .sh o w();
}

Since, in this case, all of the code in st a r t ( ) is included in m a i n ( ) ,we can replace those lines in
m ai n () with a call to star t () if we have s t a r t ( ) return the window it creates:
p ub l ic s tat i c Sw i ngMo v ieLi stEd i t o r V i e w s t a r t ( ) {
S w in gM ovi e List E dito r View win d o w = n e w S w i n g M o v i e L i s t E d i t o r V i e w ( ) ;
w i nd ow .in i t();
w i nd ow .sh o w();
r e tu rn wi n dow;
}
p ub l ic s tat i c vo i d ma i n(St ring [ ] a r g s ) {
S w in gM ovi e List E dito r View win d o w = S w i n g M o v i e L i s t E d i t o r V i e w . s t a r t ( ) ;
M o vi eL ist list = ne w Mov ieLi s t ( ) ;
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( l i s t , w i n d o w ) ;
}

Compile, run all the tests, green bar. No worries!

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
In this chapter we've added a small bit of functionality to the system: the ability to rename a movie.
This extends to the GUI where we can now select a movie in the list, edit its name, and update it.

Amazon

Prev

don't be afraid of buying books

Chapter 12. MOVIES ARE UNIQUE


A movie appears only once in the list; the names are unique and case-insensitive.
Attempts to add a duplicate should fail and result in an error.
Now that we're getting the hang of this, we'll pick up the pace a bit.

Next

Amazon

Prev

don't be afraid of buying books

Next

MOVIES ARE UNIQUE


Enforce uniqueness of movies during add and update operations. Attempts to add or
rename with the same name as an existing movie should raise an exception.
This task adds a bit of error detection/prevention functionality to what we did in the last chapter: it
disallows movies with duplicate names.
Test 18. Attempting to add a duplicate movie throws an exception and leaves the list
unchanged.

Test 19. Asking the movie list to rename a movie results in its name being changed.

Test 20. Asking the logical layer to add a duplicate movie causes it to inform the
presentation layer that the operation would result in a duplicate.

Test 21. Asking the logical layer to update a movie that would result in a duplicate
causes it to inform the presentation layer that the operation would result in a duplicate.

Test 22. Trying to add a movie that is the same as one in the list results in the display
of a "Duplicate Movie" error dialog.

Test 23. Trying to rename a movie to the name of one in the list results in the display
of a "Duplicate Movie" error dialog.

First, we'll tackle adding a new Mov ie that is the same as one in the list.

Test 18: Attempting to add a duplicate movie throws an exception and


leaves the list unchanged
The test we're about to write needs to start with a populated list. OK, that should go into the fixture. All
the T es tC ase s we have so far have a sepcific fixture that serves their tests. The biggest fixture has
two movies in it. Is this enough? I'd like something a bit bigger... say, three movies. Should we try to
do this with T e stMo v ieLi s tWit hTwoM o v i e s , or should we create a new T e s t C a s e (aka fixture)?
When in doubt, create a new fixture.
Here's the test and new fixture:
p ub l ic c las s Tes t Movi e List With P o p u l a t e d L i s t e x t e n d s T e s t C a s e {

p r iv at e
p r iv at e
p r iv at e
p r iv at e

M o vieL i st m o vieL ist = n u l l ;


M o vie s tarW a rs = nul l ;
M o vie s tarT r ek = nul l ;
M o vie s targ a te = nul l ;

p r ot ec ted void setU p () t hrow s D u p l i c a t e M o v i e E x c e p t i o n {


st ar War s = n e w Mo v ie(" Star W a r s " ) ;
st ar Tre k = n e w Mo v ie(" Star T r e k " ) ;
st ar gat e = n e w Mo v ie(" Star g a t e " ) ;

mo vi eLi s t = n ewMo v ieLi st() ;


mo vi eLi s t.ad d (sta r Wars );
mo vi eLi s t.ad d (sta r Trek );
mo vi eLi s t.ad d (sta r gate );

p u bl ic vo i d te s tAdd i ngDu plic a t e ( ) t h r o w s D u p l i c a t e M o v i e E x c e p t i o n {


mo vi eLi s t.ad d (sta r Wars );
mo vi eLi s t.ad d (sta r Trek );
Mo vi e d u plic a te = new Movi e ( s t a r T r e k . g e t N a m e ( ) ) ;
tr y {
mo vie L ist. a dd(d u plic ate) ;
fa il( " Addi n g du p lica te M o v i e s h o u l d t h r o w D u p l i c a t e M o v i e E x c e p t i o n " ) ;
} ca tch (Dup l icat e Movi eExc e p t i o n e x ) {
as ser t Equa l s("F a iled add o f d u p l i c a t e s h o u l d n ' t c h a g e t h e l i s t s i z e . " ,
2,
m o vieL ist. s i z e ( ) ) ;
}
}

To compile this test, we need to create a class, D u p l i c a t e M o v i e E x c e p t i o n , as well as indicate that


M ov i eL is t.a d d() throws this new exception. This, in turn, requires that everything that calls
M ov i eL is t.a d d() has to be updated to reflect the fact that a D u p l i c a t e M o v i e E x c e p t i o n could be
thrown.
First, we verify that the test fails. It doesgood. Here's the extended M o v i e - L i s t . a d d ( ) method that
lets the test pass:
p ub l ic v oid add( M ovie movi eToA d d ) t h r o w s D u p l i c a t e M o v i e E x c e p t i o n {
i f ( th is. c onta i ns(m o vieT oAdd ) ) {
th ro w n e w Du p lica t eMov ieEx c e p t i o n ( m o v i e T o A d d . g e t N a m e ( ) ) ;
}
m o vi es .ad d (mov i eToA d d);
}

Test 19: Asking the movie list to rename a movie results in its name
being changed
Next, we'll move to renaming. It's a bit different because renaming was written with the focus on a
specific M ovi e in isolation. Now we are considering renaming in the context of a M o v i e L i s t . So, we
need to work at the M ovie L ist level. That will require some sort of rename method in that class. But
we can't do that without a test requiring it. We now need to digress for a short while to take care of
that:
p ub l ic v oid test R enam i ng() {
f i na l Str i ng n e wNam e = " Star T r e k I " ;
m o vi eL ist . rena m e(st a rTre k, n e w N a m e ) ;
a s se rt Equ a ls(" n ame s houl d be d i f f e r e n t a f t e r r e n a m i n g " ,
n e wNam e ,
s t arTr e k.ge tNam e ( ) ) ;
}

To get this to pass, we need to add re n a m e ( ) to M o v i e L i s t :

p ub l ic v oid rena m e(Mo v ie a Movi e , S t r i n g n e w N a m e ) {


a M ov ie .re n ame( n ewNa m e);
}

OK, now we're ready to add the test for renaming a duplicate:
p ub l ic v oid test R enam i ngDu plic a t e ( ) {
try {
mo vi eLi s t.re n ame( s tarT rek, " S t a r W a r s " ) ;
fa il ("R e nami n g to a du plic a t e s h o u l d t h r o w D u p l i c a t e M o v i e E x c e p t i o n " ) ;
} ca tc h ( D upli c ateM o vieE xcep t i o n e x ) {
as se rtE q uals ( "Fai l ed r enam e s h o u l d n ' t c h a n g e l i s t s i z e . " ,
3,
movi e List .siz e ( ) ) ;

as se rtE q uals ( "Fai l ed r enam e s h o u l d n ' t c h a n g e t h e n a m e . " ,


" Star Trek ",
s tarT r ek.g etNa m e ( ) ) ;

This test requires that Movi e List .ren a m e ( ) be marked as throwing D u p l i c a t e M o v i e - E x c e p t i o n .


When we do that, this test compiles, but our previous one won't. We need to take that t h r o w s clause
into account. The simplest thing to do is ripple it and let JUnit handle it as an error. That's exactly what
we would want to happen.
Now everything compiles and we have a red bar! The failure is because the rename is succeeding when
it shouldn't. We need to add code to Mo v i e L i s t . r e n a m e ( ) to throw a D u p l i c a t e M o v i e E x c e p t i o n
when a rename would result in a duplicate movie:
p ub l ic v oid rena m e(Mo v ie a Movi e , S t r i n g n e w N a m e )
thro w s Du p lica teMo v i e E x c e p t i o n {
M o vi e pot e ntia l Movi e = n ew M o v i e ( a M o v i e ) ;
p o te nt ial M ovie . rena m e(ne wNam e ) ;
i f ( th is. c onta i ns(p o tent ialM o v i e ) ) {
th ro w n e w Du p lica t eMov ieEx c e p t i o n ( n e w N a m e ) ;
}
a M ov ie .re n ame( n ewNa m e);
}

The approach we've used is to create a copy of the movie to be renamed, rename it, and check for the
result in the Movi e Lis t . To do that, we need to add a copy constructor to M o v i e . But first we need a
test (in Te stM o vie):
p ub l ic v oid test C opyC o nstr ucto r ( ) {
M o vi e cop y OfSt a rWar s = n ew M o v i e ( s t a r W a r s ) ;
a s se rt Not S ame( " A co p y sh ould n o t b e t h e s a m e a s t h e o r i g i n a l . " ,
s t arWa r s,
c o pyOf S tarW ars) ;

a s se rt Equ a ls(" A cop y sho uld b e e q u a l t o t h e o r i g i n a l . " ,


s t arWa r s,
c o pyOf S tarW ars) ;

And now we'll add the copy constructor:


p ub l ic M ovi e (Mov i e or i gina l) {
n a me = or i gina l .nam e ;
}

Something interesting happened. In Tes t M o v i e . t e s t N u l l N a m e ( ) the constructor call, n e w


M ov i e( nu ll) ,no longer compiles since it's ambiguous. It could be a reference to either constructor. It
needs to be tweaked to attach type information to the n u l l :
p ub l ic v oid test N ullN a me() {
S t ri ng nu l lStr i ng = null ;
try {
ne w Mov i e(nu l lStr i ng);
fa il ("n u ll n a me s h ould hav e t h r o w n I l l e g a l A r g u m e n t E x c e p t i o n . " ) ;
} ca tc h ( I lleg a lArg u ment Exce p t i o n e x ) {
}
}

OK, everything compiles and te stCo py C o n s t r u c t o r ( ) passes. We have a copy constructor in place
for M o vi e now, so we can go back to our renaming test. Green bar there as well. Run
A ll T es ts ...green bar.
We should revisit the GUI rename code we wrote earlier and bring it up to spec in regards to routing the
renaming through M ovi e List rather than calling M o v i e . r e n a m e ( ) directly. All that needs to be
changed is Mo v ieLi s tEdi t or.u pdate ( ) . Because we have tests in place, we can make changes and
immediately see if they work. Here's the updated method:
p ub l ic v oid upda t e() {
i f ( se lec t edMo v ie ! = nul l) {
mo vi es. r enam e (sel e cted Movi e , v i e w . g e t N e w N a m e ( ) ) ;
up da teM o vieL i st() ;
}
}

This won't compile until we deal with the D u p l i c a t e M o v i e E x c e p t i o n that can be thrown by
M ov i eL is t.r e name ( ). Because there is no test to require anything more advanced, we'll just ignore
it:
p ub l ic v oid upda t e() {
i f ( se lec t edMo v ie ! = nul l) {
tr y {
mo vie s .ren a me(s e lect edMo v i e , v i e w . g e t N e w N a m e ( ) ) ;
up dat e Movi e List ( );
} ca tch (Dup l icat e Movi eExc e p t i o n e ) {
}
}
}

OK, we're ready to move on to the next task.

Amazon

Prev

don't be afraid of buying books

Next

ERROR MESSAGE ON NON-UNIQUENESS


The GUI provides an error message dialog when a non-unique add or rename is attempted.
Now we need to percolate the rename would cause a duplicate error up through the GUI. As usual, we will
start at the logic layer.

Test 20: Asking the logical layer to add a duplicate movie causes it to
inform the presentation layer that the operation would result in a duplicate
We need to add a test for a duplicate addition:
p u bl i c vo id tes t Dup l icate Caus in g A d d ( ) {
mo c k Vie w. setM o vie s (movi es);
co n t rol .s etVo i dCa l lable (1);
mo c k Vie w. getN e wNa m e();
co n t rol .s etRe t urn V alue( "Sta r W a r s " , 1 ) ;

mo c k Vie w. dupl i cat e Excep tion (" S t a r W a r s " ) ;


co n t rol .s etVo i dCa l lable (1);
co n t rol .a ctiv a te( ) ;
Mo v i eLi st Edit o r e d itor = ne w M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
ed i t or. ad d();
}

co n t rol .v erif y ();

We need to add a d u pli c ateEx cept io n method to the M o v i e L i s t E d i t o r V i e w interface that will take
a S t r i ng indicating the potential duplicate name:
p u bl i c in te rfac e Mo v ieLis tEdi to r V i e w {
vo i d se tM ovie s (Ve c tor m ovie s) ;
St r i ng ge tNew N ame ( );
vo i d se tE dito r (Mo v ieLis tEdi to r a n E d i t o r ) ;
vo i d se tN ewNa m e(S t ring stri ng ) ;
vo i d du pl icat e Exc e ption (Str in g s t r i n g ) ;
}

The compiler tells us that Movi e List Edi t o r . a d d ( ) can throw D u p l i c a t e M o v i e E x c e p t i o n which
needs handling. Well, we don't want to handle it in the test case; it's what should be causing the call to
d u pl i c ate Ex cept i on. Let's revisit Mov i e L i s t E d i t o r . a d d ( ) :
p u bl i c vo id add ( ) t h rows Dupl ic a t e M o v i e E x c e p t i o n {
Mo v i ene wM ovie = n e w Mov ie(v ie w . g e t N e w N a m e ( ) ) ;
mo v i es. ad d(ne w Mov i e);
up d a teM ov ieLi s t() ;
}

It just ripples the exception upward. It needs to handle it by informing the view of the problem. And we
know how, because we figured that out when we wrote the test: it has to call d u p l i c a t e E x c e p t i o n ( ) :
p u bl i c vo id add ( ) {
Mo v i e n ew Movi e = n ew Mo vie( vi e w . g e t N e w N a m e ( ) ) ;
tr y {
m o vie s. add( n ewM o vie);
u p dat eM ovie L ist ( );
} c a tch ( Dupl i cat e Movie Exce pt i o n e ) {
v i ew. du plic a teE x cepti on(n ew M o v i e . g e t N a m e ( ) ) ;
}
}

Green bar. Can we clean anything up? Yes, we get the name from the view twice in a d d ( ) . Let's pull that
out into a local variable:
p u bl i c vo id add ( ) {
St r i ng ne wNam e = v iew.g etNe wN a m e ( ) ;
Mo v i e n ew Movi e = n ew Mo vie( ne w N a m e ) ;
tr y {
m o vie s. add( n ewM o vie);
u p dat eM ovie L ist ( );
} c a tch ( Dupl i cat e Movie Exce pt i o n e ) {
v i e w.d up lica t eEx c eptio n(ne wN a m e ) ;
}
}

Test 21: Asking the logical layer to update a movie that would result in a
duplicate causes it to inform the presentation layer that the operation
would result in a duplicate
We need a similar test for updating that would cause a duplicate:
p u bl i c vo id tes t Dup l icate Caus in g U p d a t e ( ) {
mo c k Vie w. setM o vie s (movi es);
co n t rol .s etVo i dCa l lable (1);
mo c k Vie w. setN e wNa m e("St ar T re k " ) ;
co n t rol .s etVo i dCa l lable (1);
mo c k Vie w. getN e wNa m e();
co n t rol .s etRe t urn V alue( "Sta r W a r s " , 1 ) ;
mo c k Vie w. dupl i cat e Excep tion (" S t a r W a r s " ) ;
co n t rol .s etVo i dCa l lable (1);
co n t rol .a ctiv a te( ) ;
Mo v i eLi st Edit o r e d itor = ne w M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
ed i t or. se lect ( 1);
ed i t or. up date ( );
}

co n t rol .v erif y ();

Red bar. EasyMock is complaining that it got an extra call to:


s e tM o v ies ([ Star War s , Sta r Tr ek , S t a r g a t e ] )

This indicates that the rename was not performed (same movie names), but that the error was ignored.
Recall that we needed to handle the exception in M o v i e L i s t E d i t o r . u p d a t e ( ) . Let's revisit that:
p u bl i c vo id upd a te( ) {
if ( sel ec tedM o vie != nu ll) {
try {
mov ie s.re n ame ( selec tedM ov i e , v i e w . g e t N e w N a m e ( ) ) ;
upd at eMov i eLi s t();
} cat ch (Du p lic a teMov ieEx ce p t i o n e ) {
}
}
}

It's just ignoring the exception. That's no longer the simplest thing that could possibly work. We need to do
something. We need to pass the information on to the view that there was a problem so that it can inform
the user:
p u bl i c vo id upd a te( ) {
if ( sel ec tedM o vie != nu ll) {
try {
mov ie s.re n ame ( selec tedM ov i e , v i e w . g e t N e w N a m e ( ) ) ;
upd at eMov i eLi s t();
} cat ch (Du p lic a teMov ieEx ce p t i o n e ) {
vie w. dupl i cat e Excep tion (v i e w . g e t N e w N a m e ( ) ) ;
}
}
}

But that doesn't work since it's doing two calls to v i e w . g e t N e w N a m e ( ) when the mock only expected one.
This is a problem that can occur when you are using mocks:
overspecification. Do we really care that we call g e t N e w N a m e ( ) an extra
time? Not really. It shouldn't cause the test to fail. We could have
specified the mock differently, setting up a default expectation for the call
to g e tNew N ame( ). See page 167 for more information.

Anyway, this is the same situation that we encountered earlier, so we can extract the g e t N e w N a m e ( ) call
into a local variable, which is clearer. The result is:
p u bl i c vo id upd a te( ) {
if ( sel ec tedM o vie != nu ll) {
S t rin g newN a me = view .get Ne w N a m e ( ) ;
try {
mov ie s.re n ame ( selec tedM ov i e , n e w N a m e ) ;
upd at eMov i eLi s t();

} cat ch (Du p lic a teMov ieEx ce p t i o n e ) {


vie w. dupl i cat e Excep tion (n e w N a m e ) ;
}

Green bar. On to the Swing GUI. The first thing we notice is that S w i n g M o v i e L i s t E d i t o r V i e w doesn't
compile since Mov ie Lis t Edit o r.ad d() no longer throws D u p l i c a t e M o v i e - E x c e p t i o n . We can
remove the tr y-ca t ch wrapper around the call to a d d ( ) :
p r iv a t e v oi d in i tAd d Butto n() {
JB u t ton a ddBu t ton = new JBu tt o n ( " A d d " ) ;
ad d B utt on .add A cti o nList ener (n e w A c t i o n L i s t e n e r ( ) {
p u bli c void act i onPer form ed ( A c t i o n E v e n t e ) {
myE di tor. a dd( ) ;
}
}) ;
ge t C ont en tPan e (). a dd(ad dBut to n ) ;
}

Test 22: Trying to add a movie that is the same as one in the list results in
the display of a "Duplicate Movie" error dialog
Now, a test for a bad add:
p u bl i c vo id tes t Dup l icate Caus in g A d d ( ) {
ma i n Win do w = n ew J Frame Oper at o r ( " M o v i e L i s t " ) ;
Mo v i eLi st Edit o r e d itor =
n e w M ov ieLi s tEd i tor(m ovie Li s t ,
(S wi ngMo v ieL i stEdi torV ie w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;
JT e x tFi el dOpe r ato r newM ovie Fi e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ) ;
ne w M ovi eF ield . ent e rText (sta rW a r s . g e t N a m e ( ) ) ;
JB u t ton Op erat o r a d dButt on = n e w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " A d d " ) ;
ad d B utt on .pus h NoB l ock() ;
JD i a log Op erat o r m e ssage Dial og = n e w J D i a l o g O p e r a t o r ( " D u p l i c a t e M o v i e " ) ;
JL a b elO pe rato r me s sage = ne w J L a b e l O p e r a t o r ( m e s s a g e D i a l o g ) ;
as s e rtE qu als( " Wro n g mes sage t e x t " ,
" Tha t woul d re su l t i n a d u p l i c a t e M o v i e . " ,
m ess a ge.ge tTex t( ) ) ;
JB u t ton Op erat o r o k Butto n = ne w J B u t t o n O p e r a t o r ( m e s s a g e D i a l o g , " O K " ) ;
ok B u tto n. doCl i ck( ) ;
JL i s tOp er ator mov i eList = n ew J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
Li s t Mod el lis t Mod e l = m ovie Li s t . g e t M o d e l ( ) ;
as s e rtE qu als( " Mov i e lis t is t h e w r o n g s i z e " ,
m ovi e s.siz e(),
l ist M odel. getS iz e ( ) ) ;

fo r (in t i = 0 ; i < mov ies. si z e ( ) ; i + + ) {


a s ser tE qual s (
"Mo vi e li s t c o ntain s ba d m o v i e a t i n d e x " + i ,
mov ie s.ge t (i) ,
lis tM odel . get E lemen tAt( i) ) ;
}

Getting this test to pass requires filling in the d u p l i c a t e E x c e p t i o n ( ) method in the GUI class:
p u bl i c vo id dup l ica t eExce ptio n( S t r i n g d u p l i c a t e N a m e ) {
JO p t ion Pa ne.s h owM e ssage Dial og ( t h i s ,
"T ha t w o u l d r e s u l t i n a d u p l i c a t e M o v i e . " ,
"D up l i c a t e M o v i e " ,
JO pt i o n P a n e . E R R O R M E S S A G E ) ;
}

Notice that since the expected error dialog is modal, we need to use the
pu s hNo Bl ock( ) method in J B u t t o n O p e r a t o r rather than d o C l i c k ( )
as we have up until now. If we don't, d o C l i c k ( ) wouldn't return until
the user manually dismissed the dialog. By then, the dialog would be
gone and the next step in the test (finding the dialog) would fail.

Test 23: Trying to rename a movie to the name of one in the list results in
the display of a "Duplicate Movie" error dialog
The test for a bad update is similar:
p u bl i c vo id tes t Dup l icate Caus in g U p d a t e ( ) {
ma i n Win do w = n ew J Frame Oper at o r ( " M o v i e L i s t " ) ;
Mo v i eLi st Edit o r e d itor =
n ew Mo vieL i stE d itor( movi eL i s t ,
( Sw ingM o vie L istEd itor Vi e w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;
JL i s tOp er ator mov i eList = n ew J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
mo v i eLi st .cli c kOn I tem(1 , 1) ;
JT e x tFi el dOpe r ato r newM ovie Fi e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ) ;
ne w M ovi eF ield . ent e rText (sta rW a r s . g e t N a m e ( ) ) ;
JB u t ton Op erat o r u p dateB utto n = n e w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " U p d a t e " ) ;
up d a teB ut ton. p ush N oBloc k();
JD i a log Op erat o r m e ssage Dial og = n e w J D i a l o g O p e r a t o r ( " D u p l i c a t e M o v i e " ) ;
JL a b elO pe rato r me s sage = ne w J L a b e l O p e r a t o r ( m e s s a g e D i a l o g ) ;
as s e rtE qu als( " Wro n g mes sage t e x t " ,
"Th a t wou ld r es u l t i n a d u p l i c a t e M o v i e . " ,
mes s age.g etTe xt ( ) ) ;

JB u t ton Op erat o r o k Butto n = ne w J B u t t o n O p e r a t o r ( m e s s a g e D i a l o g , " O K " ) ;


ok B u tto n. doCl i ck( ) ;
Li s t Mod el lis t Mod e l = m ovie Li s t . g e t M o d e l ( ) ;
as s e rtE qu als( " Mov i e lis t is t h e w r o n g s i z e " ,
mov i es.si ze() ,
lis t Model .get Si z e ( ) ) ;

fo r (in t i = 0 ; i < mov ies. si z e ( ) ; i + + ) {


a s ser tE qual s ("M o vie l ist co n t a i n s b a d m o v i e a t i n d e x " + i ,
m o vies. get( i) ,
l i stMod el.g et E l e m e n t A t ( i ) ) ;
}

Green bar right away. Is that odd? No, since it's testing the same reaction to a bad update as we just
implemented for a bad add.
Take a minute to look at the tests we just added. They are very similar. We can refactor some of that
commonality out by extracting a couple of methods for the message dialog check and the check that the list
is unchanged. The result is:
p u bl i c vo id tes t Dup l icate Caus in g A d d ( ) {
ma i n Win do w = n ew J Frame Oper at o r ( " M o v i e L i s t " ) ;
Mo v i eLi st Edit o r e d itor =
n ew Mo vieL i stE d itor( movi eL i s t ,
( Sw ingM o vie L istEd itor Vi e w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;
JT e x tFi el dOpe r ato r newM ovie Fi e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ) ;
ne w M ovi eF ield . ent e rText (sta rW a r s . g e t N a m e ( ) ) ;
JB u t ton Op erat o rad d Butto n = ne w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " A d d " ) ;
ad d B utt on .pus h NoB l ock() ;
ch e c kDu pl icat e Exc e ption Dial og ( ) ;

JL i s tOp er ator mov i eList = n ew J L i s t O p e r a t o r ( m a i n W i n d o w ) ;


ch e c kLi st IsUn c han g ed(mo vieL is t ) ;

p u bl i c vo id tes t Dup l icate Caus in g U p d a t e ( ) {


ma i n Win do w = n ew J Frame Oper at o r ( " M o v i e L i s t " ) ;
Mo v i eLi st Edit o r e d itor =
n ew Mo vieL i stE d itor( movi eL i s t ,
( Sw ingM o vie L istEd itor Vi e w ) m a i n W i n d o w . g e t W i n d o w ( ) ) ;
JL i s tOp er ator mov i eList = n ew J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
mo v i eLi st .cli c kOn I tem(1 , 1) ;
JT e x tFi el dOpe r ato r newM ovie Fi e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ) ;
ne w M ovi eF ield . ent e rText (sta rW a r s . g e t N a m e ( ) ) ;
JB u t ton Op erat o r u p dateB utto n = n e w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " U p d a t e " ) ;
up d a teB ut ton. p ush N oBloc k();

ch e c kDu pl icat e Exc e ption Dial og ( ) ;


ch e c kLi st IsUn c han g ed(mo vieL is t ) ;

p r iv a t e v oi d ch e ckL i stIsU ncha ng e d ( J L i s t O p e r a t o r m o v i e L i s t ) {


Li s t Mod el lis t Mod e l = m ovie Li s t . g e t M o d e l ( ) ;
as s e rtE qu als( " Mov i e lis t is t h e w r o n g s i z e " ,
mov i es.si ze() ,
lis t Model .get Si z e ( ) ) ;

fo r (in t i = 0 ; i < mov ies. si z e ( ) ; i + + ) {


a s ser tE qual s ("M o vie l ist co n t a i n s b a d m o v i e a t i n d e x " + i ,
m o vies. get( i) ,
l i stMod el.g et E l e m e n t A t ( i ) ) ;
}

p r iv a t e v oi d ch e ckD u plica teEx ce p t i o n D i a l o g ( ) {


JD i a log Op erat o r m e ssage Dial og = n e w J D i a l o g O p e r a t o r ( " D u p l i c a t e M o v i e " ) ;
JL a b elO pe rato r me s sage = ne w J L a b e l O p e r a t o r ( m e s s a g e D i a l o g ) ;
as s e rtE qu als( " Wro n g mes sage t e x t " ,
"Th a t wou ld r es u l t i n a d u p l i c a t e M o v i e . " ,
mes s age.g etTe xt ( ) ) ;

JB u t ton Op erat o r o k Butto n = ne w J B u t t o n O p e r a t o r ( m e s s a g e D i a l o g , " O K " ) ;


ok B u tto n. doCl i ck( ) ;

Looking more at this test case, we notice that the code at the beginning of almost every test is identical:
m a in W i ndo w = ne w JF r ameOp erat or ( " M o v i e L i s t " ) ;
M o vi e L ist Ed itor edi t or =
n e w Mo vi eLis t Edi t or(
mov ie List ,
( Swi ng Movi e Lis t Edito rVie w) m a i n W i n d o w . g e t W i n d o w ( ) ) ;

We can pull that out and put it in s etUp( ) :


p r ot e c ted v oid s etU p () th rows E x c e p t i o n {
su p e r.s et Up() ;
Sw i n gMo vi eLis t Edi t orVie w.st ar t ( ) ;
st a r War s = ne w Mo v ie("S tar Wa r s " ) ;
st a r Tre k = ne w Mo v ie("S tar Tr e k " ) ;
st a r gat e = ne w Mo v ie("S targ at e " ) ;
mo v i es =n ewVe c tor ( );
mo v i es. ad d(st a rWa r s);
mo v i es. ad d(st a rTr e k);
mo v i es. ad d(st a rga t e);
mo v i eLi st = n e wMo v ieLis t();
mo v i eLi st .add ( sta r Wars) ;
mo v i eLi st .add ( sta r Trek) ;
mo v i eLi st .add ( sta r gate) ;

ma i n Win do w = n ew J Frame Oper at o r ( " M o v i e L i s t " ) ;


ed i t or = new M ovi e ListE dito r( m o v i e L i s t ,
(SwingMovieListEditorView) mainWindow.getWindow());

Now those two lines can be removed from each of the tests. This cleans things up nicely.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
In this chapter we added some error checking. Specifically, we checked whether an add or rename
operation will result in a duplicate movie in the list. If not, the action is performed, otherwise, a
Dup l icate Mov ie Exc ep tion is thrown. This percolates to the GUI where an error dialog is presented
to the user. Since this is a modal dialog, we had to use pu sh No B lo c k( ) rather than d oC l ic k( ) in
Jemmy to click the Add and Update buttons.

Amazon

Prev

don't be afraid of buying books

Chapter 13. RATINGS


Movies each have a single, editable rating.

Next

Amazon

Prev

don't be afraid of buying books

Next

ADD A SINGLE RATING TO MOVIE


Add support for a single rating to Movie. Support the concept of an unrated movie.
This is a straightforward task; we're simply adding a piece of information to a class. Note that the task
does not require changing the rating.
Test 24. An unrated movie answers negative when asked if it is rated.

Test 25. A rated movie answers positive when asked if it is rated, and it can answer its
rating when asked.

Test 26. Asking an unrated movie for its rating throws an exception.

We'll start with the test for unrated movies.

Test 24: An unrated movie answers negative when asked if it is rated


p ub l ic v oid test U nrat e d() {
a s se rt Fal s e("s t arWa r s sh ould b e u n r a t e d . " , s t a r W a r s . h a s R a t i n g ( ) ) ;
}

To pass this test we add Mo v ie.h asRa t i n g ( ) :


p ub l ic b ool e an h a sRat i ng() {
r e tu rn fa l se;
}

Test 25: A rated movie answers positive when asked if it is rated, and it
can answer its rating when asked
Now we write a test for a movie with a rating. Here's the start of the test:
p ub l ic v oid test R ated M ovie () {
a s se rt Tru e ("fo t r sh o uld be r a t e d . " , f o t r . h a s R a t i n g ( ) ) ;
a s se rt Equ a ls(" f otr s houl d be r a t e d a t 5 . " , 5 , f o t r . g e t R a t i n g ( ) ) ;
}

Now we need to answer the question, "How does a movie get a rating?" There are two reasonable
answers to this:
1. by a constructor, and/or
2. using a mutator (e.g., s etRa ti n g ( ) ).
Since we have no requirement yet to be able to change a movie's rating, let's go with the first option.
We will add another constructor that takes a rating in addition to a name. Our test is:
p ub l ic v oid test R ated M ovie () {
M o vi e fot r = n e w Mo v ie(" Fell o w s h i p o f t h e R i n g " , 5 ) ;
a s se rt Tru e ("fo t r sh o uld be r a t e d " , f o t r . h a s R a t i n g ( ) ) ;
a s se rt Equ a ls(" f otr s houl d be r a t e d a t 5 . " , 5 , f o t r . g e t R a t i n g ( ) ) ;
}

Now we have to add a constructor and a g e t R a t i n g ( ) method to M o v i e to get the test to compile:
p ub l ic M ovi e (Str i ng a N ame, int a R a t i n g ) {
t h is (a Nam e );
}
p ub l ic i nt g etRa t ing( ) {
r e tu rn 5;
}

Now, of course, ha sRa t ing( ) always returns f a l s e which causes the first assert to fail. We could go
in baby steps, but by now we're feeling more confident and experienced so we'll take a bigger step.
What needs to be done? It's pretty obvious that we should grab the rating that is passed to the
constructor, store it somewhere, and use that to determine the value returned from h a s R a t i n g ( ) and
g et R at in g() .Now the question is, "What does it mean for a movie to be unrated?" A valid rating is
between 0 and 5 , inclusive. The simplest thing is to assign - 1 to denote unrated. Here's the changed
code in Mo vie :
p ri v at e int rati n g;
p ub l ic M ovi e (Str i ng a N ame) {
t h is (a Nam e , -1 ) ;
}
p ub l ic M ovi e (Mov i e or i gina l)
{
n a me = or i gina l .nam e ;
r a ti ng = o rigi n al.r a ting ;
}

p ub l ic M ovi e (Str i ng a N ame, int a R a t i n g ) {


c h ec kN ull ( aNam e );
c h ec kE mpt y (aNa m e);
n a me = aN a me;
r a ti ng = a Rati n g;

}
p ub l ic b ool e an h a sRat i ng() {
r e tu rn ra t ing > -1;
}
p ub l ic i nt g etRa t ing( ) {
r e tu rn ra t ing;
}

Our tests all pass now. Can we refactor anything? I don't think so. One thing that would be nice to do
would be to move ra t ing from being an i n t to an object of some sort. In the future, may be, when
we need it. For now, however, an int works fine.

Test 26: Asking an unrated movie for its rating throws an exception
Next, we want to throw an exception if an unrated M o v i e is asked for its rating:
p ub l ic v oid test U nrat e dExc epti o n ( ) {
try {
st ar War s .get R atin g ();
fa il ("g e tRat i ng() of a n un r a t e d M o v i e s h o u l d t h r o w U n r a t e d E x c e p t i o n . " ) ;
} ca tc h ( U nrat e dExc e ptio n ex ) {
as se rtE q uals ( "Unr a tedE xcep t i o n s h o u l d i d e n t i f y t h e m o v i e . " ,
star W ars. getN a m e ( ) ,
ex.g e tMes sage ( ) ) ;
}
}

This requires us to add an exception class, U n r a t e d E x c e p t i o n , and have it thrown by


M ov i e. ge tRa t ing( ) . The code changes are simple enough. The fallout from this is that now
t es t Ra te dMo v ie() doesn't compile since it doesn't handle U n r a t e d E x c e p t i o n . We simply make the
test method throw it on to JUnit and all is well. Well, everything compiles. When we run the tests we get
a red barthat is expected and desired. Now we can add the code to make it green again:
p ub l ic i nt g etRa t ing( ) thr ows U n r a t e d E x c e p t i o n {
i f ( ha sRa t ing( ) ) {
re tu rn r atin g ;
} el se {
th ro w n e w Un r ated E xcep tion ( n a m e ) ;
}
}

Amazon

Prev

don't be afraid of buying books

Next

SHOW THE RATING IN THE GUI


Show the rating in the GUI, next to the movie name.
Now we need the rating on the GUI. This is all in the presentation layer since the movie list is given a
list of movies. This means that we have nothing to do at the logic level (i.e., M o v i e L i s t E d i t o r ) and
we can move directly to the Swing classes. After getting more information about what is
desired/possible/easy, it is decided that we will display an Nof5 star indicator to the left of the movie
name in the list. This will require a custom renderer for the list. We can easily write tests for this. We'll
need a new test case, T estC u stom Lis t R e n d e r e r .
Test 27. When asked for the renderer component, the renderer returns itself.

Test 28. When given a movie to render, the resulting test and rating image corresponds
to the movie being rendered.

Test 29. When rendering an unselected item, the renderer uses its list's unselected
colors.

Test 30. When rendering a selected item, the renderer uses its list's selected colors.

We'll need an instance of the renderer to test, a J L i s t to pass in, and a couple of M o v i e s to test
with. Let's start by creating a fixture:
p ri v at e
p ri v at e
p ri v at e
p ri v at e

Mov i e fo t r = n ull;
Mov i e st a rTre k = n ull;
Cus t omMo v ieLi s tRen dere r r e n d e r e r = n u l l ;
JLi s t li s t = n ull;

p ro t ec te d v o id s e tUp( ) {
f o tr = ne w Mov i e("F e llow ship o f T h e R i n g " , 5 ) ;
s t ar Tr ek = new Movi e ("St ar T r e k " , 3 ) ;
r e nd er er = new Cust o mMov ieLi s t R e n d e r e r ( ) ;
l i st = ne w JLi s t();
}

We'll work simply, testing one behavior at a time.

Test 27: When asked for the renderer component, the renderer returns
itself
To start, the rendering method should return the renderer itself:
p ub l ic v oid test R etur n sSel f() {
C o mp on ent resu l t = r ende rer. g e t L i s t C e l l R e n d e r e r C o m p o n e n t ( l i s t ,
fotr,

1,
false,
false);

a s se rt Sam e ("ge t List C ellR ende r e r C o m p o n e n t s h o u l d r e t u r n s e l f . " ,


re n dere r ,
re s ult) ;

This needs just the basic rendering method:


p ub l ic C omp o nent getL i stCe llRe n d e r e r C o m p o n e n t ( J L i s t l i s t ,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
r e tu rn th i s;
}

Test 28: When given a movie to render, the resulting test and rating
image corresponds to the movie being rendered
The next step is to make sure the text and rating image is getting set properly:
p ub l ic v oid test C onte n ts() {
r e nd er er. g etLi s tCel l Rend erer C o m p o n e n t ( l i s t , f o t r , 1 , f a l s e , f a l s e ) ;
a s se rt Equ a ls(" T ext s houl d be " + f o t r . g e t N a m e ( ) ,
f o tr.g e tNam e(),
r e nder e r.ge tTex t ( ) ) ;
a s se rt Sam e ("Ic o n sh o uld be 5 s t a r s . " ,
Cus t omMo v ieLi stRe n d e r e r . i c o n F o r R a t i n g ( 5 ) ,
ren d erer . getI con( ) ) ;
r e nd er er. g etLi s tCel l Rend erer C o m p o n e n t ( l i s t , s t a r T r e k , 1 , t r u e , f a l s e ) ;
a s se rt Equ a ls(" T ext s houl d be " + s t a r T r e k . g e t N a m e ( ) ,
s t arTr e k.ge tNam e ( ) ,
r e nder e r.ge tTex t ( ) ) ;

a s se rt Sam e ("Ic o n sh o uld be 3 s t a r s . " ,


Cus t omMo v ieLi stRe n d e r e r . i c o n F o r R a t i n g ( 3 ) ,
ren d erer . getI con( ) ) ;

Here's the new rendering method:


p ub l ic C omp o nent getL i stCe llRe n d e r e r C o m p o n e n t ( J L i s t l i s t ,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {

M o vi e mov i eToR e nder = (M ovie ) v a l u e ;


s e tT ex t(m o vieT o Rend e r.ge tNam e ( ) ) ;

i f ( mo vie T oRen d er.h a sRat ing( ) ) {


tr y {
se tIc o n(ra t ingI c ons[ movi e T o R e n d e r . g e t R a t i n g ( ) + 1 ] ) ;
} ca tch (Unr a tedE x cept ion e x ) { }
} el se {
se tI con ( rati n gIco n s[0] );
}
r e tu rn th i s;

To support this we need some icons where we can get at them based on the rating. We'll do this with a
static variable on the renderer:
p ri v at e
{
ne w
ne w
ne w
ne w
ne w
ne w
ne w
};

sta t ic I m ageI c on[ ] ra t i n g I c o n s =


Ima g eIco n ("im a ges/ no-r a t i n g . g i f " ) ,
Ima g eIco n ("im a ges/ zero - s t a r s . g i f " ) ,
Ima g eIco n ("im a ges/ one- s t a r . g i f " ) ,
Ima g eIco n ("im a ges/ two- s t a r s . g i f " ) ,
Ima g eIco n ("im a ges/ thre e - s t a r s . g i f " ) ,
Ima g eIco n ("im a ges/ four - s t a r s . g i f " ) ,
Ima g eIco n ("im a ges/ five - s t a r s . g i f " )

p ub l ic s tat i c Im a geIc o n ic onFo r R a t i n g ( i n t r a t i n g ) {


r e tu rn ra t ingI c ons[ r atin g];
}

Test 29: When rendering an unselected item, the renderer uses its list's
unselected colors
Next, we need to deal with the colors. First, the unselected colors:
p ub l ic v oid test U nSel e cted Colo r s ( ) {
l i st .s etB a ckgr o und( C olor .BLU E ) ;
l i st .s etF o regr o und( C olor .RED ) ;
l i st .s etS e lect i onBa c kgro und( C o l o r . R E D ) ;
l i st .s etS e lect i onFo r egro und( C o l o r . B L U E ) ;
r e nd er er. g etLi s tCel l Rend erer C o m p o n e n t ( l i s t , f o t r , 1 , f a l s e , f a l s e ) ;
a s se rt Equ a ls(" U nsel e cted bac k g r o u n d s h o u l d b e b l u e . " ,
C o lor. B LUE,
r e nder e r.ge tBac k g r o u n d ( ) ) ;

a s se rt Equ a ls(" U nsel e cted for e g r o u n d s h o u l d b e r e d . " ,


C o lor. R ED,
r e nder e r.ge tFor e g r o u n d ( ) ) ;

And the new renderer method to pass it:


p ub l ic C omp o nent getL i stCe llRe n d e r e r C o m p o n e n t ( J L i s t l i s t ,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
M o vi e mov i eToR e nder = (M ovie ) v a l u e ;
s e tB ac kgr o und( l ist. g etBa ckgr o u n d ( ) ) ;
s e tF or egr o und( l ist. g etFo regr o u n d ( ) ) ;
s e tT ex t(m o vieT o Rend e r.ge tNam e ( ) ) ;

i f ( mo vie T oRen d er.h a sRat ing( ) ) {


tr y {
se tI con ( rati n gIco n s[mo vieT o R e n d e r . g e t R a t i n g ( ) + 1 ] ) ;
} ca tch (Unr a tedE x cept ion e x ) { }
} el se {
se tI con ( rati n gIco n s[0] );
}
r e tu rn th i s;

Test 30: When rendering a selected item, the renderer uses its list's
selected colors
And, finally, selected colors:
p ub l ic v oid test S elec t edCo lors ( ) {
l i st .s etB a ckgr o und( C olor .BLU E ) ;
l i st .s etF o regr o und( C olor .RED ) ;
l i st .s etS e lect i onBa c kgro und( C o l o r . R E D ) ;
l i st .s etS e lect i onFo r egro und( C o l o r . B L U E ) ;
r e nd er er. g etLi s tCel l Rend erer C o m p o n e n t ( l i s t , f o t r , 1 , t r u e , f a l s e ) ;

a s se rt Equ a ls(" S elec t ed b ackg r o u n d s h o u l d b e b l u e r e d . " ,


C o lor. R ED,
r e nder e r.ge tBac k g r o u n d ( ) ) ;
a s se rt Equ a ls(" S elec t ed f oreg r o u n d s h o u l d b e r e d b l u e . " ,
C o lor. B LUE,
r e nder e r.ge tFor e g r o u n d ( ) ) ;

And the code to pass it:


p ub l ic C omp o nent getL i stCe llRe n d e r e r C o m p o n e n t ( J L i s t l i s t ,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
M o vi e mov i eToR e nder = (M ovie ) v a l u e ;

i f ( is Sel e cted ) {
se tB ack g roun d (lis t .get Sele c t i o n B a c k g r o u n d ( ) ) ;
se tF ore g roun d (lis t .get Sele c t i o n F o r e g r o u n d ( ) ) ;

} el se {
se tB ack g roun d (lis t .get Back g r o u n d ( ) ) ;
se tF ore g roun d (lis t .get Fore g r o u n d ( ) ) ;
}

s e tT ex t(m o vieT o Rend e r.ge tNam e ( ) ) ;


i f ( mo vie T oRen d er.h a sRat ing( ) ) {
tr y {
se tIc o n(ra t ingI c ons[ movi e T o R e n d e r . g e t R a t i n g ( ) + 1 ] ) ;
} ca tch (Unr a tedE x cept ion e x ) {
}
} el se {
se tI con ( rati n gIco n s[0] );
} r et ur n t h is;

Green bar all the way. We should refactor the code that sets the list colors into s e t U p ( ) . Here's the
new version of the affected code:
p ro t ec te d v o id s e tUp( ) {
f o tr = ne w Mov i e("F e llow ship o f T h e R i n g " , 5 ) ;
s t ar Tr ek = new Movi e ("St ar T r e k " , 3 ) ;
r e nd er er = new Cust o mMov ieLi s t R e n d e r e r ( ) ;
l i st = ne w JLi s t();

l i st .s etB a ckgr o und( C olor .BLU E ) ;


l i st .s etF o regr o und( C olor .RED ) ;
l i st .s etS e lect i onBa c kgro und( C o l o r . R E D ) ;
l i st .s etS e lect i onFo r egro und( C o l o r . B L U E ) ;

p ub l ic v oid test U nSel e cted Colo r s ( ) {


r e nd er er. g etLi s tCel l Rend erer C o m p o n e n t ( l i s t , f o t r , 1 , f a l s e , f a l s e ) ;
a s se rt Equ a ls(" U nsel e cted bac k g r o u n d s h o u l d b e b l u e . " ,
C o lor. B LUE,
r e nder e r.ge tBac k g r o u n d ( ) ) ;

a s se rt Equ a ls(" U nsel e cted for e g r o u n d s h o u l d b e r e d . " ,


C o lor. R ED,
r e nder e r.ge tFor e g r o u n d ( ) ) ;

p ub l ic v oid test S elec t edCo lors ( ) {


r e nd er er. g etLi s tCel l Rend erer C o m p o n e n t ( l i s t , f o t r , 1 , t r u e , f a l s e ) ;
a s se rt Equ a ls(" S elec t ed b ackg r o u n d s h o u l d b e r e d . " ,
C o lor. R ED,
r e nder e r.ge tBac k g r o u n d ( ) ) ;

a s se rt Equ a ls(" S elec e td f oreg r o u n d s h o u l d b e b l u e . " ,


C o lor. B LUE,
r e nder e r.ge tFor e g r o u n d ( ) ) ;

Now all we need to do is use this renderer in the Swing GUI. Figure 13.1 shows the new version of the
GUI, and here's the updated code:
p ri v at e voi d ini t List ( ) {
m o vi eL ist = ne w JLi s t(ne w Ve c t o r ( ) ) ;
m o vi eL ist . setS e lect i onMo de(L i s t S e l e c t i o n M o d e l . S I N G L E S E L E C T I O N ) ;
m o vi eL ist . setC e llRe n dere r(ne w C u s t o m M o v i e L i s t R e n d e r e r ( ) ) ;
m o vi eL ist . addL i stSe l ecti onLi s t e n e r ( n e w L i s t S e l e c t i o n L i s t e n e r ( ) {

p ub li c v o id v a lueC h ange d(Li s t S e l e c t i o n E v e n t e ) {


my Ed ito r .sel e ct(m o vieL ist. g e t S e l e c t e d I n d e x ( ) ) ;
}
});

J S cr ol lPa n e sc r olle r =
ne w JSc r ollP a ne(
m ov ieL i st,
S cr oll P aneC o nsta n ts.V ERTI C A L S C R O L L B A R A L W A Y S ,
S cr oll P aneC o nsta n ts.H ORIZ O N T A L S C R O L L B A R N E V E R ) ;
g e tC on ten t Pane ( ).ad d (scr olle r ) ;

Figure 13.1. GUI with ratings.

Amazon

Prev

don't be afraid of buying books

Next

EDIT THE RATING


Allow the rating to be edited.
For this task, we'll need the following tests:
Test 31. Selecting a movie updates the rating in the GUI.

Test 32. Updating a movie changes its rating if a different rating was selected for it.

Test 33. Selecting a movie from the list updates the displayed rating.

Test 34. Updating a movie in the GUI changes its rating if a different rating was
selected for it, and updates the display accordingly.

We'll start by adding some ratings to Te s t M o v i e L i s t E d i t o r 's fixture:


s ta r Wa rs = n ew M o vie( " Star War s " , 5 ) ;
s ta r Tr ek = n ew M o vie( " Star Tre k " , 3 ) ;
s ta r ga te = n ew M o vie( " Star gate " ) ;

Test 31: Selecting a movie updates the rating in the GUI


Next we'll extend te s tSel e ctin g() to check that a selection in the movie list causes an update of
the rating:
p ub l ic v oid test S elec t ing( ) {
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Name ( "Sta r Wa r s " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Rati n g(6) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Name ( "Sta r Tr e k " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Rati n g(4) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Name ( "Sta rgat e " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNe w Rati n g(0) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();

M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;


e d it or .se l ect( 0 );
e d it or .se l ect( 1 );
e d it or .se l ect( 2 );
}

c o nt ro l.v e rify ( );

The first step to getting to a green bar again is to add a method to the M o v i e L i s t E d i t o r V i e w
interface:
p ub l ic i nte r face Movi e List Edit o r V i e w {
v o id s etM o vies ( Vect o r mo vies ) ;
S t ri ng ge t NewN a me() ;
v o id s etE d itor ( Movi e List Edit o r a n E d i t o r ) ;
v o id s etN e wNam e (Str i ng s trin g ) ;
v o id d upl i cate E xcep t ion( Stri n g s t r i n g ) ;
v o id s etN e wRat i ng(i n t i) ;
}

Next, we need to add to M o vieL istEd i t o r . s e l e c t ( ) to set the rating as well as the name:
p ub l ic v oid sele c t(in t i) {
i f ( i == - 1) {
s el ect e dMov i e = n ull;
} el se {
se le cte d Movi e = m o vies .get M o v i e ( i ) ;
vi ew .se t NewN a me(s e lect edMo v i e . g e t N a m e ( ) ) ;

tr y {
vi ew. s etNe w Rati n g(se lect e d M o v i e . g e t R a t i n g ( ) + 1 ) ;
} ca tch (Unr a tedE x cept ion e ) {
vi ew. s etNe w Rati n g(0) ;
}

Now we see that several tests are failing due to the additional (and unexpected) calls to
s et N ew Ra tin g (). We can either add expectations to these tests or have them ignore those calls. We'll
do the latter since it's the simplest thing that will work. We just need to add the following line to the
beginning of each failing test:
m oc k Vi ew .se t NewR a ting ( 0);
c on t ro l. set D efau l tVoi d Call able ( ) ;

Upon looking over the code, I feel that some renaming is in order. The method for getting and setting
the name and rating fields are misleading. Let's change them as so: g e t N e w N a m e ( ) changes to
g et N am eF iel d ().For brevity, we'll just show the interface:
p ub l ic i nte r face Movi e List Edit o r V i e w {
v o id s etM o vies ( Vect o r mo vies ) ;
S t ri ng ge t Name F ield ( );
v o id s etE d itor ( Movi e List Edit o r a n E d i t o r ) ;
v o id s etN a meFi e ld(S t ring str i n g ) ;
v o id d upl i cate E xcep t ion( Stri n g s t r i n g ) ;
v o id s etR a ting F ield ( int i);
}

Test 32: Updating a movie changes its rating if a different rating was
selected for it
Now for the flip side of editing the rating...updating the movie behind it. We'll start by extending
t es t Up da tin g () by adding ratings to the M o v i e s we create and adding expectations for calls to
s et R at in gFi e ld() and g etRa tingF i e l d ( ) :
p ub l ic v oid test U pdat i ng() {
V e ct or ne w Movi e s = n ew V ecto r ( ) ;
n e wM ov ies . add( s tarW a rs);
n e wM ov ies . add( n ew M o vie( "Sta r T r e k I " , 5 ) ) ;
n e wM ov ies . add( s targ a te);
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d("S tar T r e k " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 4);
c o nt ro l.s e tVoi d Call a ble( );
m o ck Vi ew. g etNa m eFie l d();
c o nt ro l.s e tRet u rnVa l ue(" Star T r e k I " , 1 ) ;
m o ck Vi ew. g etRa t ingF i eld( );
c o nt ro l.s e tRet u rnVa l ue(6 , 1) ;
m o ck Vi ew. s etMo v ies( n ewMo vies ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
e d it or .se l ect( 1 );
e d it or .up d ate( ) ;
}

c o nt ro l.v e rify ( );

To get to green, we have to do some work on M o v i e L i s t E d i t o r . u p d a t e ( ) . We'll add a line that


sets the rating of the selected Mov ie if it can be renamed successfully:
p ub l ic v oid upda t e() {
i f ( se lec t edMo v ie ! = nul l) {
St ri ng n ewNa m e = v iew. getN a m e F i e l d ( ) ;

tr y {
mo vie s .ren a me(s e lect edMo v i e , n e w N a m e ) ;
se lec t edMo v ie.s e tRat ing( v i e w . g e t R a t i n g F i e l d ( ) ) ;
up dat e Movi e List ( );
} ca tch (Dup l icat e Movi eExc e p t i o n e ) {
vi ew. d upli c ateE x cept ion( n e w N a m e ) ;
}

Now we need to add a setR a ting () method to M o v i e . We won't test-drive that, as it will be a simple
mutator and it will be tested indirectly. In fact, I won't even bother showing it here. Most IDEs will have
a way to generate accessors and mutators for instance variables.
Green bar. Now we can turn to the Swing layer. We can take the same approach to editing the rating as
to editing the name: When a movie is selected, use a field to edit its value. In this case, since the range
is constrained, we'll use a combo box.
We'll start by adding some ratings to the fixture:
s ta r Wa rs = n ew M o vie( " Star War s " , 5 ) ;
s ta r Tr ek = n ew M o vie( " Star Tre k " , 3 ) ;
s ta r ga te = n ew M o vie( " Star gate " ) ;

Green bar. All the tests still work. We can continue.

Test 33: Selecting a movie from the list updates the displayed rating
We'll start with a test to check that selecting a movie in the list sets the rating selection combo box to
the appropriate value:
p ub l ic v oid test S elec t Upda tesR a t i n g ( ) {
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
J C om bo Box O pera t or r a ting Comb o = n e w J C o m b o B o x O p e r a t o r ( m a i n W i n d o w ) ;
m o vi eL ist . clic k OnIt e m(0, 1);
a s se rt Equ a ls(" w rong rati ng f r o m s e l e c t i n g s t a r W a r s . " ,
6,
r a ting C ombo .get S e l e c t e d I n d e x ( ) ) ;
m o vi eL ist . clic k OnIt e m(1, 1);
a s se rt Equ a ls(" w rong rati ng f r o m s e l e c t i n g s t a r T r e k . " ,
4,
r a ting C ombo .get S e l e c t e d I n d e x ( ) ) ;

m o vi eL ist . clic k OnIt e m(2, 1);


a s se rt Equ a ls(" w rong rati ng f r o m s e l e c t i n g s t a r g a t e . " ,
0,
r a ting C ombo .get S e l e c t e d I n d e x ( ) ) ;

Now we need to make it pass. We need a combo box in the GUI and methods to set and get its value:
p ri v at e JCo m boBo x rat i ngFi eld = n u l l ;

p u bl ic vo i d se t Rati n gFie ld(i n t i ) {


ra ti ngF i eld. s etSe l ecte dInd e x ( i ) ;

p ub l ic i nt g etRa t ingF i eld( ) {


r e tu rn ra t ingF i eld. g etSe lect e d I n d e x ( ) ;
}
p ub l ic v oid init ( ) {
s e tT it le( ) ;
s e tL ay out ( );
i n it Li st( ) ;
i n it Fi eld ( );
i n it Ra tin g Comb o ();
i n it Ad dBu t ton( ) ;
i n it Up dat e Butt o n();
p a ck () ;
}
p ri v at e voi d ini t Rati n gCom bo() {
r a ti ng Fie l d = n ew J C ombo Box( C u s t o m M o v i e L i s t R e n d e r e r . i c o n s ( ) ) ;
g e tC on ten t Pane ( ).ad d (rat ingF i e l d ) ;
}

Green bar. OK. We talk to the customer. We need to be able to select a movie, change the rating, and
click update. We try it. Uh oh. We get the duplicate movie message. Hmmm... think think... talk talk...
what we need to do is raise that error only if the update makes the selected movie equal to a different
movie in the list.
We need to go back to T estMo vieL is t E d i t o r and create a test to drive that behavior:
p ub l ic v oid test U pdat i ngWi thSa m e N a m e ( ) {
V e ct or ne w Movi e s = n ew V ecto r ( ) ;
n e wM ov ies . add( s tarW a rs);
n e wM ov ies . add( n ew M o vie( "Sta r T r e k " , 5 ) ) ;
n e wM ov ies . add( s targ a te);
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d("S tar T r e k " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 4);
c o nt ro l.s e tVoi d Call a ble( );
m o ck Vi ew. g etNa m eFie l d();
c o nt ro l.s e tRet u rnVa l ue(" Star T r e k " , 1 ) ;
m o ck Vi ew. g etRa t ingF i eld( );
c o nt ro l.s e tRet u rnVa l ue(6 , 1) ;
m o ck Vi ew. s etMo v ies( n ewMo vies ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
e d it or .se l ect( 1 );
e d it or .up d ate( ) ;

c o nt ro l.v e rify ( );

Now we can tweak the Movi e List Edi t o r . u p d a t e ( ) method to make the bar green again:
p ub l ic v oid upda t e() {
i f ( se lec t edMo v ie ! = nul l) {
S tr ing newN a me = view .get N a m e F i e l d ( ) ;
i f (se l ecte d Movi e .get Name ( ) . e q u a l s ( n e w N a m e ) ) {
u pda t eMov i e();
} e lse {

t ry {
mo v ies. r enam e (sel ecte d M o v i e , n e w N a m e ) ;
up d ateM o vie( ) ;
} ca t ch ( D upli c ateM ovie E x c e p t i o n e ) {
vie w .dup l icat e Exce ptio n ( n e w N a m e ) ;
}

p ri v at e voi d upd a teMo v ie() {


s e le ct edM o vie. s etRa t ing( view . g e t R a t i n g F i e l d ( ) - 1 ) ;
u p da te Mov i eLis t ();
}

Notice that we did some refactoring while we were there.

Test 34: Updating a movie in the GUI changes its rating if a different
rating was selected for it, and updates the display accordingly
Now back to the Swing GUI. We need a test that selects a movie, changes the rating, and updates:
p ub l ic v oid test U pdat e Rati ng() {
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
J C om bo Box O pera t or r a ting Comb o = n e w J C o m b o B o x O p e r a t o r ( m a i n W i n d o w ) ;
m o vi eL ist . clic k OnIt e m(0, 1);
r a ti ng Com b o.se t Sele c tedI ndex ( 4 ) ;

J B ut to nOp e rato r upd a teBu tton = n e w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " U p d a t e " ) ;


u p da te But t on.p u shNo B lock ();
m o vi eL ist . clic k OnIt e m(1, 1);
m o vi eL ist . clic k OnIt e m(0, 1);
a s se rt Equ a ls(" u pdat i ng s houl d h a v e c h a n g e d r a t i n g . " ,
4,
r atin g Comb o.ge t S e l e c t e d I n d e x ( ) ) ;

Green bar. Life is good. Finally, Figure 13.2 shows the current state of the GUI.

Figure 13.2. GUI with editable ratings.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
We've done a few things in this chapter. One interesting thing was developing a custom list cell renderer
test-first. We've also extended our Jemmy testing repertoire by adding combo boxes.

Amazon

Prev

don't be afraid of buying books

Chapter 14. CATEGORIES


Movies belong to a category; categories such as Horror, Comedy, etc.

Next

Amazon

Prev

don't be afraid of buying books

Next

ADD A CATEGORY
Add support for a movie to have a single category, from a fixed list of alternatives.
To add support for categories through the application we will want these tests:
Test 35. A movie that hasn't explicitly been given a category should answer that it is
uncategorized when asked for its category.

Test 36. If a movie is given a category when it is created, it answers that when asked
for its category.

Test 37. Trying to create a movie with an invalid category (i.e., not from the predefined
set) throws an exception.

We begin by adding support for a category to M o vie . Notice that the fleshed out task description
mentions that the categories are from a fixed list. That means that the category of a movie can't be just
anything (like an arbitrary string), but has to be picked from a fixed set of alternatives. So let's pick an
initial list to work with: Science Fiction, Drama, Comedy, Horror, Western, and Fantasy. We will also
need an Uncategorized setting.

Test 35: A movie that hasn't explicitly been given a category should
answer that it is uncategorized when asked for its category
We'll start with the last case, since it doesn't require actually setting the category of a Mo v ie :
pub l ic vo id te stU nc ateg oriz ed() {
a s sertE qua ls ("s ta rWar s sh ould b e u n c ate g o ri ze d. " ,
"U nc ateg oriz ed",
st ar Wars .get Cate g o ry( ) ) ;
}

Getting to green bar by doing the simplest thing results in:


pub l ic St rin g get Ca tego ry() {
r e turn "Un ca teg or ized ";
}

Test 36: If a movie is given a category when it is created, it answers that

when asked for its category


Now, when we are testing for the other categories, we need a way to set the category of a M ov i e. The
simplest thing is to add a constructor that takes a category string. So here is the test:
pub l ic vo id te stS ci ence Fict ion( ) {
M o vie a lie n = n ew Mov ie(" Alie n " , " S c ien c e F ic ti o n" ) ;
a s sertE qua ls ("a li en s houl d be S cie n c e F i c ti on ." ,
"S ci ence Fict ion" ,
al ie n.ge tCat egor y ( ));
}

We know we'll have to store the category value and return it from ge t Ca te g or y( ) , so let's take a
bigger step and just do it. And here's the new code:
pri v ate S tri ng ca te gory = n ull;
pub l ic Mo vie (S tri ng aNa me, Stri n g aC a t ego r y ) {
t h is(aN ame ,
- 1) ;
c a tegor y = a Cat eg ory;
}
pub l ic Mo vie (M ovi e orig inal ) {
n a me = ori gi nal .n ame;
r a ting = o ri gin al .rat ing;
c a tegor y = o rig in al.c ateg ory;
}
pub l ic St rin g get Ca tego ry() {
ret u rn (c ate go ry != nul l) ? cat e g ory : "U n c at eg or i ze d ";
}

Green bar. The constructors of M ovie are proliferating, so let's make a more general one and retrofit
our tests to use it:
pub l ic Mo vie (S tri ng aNa me, Stri n g aC a t ego r y , in t a Ra t in g) {
c h eckNu ll( aN ame );
c h eckEm pty (a Nam e) ;
n a me = aNa me ;
c a tegor y = ( aCa te gory != null ) ? a C a teg o r y : "U n ca t eg or i ze d" ;
r a ting = a Ra tin g;
}

While we're at it we notice that some refactoring can be applied to T es t Mo vi e . We do that as well,
removing unneeded instance creation from the tests and using st a rW a rs that is created in s e tU p( ) .

Exercises
1.

Refactor Test Movi e to use a single Constructor Method that we just added to
M ov ie.

2.

Refactor the tests again, this time removing the unneeded Mo v ie creation.

Test 37: Trying to create a movie with an invalid category (i.e., not from
the predefined set) throws an exception
Now, we need to enforce the constraint that only a fixed set of alternatives can be used:
pub l ic vo id te stB ad Cate gory () {
try {
Movie al ie n = n ew M ovie ("Al i e n", " Sci F i ",
- 1) ;
fail( "Ba d cat eg ory acce pted " ) ;
} catch (I ll ega lA rgum entE xcep t i on e x )
{
}
}

So how do we constrain the category values to a closed, fixed set of alternatives? We could use guard
statements and the string comparison:
pub l ic Mo vie (S tri ng aNa me, Stri n g aC a t ego r y , in t a Ra t in g) {
c h eckNu ll( aN ame );
c h eckEm pty (a Nam e) ;
c h eckCa teg or y(a Ca tego ry);
n a me = aNa me ;
c a tegor y = ( aCa te gory != null ) ? a C a teg o r y : "U n ca t eg or i ze d" ;
r a ting = a Ra tin g;
}
pri v ate v oid c hec kC ateg ory( Stri n g aC a t ego r y ) {
i f (aCa teg or y = = null ) re turn ;
i f (aCa teg or y.e qu als( "Unc ateg o r ize d " )) r e tu rn ;
i f (aCa teg or y.e qu als( "Sci ence F ict i o n") ) re tu rn ;
i f (aCa teg or y.e qu als( "Hor ror" ) ) re t u rn;
t h row n ew Il leg al Argu ment Exce p t ion ( " Bad c at eg or y : " + aC a te go r y) ;
}

This smells! Imagine changing the spelling of a category once we have a large, complex system built.
We could refactor the string literals to constants and use them everywhere:
pub l ic st ati c fin al Str ing UNCA T E GOR I Z ED = "U nc at e go r iz ed " ;
pub l ic st ati c fin al Str ing SCIF I = " S c ien c e F ic ti o n" ;
pub l ic st ati c fin al Str ing HORR O R = " H orr o r ";
pub l ic Mo vie (S tri ng aNa me, Stri n g aC a t ego r y , in t a Ra t in g) {
c h eckNu ll( aN ame );
c h eckEm pty (a Nam e) ;
c h eckCa teg or y(a Ca tego ry);
n a me = aNa me ;
c a tegor y = ( aCa te gory != null ) ? a C a teg o r y : UN C AT E GO RI Z ED ;
r a ting = a Ra tin g;
}
pri v ate v oid c hec kC ateg ory( Stri n g aC a t ego r y ) {
i f (aCa teg or y = = null ) re turn ;
i f (aCa teg or y.e qu als( UNCA TEGO R I ZED ) ) re t u rn ;
i f (aCa teg or y.e qu als( SCIF I)) r e tur n ;
i f (aCa teg or y.e qu als( HORR OR)) r etu r n ;

t h row n ew Il leg al Argu ment Exce p t ion ( " Bad c at eg or y : " + aC a te go r y) ;

The corresponding version of the tests look like:


pub l ic vo id te stU nc ateg oriz ed() {
a s sertE qua ls ("s ta rWar s sh ould b e u n c ate g o ri ze d. " ,
Mo vi e.UN CATE GORI Z E D,
st ar Wars .get Cate g o ry( ) ) ;
}
pub l ic vo id te stS ci ence Fict ion( ) {
M o vie a lie n = n ew Mov ie(" Alie n " ,Mo v i e.S C I FI ,
- 1) ;
a s sertE qua ls ("a li en s houl d be S cie n c e F i c ti on ." ,
Mo vi e.SC IFI,
al ie n.ge tCat egor y ( ));
}

This still has a smell. It would be nice to refactor this in a way that would let us leverage Java to
enforce the constraints for us. We can do that by refactoring to a type-safe enumeration. Joshua
Kerievsky discusses this refactoring in [29].
First, we create the type-safe enumeration class:
pub l ic cl ass C ate go ry {
p r ivate St ri ng na me=n ull;
pri v ate C ate go ry( St ring cat egor y N ame ) {
n a me = cat eg ory Na me;
}
pub l ic st ati c fin al Cat egor y UN C A TEG O R IZE D = ne w C at e go ry ( "U nc a te go r iz ed " );
pub l ic st ati c fin al Cat egor y SC I F I = n ew C a te go ry ( "S c ie nc e F ic t io n" ) ;
pub l ic st ati c fin al Cat egor y HO R R OR = new C at eg or y (" H or ro r ") ;
}

Next, we update the related tests. One advantage of using a type-safe enumeration is that we no longer
need to test for bad values; the compiler enforces that for us. Here are the remaining tests:
pub l ic vo id te stU nc ateg oriz ed() {
a s sertE qua ls ("s ta rWar s sh ould b e u n c ate g o ri ze d. " ,
Ca te gory .UNC ATEG O R IZE D ,
st ar Wars .get Cate g o ry( ) ) ;
}
pub l ic vo id te stS ci ence Fict ion( ) {
M o vie a lie n = n ew Mov ie(" Alie n " ,Ca t e gor y . SC IF I,
- 1) ;
a s sertE qua ls ("a li en s houl d be S cie n c e F i c ti on ." ,
Ca te gory .SCI FI,
al ie n.ge tCat egor y ( ));
}

And, finally, here are the affected bits of M o vie (note that the string constants have been deleted):
pri v ate C ate go ry ca tego ry = Cat e g ory . U NCA T E GO RI ZE D ;
pub l ic Mo vie (S tri ng aNa me, Cate g o ry a C ate g o ry , in t a R at in g ) {

c h eckNu ll( aN ame );


c h eckEm pty (a Nam e) ;
n a me = aNa me ;
c a tegor y = ( aCa te gory != null ) ? a C a teg o r y : Ca t eg o ry .U N CA TE G OR IZ E D;
r a ting = a Ra tin g;

pub l ic Ca teg or y g et Cate gory () {


r e turn cat eg ory ;
}

Now we can go back to the customer and get a list of the categories that should be supported and add
them to Cate gor y.

Exercises
3.

The customer identified these categories: Science Fiction, Horror, Comedy, Western,
Drama, Fantasy, Kids, Adult, Mystery, Thriller. Add these to C a te go r y.

Amazon

Prev

don't be afraid of buying books

Next

SHOW THE CATEGORY IN THE GUI


Show the category in the GUI. There should be a field that gets filled in when a movie is
selected.
Now that we have categories supported in M o v i e , we can add support for them to the interface layers.
We'll start with these tests:
Test 38. Telling the logical layer that a movie is selected causes the presentation layer to
be told the category to display.

Test 39. Selecting a movie in the GUI causes the category field to be updated.

To get the category to reflect on the interface, we'll start, as usual, with M o v i e L i s t - E d i t o r .

Test 38: Telling the logical layer that a movie is selected causes the
presentation layer to be told the category to display
We start by extending test S elec ting :
p ub l ic v oid test S elec t ing( ) {
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d(st arWa r s . g e t N a m e ( ) ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 6);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etCa t egor y Fiel d(Ca t e g o r y . S C I F I ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d(st arTr e k . g e t N a m e ( ) ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 4);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etCa t egor y Fiel d(Ca t e g o r y . S C I F I ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d(st arga t e . g e t N a m e ( ) ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 0);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etCa t egor y Fiel d(Ca t e g o r y . S C I F I ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d(th eShi n i n g . g e t N a m e ( ) ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 3);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etCa t egor y Fiel d(Ca t e g o r y . H O R R O R ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
e d it or .se l ect( 0 );
e d it or .se l ect( 1 );

e d it or .se l ect( 2 );
e d it or .se l ect( 3 );
}

c o nt ro l.v e rify ( );

To support this, we have to update set U p ( ) as well. Note: We don't need to create a new fixture
(which would require a new T e stCa se), but the current fixture needs to be more complete. Here it is:
p ro t ec te d v o id s e tUp( ) thr ows D u p l i c a t e M o v i e E x c e p t i o n {
s t ar Wa rs = new Movi e ("St ar W a r s " , C a t e g o r y . S C I F I , 5 ) ;
s t ar Tr ek = new Movi e ("St ar T r e k " , C a t e g o r y . S C I F I , 3 ) ;
s t ar ga te = new Movi e ("St arga t e " , C a t e g o r y . S C I F I ,
- 1) ;
t h eS hi nin g = n e w Mo v ie(" The S h i n i n g " , C a t e g o r y . H O R R O R , 2 ) ;
m o vi es =n e wVec t or() ;
m o vi es .ad d (sta r Wars ) ;
m o vi es .ad d (sta r Trek ) ;
m o vi es .ad d (sta r gate ) ;
m o vi es .ad d (the S hini n g);
m o vi eL ist = newM o vieL i st() ;
m o vi eL ist . add( s tarW a rs);
m o vi eL ist . add( s tarT r ek);
m o vi eL ist . add( s targ a te);
m o vi eL ist . add( t heSh i ning );
c o nt ro l = Easy M ock. c ontr olFo r ( M o v i e L i s t E d i t o r V i e w . c l a s s ) ;
m o ck Vi ew = (Mo v ieLi s tEdi torV i e w ) c o n t r o l . g e t M o c k ( ) ;

m o ck Vi ew. s etEd i tor( n ull) ;


c o nt ro l.s e tDef a ultV o idCa llab l e ( ) ;

Now we have the test. Let's make it compile and get to the red bar as quickly as we can. We need to
add a se tCa t egor y Fiel d () method to M o v i e L i s t E d i t o r V i e w . Now we have the red bar we were
aiming for.

Exercises
4.

Extend s et Cate goryF i e l d ( ) to make t e s t S e l e c t i n g ( ) pass.

5.

What problem does this cause? Why? Fix it.

Green bar! To the Swing interface layer.

Test 39: Selecting a movie in the GUI causes the category field to be
updated
First, we need a test that checks that selecting updates the category field as expected:
p ub l ic v oid test S elec t Upda tesC a t e g o r y ( ) {
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
J T ex tF iel d Oper a tor c ateg oryF i e l d = n e w J T e x t F i e l d O p e r a t o r ( m a i n W i n d o w ,
"category");
m o vi eL ist . clic k OnIt e m(0, 1);

a s se rt Equ a ls(" w rong cate gory f r o m s e l e c t i n g s t a r W a r s . " ,


C a tego r y.SC IFI. t o S t r i n g ( ) ,
c a tego r yFie ld.g e t T e x t ( ) ) ;

m o vi eL ist . clic k OnIt e m(3, 1);


a s se rt Equ a ls(" w rong cate gory f r o m s e l e c t i n g t h e S h i n i n g . " ,
C a tego r y.HO RROR . t o S t r i n g ( ) ,
c a tego r yFie ld.g e t T e x t ( ) ) ;

m o vi eL ist . clic k OnIt e m(1, 1);


a s se rt Equ a ls(" w rong cate gory f r o m s e l e c t i n g s t a r T r e k . " ,
C a tego r y.SC IFI. t o S t r i n g ( ) ,
c a tego r yFie ld.g e t T e x t ( ) ) ;

Now we need to add a JTex t Fiel d for displaying the category:


p ri v at e JTe x tFie l d ca t egor yFie l d = n u l l ;
p ub l ic v oid setC a tego r yFie ld(C a t e g o r y a C a t e g o r y ) {
c a te go ryF i eld. s etTe x t(aC ateg o r y . t o S t r i n g ( ) ) ;
}
p ub l ic v oid init ( ) {
s e tT it le( ) ;
s e tL ay out ( );
i n it Li st( ) ;
i n it Na meF i eld( ) ;
i n it Ra tin g Comb o ();
i n it Ca teg o ryFi e ld() ;
i n it Ad dBu t ton( ) ;
i n it Up dat e Butt o n();
p a ck () ;
}
p ri v at e voi d ini t Cate g oryF ield ( ) {
c a te go ryF i eld = new JTex tFie l d ( 1 6 ) ;
c a te go ryF i eld. s etTe x t("c ateg o r y " ) ;
g e tC on ten t Pane ( ).ad d (cat egor y F i e l d ) ;
}

Green bar. Next.

Exercises
6.

We used the t oStr ing ( ) method to get the value for the category field, as well as
the value from the expected C a t e g o r y to compare against the field contents. What's
the problem that we have with the system in its current state? (Hint: look at
Ca t ego r y.) Fix it.

Amazon

Prev

don't be afraid of buying books

Next

ADD A SELECTION OF CATEGORY


Allow the category of a movie to be changed by selecting it from a fixed list of
possibilities.
For this task we need only be concerned with the interface layers. Here's our list of tests:
Test 40. Telling the logical layer to update and providing it with data that indicates a
category change results in the GUI layer being given a new set of movies with that
change reflected.

Test 41. Selecting a movie from the list, changing the value of the category, and
pressing Update updates the data for that movie. When that movie is selected again, the
new category is displayed.

Here we go, then. As usual, we'll start at the logical layer.

Test 40: Telling the logical layer to update and providing it with data
that indicates a category change results in the GUI layer being given a
new set of movies with that change reflected
For this test we can extend T e stMo vie L i s t E d i t o r . t e s t U p d a t i n g ( ) to check that the movie
category gets changed:
p ub l ic v oid test U pdat i ng() {
V e ct or ne w Movi e s = n ew V ecto r ( ) ;
n e wM ov ies . add( s tarW a rs);
n e wM ov ies . add( n ew M o vie( "Sta r T r e k I " , C a t e g o r y . C O M E D Y , 5 ) ) ;
n e wM ov ies . add( s targ a te);
n e wM ov ies . add( t heSh i ning );
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etNa m eFie l d("S tar T r e k " ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. s etRa t ingF i eld( 4);
c o nt ro l.s e tVoi d Call a ble( );
m o ck Vi ew. s etCa t egor y Fiel d(Ca t e g o r y . S C I F I ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. g etNa m eFie l d();
c o nt ro l.s e tRet u rnVa l ue(" Star T r e k I " , 1 ) ;
m o ck Vi ew. g etRa t ingF i eld( );
c o nt ro l.s e tRet u rnVa l ue(6 , 1) ;
m o ck Vi ew. g etCa t egor y Fiel d();
c o nt ro l.s e tRet u rnVa l ue(C ateg o r y . C O M E D Y , 1 ) ;
m o ck Vi ew. s etMo v ies( n ewMo vies ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
e d it or .se l ect( 1 );
e d it or .up d ate( ) ;

c o nt ro l.v e rify ( );

Getting this to compile requires the addition of g e t C a t e g o r y F i e l d ( ) to the M o v i e L i s t E d i t o r V i e w


interface:
p ub l ic i nte r face Movi e List Edit o r V i e w {
v o id s etM o vies ( Vect o r mo vies ) ;
S t ri ng ge t Name F ield ( );
v o id s etE d itor ( Movi e List Edit o r a n E d i t o r ) ;
v o id s etN a meFi e ld(S t ring str i n g ) ;
v o id d upl i cate E xcep t ion( Stri n g s t r i n g ) ;
v o id s etR a ting F ield ( int i);
i n t ge tRa t ingF i eld( ) ;
v o id s etC a tego r yFie l d(Ca tego r y a C a t e g o r y ) ;
C a te go ry g etCa t egor y Fiel d();
}

Now to get a green bar. We'll start by updating M o v i e L i s t E d i t o r . u p d a t e M o v i e ( ) to fetch the


category from the view and use that to set the category of the selected M o v i e :
p ri v at e voi d upd a teMo v ie() {
s e le ct edM o vie. s etRa t ing( view . g e t R a t i n g F i e l d ( )
- 1);
s e le ct edM o vie. s etCa t egor y(vi e w . g e t C a t e g o r y F i e l d ( ) ) ;
u p da te Mov i eLis t ();
}

This in turn requires a s etCa t egor y() mutator in M o v i e , which we won't bother showing here. Next,
we need to add an expectation to test U p d a t i n g W i t h S a m e N a m e ( ) for the call to
g et C at eg ory F ield ( ). OK. Green bar!

Exercises
7.

Make the required changes to M o v i e and T e s t M o v i e L i s t E d i t o r .

Test 41: Selecting a movie from the list, changing the value of the
category, and pressing Update updates the data for that movie. When
that movie is selected again, the new category is displayed
Now that we've finished with the logic layer for this task, we can turn to the Swing layer. We'll start by
adding a test for updating the category:
p ub l ic v oid test U pdat e Cate gory ( ) {
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
J C om bo Box O pera t or c a tego ryCo m b o =
ne w J C ombo B oxOp e rato r(ma i n W i n d o w ,
Cat e g o r y . U N C A T E G O R I Z E D . t o S t r i n g ( ) ) ;
m o vi eL ist . clic k OnIt e m(0, 1);
c a te go ryC o mbo. s etSe l ecte dInd e x ( 2 ) ;
J B ut to nOp e rato r upd a teBu tton = n e w J B u t t o n O p e r a t o r ( m a i n W i n d o w , " U p d a t e " ) ;
u p da te But t on.p u shNo B lock ();
m o vi eL ist . clic k OnIt e m(1, 1);

m o vi eL ist . clic k OnIt e m(0, 1);


a s se rt Equ a ls(" u pdat i ng s houl d h a v e c h a n g e d c a t e g o r y . " ,
C ateg o ry.H ORRO R ,
c ateg o ryCo mbo. g e t S e l e c t e d I t e m ( ) ) ;

First, we need to change the category field to a combo box:


p ri v at e JCo m boBo x cat e gory Fiel d = n u l l ;
p ub l ic v oid setC a tego r yFie ld(C a t e g o r y a C a t e g o r y ) {
c a te go ryF i eld. s etSe l ecte dIte m ( a n O b j e c t ) ( a C a t e g o r y ) ;
}
p ub l ic C ate g ory g etCa t egor yFie l d ( ) {
r e tu rn (C a tego r y) c a tego ryFi e l d . g e t S e l e c t e d I t e m ( ) ;
}
p ri v at e voi d ini t Cate g oryF ield ( ) {
c a te go ryF i eld = new JCom boBo x ( C a t e g o r y . c a t e g o r i e s ( ) ) ;
c a te go ryF i eld. s etSe l ecte dIte m ( C a t e g o r y . U N C A T E G O R I Z E D ) ;
g e tC on ten t Pane ( ).ad d (cat egor y F i e l d ) ;
}

This requires some slight changes to Ca t e g o r y to collect and fetch all the defined categories:
p ri v at e sta t ic V e ctor allC ateg o r i e s = n e w V e c t o r ( ) ;
p ri v at e Cat e gory ( Stri n g ca tego r y N a m e ) {
n a me = ca t egor y Name ;
a l lC at ego r ies. a dd(t h is);
}
p ub l ic s tat i c Ve c tor c ateg orie s ( ) {
r e tu rn (V e ctor ) all C ateg orie s . c l o n e ( ) ;
}

OK, that gets our new test to pass, but we've broken our earlier t e s t S e l e c t U p - d a t e s C a t e g o r y test.
It needs to be rewritten to use a combo box for the category field:
p ub l ic v oid test S elec t Upda tesC a t e g o r y ( ) {
J L is tO per a tor m ovie L ist = ne w J L i s t O p e r a t o r ( m a i n W i n d o w ) ;
J C om bo Box O pera t or c a tego ryFi e l d =
ne w J C ombo B oxOp e rato r(ma i n W i n d o w ,
C ateg o r y . U N C A T E G O R I Z E D . t o S t r i n g ( ) ) ;
m o vi eL ist . clic k OnIt e m(0, 1);
a s se rt Equ a ls(" w rong cate gory f r o m s e l e c t i n g s t a r W a r s . " ,
C a tego r y.SC IFI,
c a tego r yFie ld.g e t S e l e c t e d I t e m ( ) ) ;
m o vi eL ist . clic k OnIt e m(3, 1);
a s se rt Equ a ls(" w rong cate gory f r o m s e l e c t i n g t h e S h i n i n g . " ,
C a tego r y.HO RROR ,
c a tego r yFie ld.g e t S e l e c t e d I t e m ( ) ) ;

m o vi eL ist . clic k OnIt e m(1, 1);


a s se rt Equ a ls(" w rong cate gory f r o m s e l e c t i n g s t a r T r e k . " ,
C a tego r y.SC IFI,
c a tego r yFie ld.g e t S e l e c t e d I t e m ( ) ) ;

Green bar! We're done. Figure 14.1 shows the current GUI.

Figure 14.1. GUI with categories.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
There wasn't much that was really new in this chapter. We did make use of a type-safe enumeration to
encapsulate a closed, fixed set of alternative values. There's a good discussion of this pattern in [10],
and one of the refactorings in [29] deals with replacing a type with a type-safe enumeration.

Amazon

Prev

don't be afraid of buying books

Chapter 15. FILTER ON CATEGORY


Filter the movies in the list by category. There needs to be a way to select the category
to show, including ALL.

Next

Amazon

Prev

don't be afraid of buying books

Next

GET A SUBLIST BASED ON CATEGORY


Given a category, ask the movie list for a list of its movies that belong to that category.
Now we need to have Mov ieLis t generate sublists based on categories. Here are some tests:
Test 42. Requesting a sublist filtered on a specific category answers a list containing all
movies of that category, and only those movies.

Let's start by setting up a fixture with a M ovi e L ist containing a selection of movies with various
categories and another M ov ieLi st containing just fantasy movies:
pro t ected vo id se tU p() thro ws E x c ept i o n {
s t arWar s = n ew Mo vie( "Sta r Wa r s ", C a teg o r y. SC IF I , 5 );
s t arTre k = n ew Mo vie( "Sta r Tr e k ", C a teg o r y. SC IF I , 3 );
s t argat e = n ew Mo vie( "Sta rgat e " , C a t ego r y .S CI FI , - 1 );
t h eShin ing = ne w Movi e("T he S h i nin g " , C a t eg or y. H OR R OR , 2 );
c a rrie = n ew Mo vi e("C arri e", C a teg o r y.H O R RO R, 3 ) ;
f o tr = new M ovi e( "The Fel lows h i p o f The R in g" , C at e go ry . FA NT A SY , 5 );
r e dOcto ber = ne w Movi e("T he H u n t F o r Re d Oc to be r ", Ca te g or y. T HR IL L ER , 3 );
c o ngo = ne w Mov ie ("Co ngo" , Ca t e gor y . THR I L LE R, 3 ) ;
p r inces sBr id e = n ew M ovie ("Th e Pri n c ess B ri de ", Ca t eg or y .F AN T AS Y, 5) ;
m o vieLi st. ad d(s ta rWar s);
m o vieLi st. ad d(s ta rTre k);
m o vieLi st. ad d(s ta rgat e);
m o vieLi st. ad d(t he Shin ing) ;
m o vieLi st. ad d(c ar rie) ;
m o vieLi st. ad d(f ot r);
m o vieLi st. ad d(r ed Octo ber) ;
m o vieLi st. ad d(c on go);
m o vieLi st. ad d(p ri nces sBri de);

f a ntasy Lis t= new Mo vieL ist( );


f a ntasy Lis t. add (f otr) ;
f a ntasy Lis t. add (p rinc essB ride ) ;

Test 42: Requesting a sublist filtered on a specific category answers a


list containing all movies of that category, and only those movies
pub l ic vo id te stS ub sets () {

a s sertE qua ls ("w ro ng F ANTA SY s u b lis t . ",


fa nt asyL ist,
mo vi eLis t.ca tego r y Sub l i st( C a te go ry . FA N TA SY ) );

To get this compiled we need to add a stub for Mov i e Lis t .c a te go r yS u bl is t () :


pub l ic Ob jec t cat eg oryS ubli st(C a t ego r y ca t e go ry ) {
r e turn nul l;
}

Compile, run, red bar. Now to make it green. But first, consider the failure message:
wro n g FAN TAS Y sub li st.
expec ted :< com .s aors a.no wpla y i ng. M o vie L i st @d 55 5 0d >
but was :< nul l>

That doesn't tell us much other than something was expected, but nothing was provided. The instance
identifier of the expected Mo vieL ist is essentially unreadable, and generally meaningless. We can make
that more useful by adding a reasonable toS t r ing ( ) to Mo vi e Li st :
wro n g FAN TAS Y sub li st.
expec ted :< ["T he Fel lows hip o f Th e Rin g " " Th e P ri n ce ss Br id e "] >
but was :< nul l>

Exercises
8.

Write a to Stri ng() method for M ovi e L is t.

OK, back to the pursuit of the green bar. The simplest thing is to make a new M ov ie L is t and
selectively add movies to it that match the filter criteria, in this case, a category. We'll move a bit faster
this time and skip the fake it step. I can imagine a time when we might want something more elaborate,
possibly using a Decorator pattern where you could decorate a Mo vi e Li s t with multiple filters and/or
sorters. Such a possibility is shown in Figure 15.1. This would involve a class structure something like
that shown in Figure 15.2. For now, though, the simplest thing will suffice:
pub l ic Mo vie Li st ca tego ry(C ateg o r y a C a teg o r y) {
M o vieLi st fi lte re dLis t = new M o vie L i st( ) ;
I t erato r m ov ieI te rato r = movi e s .it e r ato r ( );

w h ile ( mov ie Ite ra tor. hasN ext( ) ) {


Movie aM ov ie = (Mov ie)m ovie I t era t o r.n e x t( );
i f (aMo vie .i sOf Ca tego ry(a Cate g o ry) ) {
try {
fil ter ed Lis t. add( aMov ie);
} cat ch (D upl ic ateM ovie Exce p t ion e ) {
}
}

r e turn fil te red Li st;

Figure 15.1. Example of decorator style filters.

Figure 15.2. Classes for decorator style filters.

The bar's still red:


wro n g FAN TAS Y sub li st.
e x pecte d:< [" The F ello wshi p of T he R i ng" " Th e Pr i nc e ss B r id e" ] >
b ut wa s:< [" The F ello wshi p of T he R i ng" " Th e Pr i nc e ss B r id e" ] >

Looks like we need an eq ua ls() method for Mov i e Lis t . Sure enough, so we'll write one.

Exercises
9.

Write an equ als( ) method for Mov i e Lis t . Start by adding a test to
T es tMo vi eLis t-Wi thPo p u lat e d Lis t .

That does itgreen bar! Now beef up the test to check for other subsets:
pub l ic vo id te stS ub sets () {
a s sertE qua ls ("w ro ng F ANTA SY s u b lis t . ",
fa nt asyL ist,
mo vi eLis t.ca tego r y Sub l i st( C a te go ry . FA N TA SY ) );
a s sertE qua ls ("w ro ng S CIFI sub l i st. " ,
sc if iLis t,
mo vi eLis t.ca tego r y Sub l i st( C a te go ry . SC I FI )) ;
a s sertE qua ls ("w ro ng H ORRO R su b l ist . " ,
ho rr orLi st,
ho rr orLi st.c ateg o r ySu b l ist ( C at eg or y .H O RR OR ) );

a s sertE qua ls ("w ro ng T HRIL LER s u bli s t .",


th ri ller List ,
mo vi eLis t.ca tego r y Sub l i st( C a te go ry . TH R IL LE R )) ;

Exercises
10.

Extend the fixture to support this test.

Amazon

Prev

don't be afraid of buying books

Next

SUPPORT AN ALL CATEGORY


Get the entire list using an ALL category.
We can now ask a Mo vi eLis t to return a sublist for a given category. The story requires that we be
able to fetch the entire list for the ALL category. Here's the test:
Test 43. Asking for a subset for the ALL category answers the original list.

Test 43: Asking for a subset for the ALL category answers the original
list
pub l ic vo id te stA ll Subs et() {
a s sertE qua ls ("A ll sub set shou l d eq u a l t h e f ul l l is t ." ,
mo vi eLis t,
mo vi eLis t.ca tego r y Sub l i st( C a te go ry . AL L )) ;
}

To compile we need to add an ALL instance to C ate g o ry :


pub l ic st ati c fin al Cat egor y AL L = new C a teg o r y( "A ll " );

Red bar. Now we need to update c atego r ySu b l ist ( ) to handle the A LL category:
pub l ic Mo vie Li st ca tego rySu blis t ( Cat e g ory a Ca te go r y) {
i f (aCa teg or y.e qu als( Cate gory . A LL) ) {
retur n t hi s;
}
M o vieLi st fi lte re dLis t = new M o vie L i st( ) ;
I t erato r m ov ieI te rato r = movi e s .it e r ato r ( );
w h ile ( mov ie Ite ra tor. hasN ext( ) ) {
Movie aM ov ie = (Mov ie)m ovie I t era t o r.n e x t( );
if (a Mov ie .is Of Cate gory (aCa t e gor y ) ) {
try {
f ilt er edL is t.ad d(aM ovie ) ;
} cat ch (D upl ic ateM ovie Exce p t ion e ) {
}
}

}
r e turn fil te red Li st;

Green bar. That was an easy one!

Amazon

Prev

don't be afraid of buying books

Next

ADD A CATEGORY SELECTOR TO THE GUI


Add a combo box to the GUI to select a category. When a category is selected, update
the list to include only movies of that category.
Now another GUI task. We need to extend the GUI logic layer to handle category changes and respond
by filtering the displayed list of movies.
Test 44. When the logical layer is told to filter on a specific category, the presentation
layer is given a new list to display containing movies for the specified category.

Test 45. Telling the logical layer to select a specific movie in a filtered list rather than
the complete list from actually selects the appropriate movie, in spite of being selected
from a sublist.

Test 46. Telling the logical layer to update a specific movie in a filtered list rather than
from the complete list actually updates the appropriate movie properly.

Test 47. A rename performed in the context of the logical layer is done in the underlying
full list; that is, potential duplicates in the full list are detected.

Test 48. Selecting a category to filter on in the GUI causes the displayed list of movies
to update accordingly.

Since we need movies with assorted categories, we'll start by creating a new Te st C as e to hold a more
elaborate fixture, rather than extending the one we have:
pub l ic cl ass T est Mo vieL istE dito r W ith C a teg o r yF il te r in g e xt e nd s T es tC a se {
p r ivate Mo ck Con tr ol c ontr ol=n u l l;
p r ivate Mo vi eLi st Edit orVi ew m o c kVi e w = n u ll ;
p r ivate
p r ivate
p r ivate
p r ivate
p r ivate
p r ivate
p r ivate
p r ivate
p r ivate

Mo vi e
Mo vi e
Mo vi e
Mo vi e
Mo vi e
Mo vi e
Mo vi e
Mo vi e
Mo vi e

s ta rWar s = null ;
s ta rTre k = null ;
s ta rgat e = null ;
t he Shin ing = nu l l ;
c ar rie = nu ll;
f ot r = null ;
r ed Octo ber = nu l l ;
c on go=n ull;
p ri nces sBri de = n ull ;

p r ivate
p r ivate
p r ivate
p r ivate
p r ivate

Mo vi eLi st
Mo vi eLi st
Mo vi eLi st
Mo vi eLi st
Mo vi eLi st

p r ivate
p r ivate
p r ivate
p r ivate
p r ivate

Ve ct or
Ve ct or
Ve ct or
Ve ct or
Ve ct or

mov ieLi st = n ull ;


fan tasy List = n ull ;
sci fiLi st = n ull ;
thr ille rLis t = n u l l;
hor rorL ist = nul l ;

mo vies = n ull;
fa ntas yMov ies = nul l ;
ho rror Movi es=n u l l;
th rill erMo vies = nu l l ;
sc ifiM ovie s = n u ll;

p r otect ed vo id se tUp( ) th rows E xce p t ion {


super .se tU p() ;
starW ars = ne w Movi e("S tar W a rs" , Cat e g or y. SC I FI , 5 );
starT rek = ne w Movi e("S tar T r ek" , Cat e g or y. SC I FI , 3 );
starg ate = ne w Movi e("S targ a t e", C ate g o ry .S CI F I, -1 );
theSh ini ng = ne w Mo vie( "The S hin i n g", C at eg or y .H O RR OR , 2 );
carri e = n ew Mo vie( "Car rie" , Cat e g ory . H OR RO R, 3) ;
fotr = n ew Mo vi e("T he F ello w s hip o f T h e R in g" , C a te go r y. FA N TA SY , 5 );
redOc tob er = ne w Mo vie( "The H unt F or R e d Oc to b er " , Ca t eg or y .T HR I LL ER , 3 );
congo = ne w M ov ie(" Cong o", C a teg o r y.T H R IL LE R, 3) ;
princ ess Br ide = new Mov ie(" T h e P r i nce s s B ri de " , C at eg o ry .F A NT AS Y , 5) ;
movie Lis t = n ew Movi eLis t();
movie Lis t. add (s tarW ars) ;
movie Lis t. add (s tarT rek) ;
movie Lis t. add (s targ ate) ;
movie Lis t. add (t heSh inin g);
movie Lis t. add (c arri e);
movie Lis t. add (f otr) ;
movie Lis t. add (r edOc tobe r);
movie Lis t. add (c ongo );
movie Lis t. add (p rinc essB ride ) ;
movie s = n ew Ve ctor (mov ieLi s t .ge t M ovi e s () );

scifi Lis t = n ew Mov ieLi st() ;


scifi Lis t. add (s tarW ars) ;
scifi Lis t. add (s tarT rek) ;
scifi Lis t. add (s targ ate) ;
scifi Mov ie s = n ew V ecto r(sc i f iLi s t .ge t M ov ie s( ) );
thril ler Li st = new Movi eLis t ( );
thril ler Li st. ad d(re dOct ober ) ;
thril ler Li st. ad d(co ngo) ;
thril ler Mo vie s = ne w Ve ctor ( t hri l l erL i s t. ge tM o vi e s( )) ;
horro rLi st = ne w Mo vieL ist( ) ;
horro rLi st .ad d( theS hini ng);
horro rLi st .ad d( carr ie);
horro rMo vi es = new Vect or(h o r ror L i st. g e tM ov ie s () ) ;
fanta syL is t=n ew Movi eLis t();
fanta syL is t.a dd (fot r);
fanta syL is t.a dd (pri nces sBri d e );
fanta syM ov ies = new Vec tor( f a nta s y Lis t . ge tM ov i es ( )) ;

contr ol = Eas yM ock. cont rolF o r (Mo v i eLi s t Ed it or V ie w .c la s s) ;


mockV iew = (M ov ieLi stEd itor V i ew) c o ntr o l .g et Mo c k( ) ;
mockV iew .s etE di tor( null );
contr ol. se tDe fa ultV oidC alla b l e() ;

Test 44: When the logical layer is told to filter on a specific category,
the presentation layer is given a new list to display containing movies
for the specified category
We need a test that will start with the entire list, select a category, then select the AL L category to get
the entire list back. Here's the test:
pub l ic vo id te stC al egor yFil teri n g () {
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(sc ifiM ovie s ) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
c o ntrol .ac ti vat e( );
M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, m oc kV i ew );
e d itor. fil te rOn Ca tego ry(C ateg o r y.S C I FI) ;
e d itor. fil te rOn Ca tego ry(C ateg o r y.A L L );
}

c o ntrol .ve ri fy( );

Add a stub for fi lte rO nCat egor y() to get to the red bar. It fails because the stub does nothing;
specifically, it does not cause a corresponding call to se t Mo v ie s( ) .We need to fill it in:
pub l ic vo id fi lte rO nCat egor y(Ca t e gor y cat e g or y) {
f i ltere dMo vi es = movi es.c ateg o r ySu b l ist ( c at eg or y );
u p dateM ovi eL ist () ;
}

We also need to add a field to hold the filtered movie list, fi l te re d Mo v ie s, to maintain and use to
fill the list in the view.

Exercises
11.

Make the required updates to add f i lte r ed M ov ie s .

Green bar.

Test 45: Telling the logical layer to select a specific movie in a filtered
list rather than the complete list from actually selects the appropriate
movie, in spite of being selected from a sublist

The next test verifies that selecting a movie in a filtered list is really selecting the appropriate movie:
pub l ic vo id te stS el ecti ng() {
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(fa ntas yMov i e s);
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Nam eF ield (fot r.ge t N ame ( ) );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Rat in gFie ld(6 );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Cat eg oryF ield (Cat e g ory . F ANT A S Y) ;
c o ntrol .se tV oid Ca llab le(1 );
c o ntrol .ac ti vat e( );
M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, m oc kV i ew );
e d itor. fil te rOn Ca tego ry(C ateg o r y.F A N TAS Y ) ;
e d itor. sel ec t(0 );
}

c o ntrol .ve ri fy( );

When we run it, it fails. We need to look at M o v ieL i s tEd i to r .s el e ct ( ). There we see that movie
selection is still dealing with the underlying movie list, not the filtered one. We need to change that:
pub l ic vo id se lec t( int i) {
i f (i = =
- 1) {
selec ted Mo vie = nul l;
} else {
selec ted Mo vie = fil tere dMov i e s.g e t Mov i e (i );
view. set Na meF ie ld(s elec tedM o v ie. g e tNa m e () );
view. set Ca teg or yFie ld(s elec t e dMo v i e.g e t Ca te go r y( ) );
try {
vie w.s et Rat in gFie ld(s elec t e dMo v i e.g e t Ra ti ng ( ) + 1 );
} cat ch (U nra te dExc epti on e ) {
vie w.s et Rat in gFie ld(0 );
}
}
}

That does it.

Test 46: Telling the logical layer to update a specific movie in a filtered
list rather than from the complete list actually updates the appropriate
movie properly
Another test to make sure that the existing functionality still works ok:
pub l ic vo id te stU pd atin g() thro w s Un r a ted E x ce pt io n {
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(fa ntas yMov i e s);
c o ntrol .se tV oid Ca llab le(2 );

m o ckVie w.s et Nam eF ield (pri nces s B rid e . get N a me () );


c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Rat in gFie ld(6 );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Cat eg oryF ield (Cat e g ory . F ANT A S Y) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.g et Nam eF ield ();
c o ntrol .se tR etu rn Valu e(pr ince s s Bri d e .ge t N am e( ), 1) ;
m o ckVie w.g et Rat in gFie ld() ;
c o ntrol .se tR etu rn Valu e(2, 1);
m o ckVie w.g et Cat eg oryF ield ();
c o ntrol .se tR etu rn Valu e(Ca tego r y .FA N T ASY , 1) ;
c o ntrol .ac ti vat e( );

M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, m oc kV i ew );


e d itor. fil te rOn Ca tego ry(C ateg o r y.F A N TAS Y ) ;
e d itor. sel ec t(1 );
e d itor. upd at e() ;
c o ntrol .ve ri fy( );
a s sertE qua ls ("p ri nces sBri de s h o uld h ave b ee n ch a ng e d to ra ti n g of 1. ",
1,
p ri nces sBri de.g e t Rat i n g() ) ;

Green bar. That's good. One more test and I'll be happy.

Test 47: A rename performed in the context of the logical layer is done
in the underlying full list; that is, potential duplicates in the full list are
detected
This one will test that renames are being done in the context of the master list, not the filtered one.
We'll do this by selecting a sublist and renaming one of the movies in it to be a duplicate of a movie
not in the sublist. This should cause an error:
pub l ic vo id te stD up lica teCa usin g U pda t e () {
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(fa ntas yMov i e s);
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Nam eF ield (pri nces s B rid e . get N a me () );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Rat in gFie ld(6 );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Cat eg oryF ield (Cat e g ory . F ANT A S Y) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.g et Nam eF ield ();
c o ntrol .se tR etu rn Valu e(st arWa r s .ge t N ame ( ) , 1) ;
m o ckVie w.d up lic at eExc epti on(" S t ar W a rs" ) ;
c o ntrol .se tV oid Ca llab le(1 );
c o ntrol .ac ti vat e( );
M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, m oc kV i ew );
e d itor. fil te rOn Ca tego ry(C ateg o r y.F A N TAS Y ) ;
e d itor. sel ec t(1 );
e d itor. upd at e() ;

c o ntrol .ve ri fy( );

It runs fine. Now I'm confident.


As usual, we need to extend category selection to the Swing GUI. Like the last test, we'll create a new
fixture/TestC ase , very similar to the last.

Test 48: Selecting a category to filter on in the GUI causes the displayed
list of movies to update accordingly
Here's the test:
pub l ic vo id te stC at egor yFil teri n g () {
J L istOp era to r m ov ieLi st = new J Lis t O per a t or (m ai n Wi n do w) ;
J C omboB oxO pe rat or cat egor yCom b o =
new JC om boB ox Oper ator (mai n W ind o w ,
Cate gory . A LL. t o Str i n g( )) ;
c a tegor yCo mb o.s et Sele cted Item ( C ate g o ry. F A NT AS Y) ;
L i stMod el fa nta sy List Mode l = m o vie L i st. g e tM od el ( );
a s sertE qua ls ("F an tasy lis t is t he w r ong s iz e" ,
fa nt asyM ovie s.si z e (),
fa nt asyL istM odel . g etS i z e() ) ;
f o r (in t i = 0; i
< f a ntasy Mov ie s.s iz e(); i++ ) {
asse rtE qu als (" Fant asy list c ont a i ns b a d mo vi e a t i nd e x " + i,
f anta syMo vies . g et( i ) ,
f anta syLi stMo d e l.g e t Ele m e nt At (i ) );
}
c a tegor yCo mb o.s et Sele cted Item ( C ate g o ry. T H RI LL ER ) ;
L i stMod elt hr ill er List Mode l = m o vie L i st. g e tM od el ( );
a s sertE qua ls ("T hr ille r li st i s the w ron g si ze ",
th ri ller Movi es.s i z e() ,
th ri ller List Mode l . get S i ze( ) ) ;
f o r (in t i = 0; i
< t h rille rMo vi es. si ze() ; i+ +) {
asse rtE qu als (" Thri ller lis t con t a ins b ad m ov i e a t in d ex " +i ,
t hril lerM ovie s . get ( i ),
t hril lerL istM o d el. g e tEl e m en tA t( i )) ;
}
c a tegor yCo mb o.s et Sele cted Item ( C ate g o ry. A L L) ;
L i stMod el al lLi st Mode l = movi e L ist . g etM o d el () ;
a s sertE qua ls ("M ov ie l ist is t h e wr o n g s i z e" ,
mo vi es.s ize( ),
al lL istM odel .get S i ze( ) ) ;
f o r (in t i = 0; i
< mo v ies.s ize () ; i ++ ) {
asser tEq ua ls( "M ovie lis t co n t ain s bad m ov ie a t i n de x " + i,
mo vies .get (i),
al lLis tMod el.g e t Ele m e ntA t ( i) );
}
}

This immediately gives us a red bar since there is no combo box for selecting the category on which to
filter. That's easy enough to add:

pub l ic vo id in it( ) {
s e tTitl e() ;
s e tLayo ut( );
i n itCat ego ry Fil te rFie ld() ;
i n itLis t() ;
i n itNam eFi el d() ;
i n itRat ing Co mbo () ;
i n itCat ego ry Fie ld ();
i n itAdd But to n() ;
i n itUpd ate Bu tto n( );
p a ck();
}
pri v ate v oid i nit Ca tego ryFi lter F i eld ( ) {
c a tegor yFi el d = n ew J Comb oBox ( C ate g o ry. c a te go ri e s( ) );
c a tegor yFi el d.s et Sele cted Item ( C ate g o ry. A L L) ;
g e tCont ent Pa ne( ). add( cate gory F i eld ) ;
}

Now we get a red bar due to the list box not getting updated when the category filter setting changes.
We need to add a selection handler to the new combo box:
pri v ate v oid i nit Ca tego ryFi lter F i eld ( ) {
J C omboB oxc at ego ry Filt erFi eld = new J Com b o Bo x( Ca t eg o ry .c a te go r ie s( ) );
c a tegor yFi lt erF ie ld.s etSe lect e d Ite m ( Cat e g or y. AL L );
c a tegor yFi lt erF ie ld.a ddAc tion L i ste n e r(n e w A ct io n Li s te ne r () {
publi c v oi d a ct ionP erfo rmed ( A cti o n Eve n t e ) {
JCo mbo Bo x s ou rce = (J Comb o B ox) e . get S o ur ce () ;
myE dit or .fi lt erOn Cate gory ( ( Cat e g ory ) s ou rc e. g et S el ec t ed It e m( )) ;
}
});
g e tCont ent Pa ne( ). add( cate gory F i lte r F iel d ) ;
}

Green bar. That does it.

Amazon

Prev

don't be afraid of buying books

Next

HANDLE CHANGING A MOVIE'S CATEGORY


When the list is filtered, changing a movie to a different category should remove it from
the list.
This task lies completely in the logic layer of the GUI. One simple test is all that is required.
Test 49. Changing the category of a movie in a filtered list causes that movie to be
removed from the list. If that category is filtered on, that movie will be in the list.

Here it is:

Test 49: Changing the category of a movie in a filtered list causes that
movie to be removed from the list. If that category is filtered on, that
movie will be in the list
pub l ic vo id te stC ha ngin gCat egor y ( ) {
V e ctor new Fa nta sy Movi es = new V ect o r ();
n e wFant asy Mo vie s. add( fotr );
V e ctor com ed yMo vi es = new Vec t o r() ;
c o medyM ovi es .ad d( prin cess Brid e ) ;
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(fa ntas yMov i e s);
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Nam eF ield (pri nces s B rid e . get N a me () );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Rat in gFie ld(6 );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Cat eg oryF ield (Cat e g ory . F ANT A S Y) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.g et Nam eF ield ();
c o ntrol .se tR etu rn Valu e(pr ince s s Bri d e .ge t N am e( ), 1) ;
m o ckVie w.g et Rat in gFie ld() ;
c o ntrol .se tR etu rn Valu e(6, 1);
m o ckVie w.g et Cat eg oryF ield ();
c o ntrol .se tR etu rn Valu e(Ca tego r y .CO M E DY, 1 );
m o ckVie w.s et Mov ie s(ne wFan tasy M o vie s ) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(co medy Movi e s );
c o ntrol .se tV oid Ca llab le(1 );

c o ntrol .ac ti vat e( );


M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, m oc kV i ew );
e d itor. fil te rOn Ca tego ry(C ateg o r y.F A N TAS Y ) ;
e d itor. sel ec t(1 );
e d itor. upd at e() ;
e d itor. fil te rOn Ca tego ry(C ateg o r y.C O M EDY ) ;
c o ntrol .ve ri fy( );

The test fails because after changing the category on The Princess Bride to CO M ED Y, it is still in the list.
We need to extend upd at e() in M ovieL i stE d i tor to refresh the filtered list.
And how do we do that? The filtered list is just a Mov i e Li st with no retention of the filtering criteria.
The simplest thing that could work is to regenerate the filtered list on each update:
pri v ate v oid u pda te Movi e() {
s e lecte dMo vi e.s et Rati ng(v iew. g e tRa t i ngF i e ld ()
- 1);
s e lecte dMo vi e.s et Cate gory (vie w . get C a teg o r yF ie ld ( )) ;
f i lterO nCa te gor y( filt erCa tego r y );
}

To support this we need a field to hold the current Cat e go r y on which to filter:
pri v ate C ate go ry fi lter Cate gory = Ca t e gor y . AL L;

The test still fails since we're not maintaining the value in the new field yet. Let's take care of that:
pub l ic vo id fi lte rO nCat egor y(Ca t e gor y cat e g or y) {
f i lterC ate go ry= ca tego ry;
f i ltere dMo vi es = movi es.c ateg o r ySu b l ist ( f il te rC a te g or y) ;
u p dateM ovi eL ist () ;
}

Green bar. Time to clean up.


The use of fi lte rO nCa te gory () is confusing. It's called from the GUI when the filter category is
changed, and from u pda te Movi e() whenever the update button is pushed. There is no reason to set
the filter category if it hasn't changed, so we should extract the common behavior into a separate
method and call that from upd ateM ovie ( ) .We'll also call it from f il t er On C at eg o ry () after setting
fil t erCat ego ry . Here's the new code:
pri v ate v oid u pda te Movi e() {
s e lecte dMo vi e.s et Rati ng(v iew. g e tRa t i ngF i e ld ()
- 1);
s e lecte dMo vi e.s et Cate gory (vie w . get C a teg o r yF ie ld ( )) ;
u p dateA ndD is pla yF ilte redM ovie s ( );
}
pub l ic vo id fi lte rO nCat egor y(Ca t e gor y cat e g or y) {
f i lterC ate go ry= ca tego ry;
u p dateA ndD is pla yF ilte redM ovie s ( );
}
pri v ate v oid u pda te AndD ispl ayFi l t ere d M ovi e s () {
f i ltere dMo vi es = movi es.c ateg o r ySu b l ist ( f il te rC a te g or y) ;
u p dateM ovi eL ist () ;
}

The bar's still green, so we're done here. Before we're happy, though, we have to run all the tests. We
get a green bar on All Te sts but the Swing tests have some failures. We investigate and find that the
problem is the search for the rating combo box. Before we added the category filter combo box, the
rating combo box was the first one returned by default when Jemmy was asked for a
JComboBoxOperator. Now life is more complex. It's time to add a bit of infrastructure; specifically, we

will add a name to each component and search for them by name. To do this, we need to create a
Com p onent Cho os er subclass (part of Jemmy) to do the name checking. We will use this chooser for
finding components. Here it is:
pub l ic cl ass N ame Ba sedC hoos er i m p lem e n ts C o mp on en t Ch o os er {
p r ivate St ri ng na me;
p u blic Nam eB ase dC hoos er(S trin g com p o nen t N am e) {
name = c om pon en tNam e;
}
p u blic boo le an ch eckC ompo nent ( C omp o n ent a Co mp on e nt ) {
Strin g t he Nam e = aC ompo nent . g etN a m e() ;
retur n ( th eNa me != null ) && t heN a m e.e q u al s( na m e) ;
}

p u blic Str in g g et Desc ript ion( ) {


retur n " Ma tch es Com pone nts n a med \ "" + n am e+ " \ "" ;
}

Now we add names to our Swing components:


pri v ate v oid i nit Ca tego ryFi lter F i eld ( ) {
J C omboB oxc at ego ry Filt erFi eld = new J Com b o Bo x( Ca t eg o ry .c a te go r ie s( ) );
c a tegor yFi lt erF ie ld.s etNa me(" c a teg o r yFi l t er ") ;
//. . .
}
pri v ate v oid i nit Ca tego ryFi eld( ) {
c a tegor yFi el d = n ew J Comb oBox ( C ate g o ry. c a te go ri e s( ) );
c a tegor yFi el d.s et Name ("ca tego r y ");
//. . .
}
pri v ate v oid i nit Ra ting Comb o() {
r a tingF iel d = n ew JCo mboB ox(C u s tom M o vie L i st Re nd e re r .i co n s( )) ;
r a tingF iel d. set Na me(" rati ng") ;
//. . .
}
pri v ate v oid i nit Up date Butt on() {
J B utton up da teB ut ton = ne w JB u t ton ( " Upd a t e" );
u p dateB utt on .se tN ame( "upd ate" ) ;
//. . .
}
pri v ate v oid i nit Ad dBut ton( ) {
J B utton ad dB utt on = n ew J Butt o n ("A d d ");
a d dButt on. se tNa me ("ad d");
//. . .
}

pri v ate v oid i nit Na meFi eld( ) {


m o vieFi eld = ne w JTex tFie ld(1 6 ) ;
m o vieFi eld .s etN am e("n ame" );
/ / ...
}
pri v ate v oid i nit Li st() {
m o vieLi st = new J List (new Vec t o r() ) ;
m o vieLi st. se tNa me ("mo vieL ist" ) ;
/ / ...
}

Finally, we use our new N am eBas edCh oo se r in the Swing tests. For example:
pub l ic vo id te stU pd ateR atin g() {
J L istOp era to r m ov ieLi st =
n ew JL ist Op erat or(m ainW i n dow , new N am eB as e dC h oo se r (" mo v ie Li s t" )) ;
J C omboB oxO pe rat or rat ingC ombo =
n ew JC omb oB oxOp erat or(m a i nWi n d ow, n ew N am e Ba s ed Ch o os er ( "r at i ng ") ) ;
m o vieLi st. cl ick On Item (0, 1);
r a tingC omb o. set Se lect edIn dex( 0 ) ;

J B utton Ope ra tor u pdat eBut ton =


n ew JB utt on Oper ator (mai n W ind o w , n e w N am eB a se d Ch oo s er (" u pd at e ") );
u p dateB utt on .pu sh NoBl ock( );
m o vieLi st. cl ick On Item (1, 1);
m o vieLi st. cl ick On Item (0, 1);
a s sertE qua ls ("u pd atin g sh ould h ave c han g e d ra ti n g. " ,
0,
r at ingC ombo .get S e lec t e dIn d e x( )) ;

OK, all is green, all is good. Now we're confident that we are finished.

Amazon

Prev

Next

don't be afraid of buying books

INTERFACE CLEANUP
The GUI has been gradually getting more complex as we've added functionality to the application. It's
time to go back and rework the layout as we've outgrown the simple Fl o wL ay o ut that we've been
using. Figure 15.3 shows the current state of the GUI.

Figure 15.3. The current

F l oo w LL aa yy oo u tt -based

GUI.

We will start by splitting the GUI into three panes:


1. a list pane for the category filter combo and the movie list,
2. a detail pane for the information about the new/selected movie, and
3. a button pane for the buttons.
For each pane we will use a BoxL ayout the button pane will be laid out horizontally, while the other
panes will be vertical. The panes themselves will be laid out by a vertical Bo x La yo u t. Here's the
updated GUI creation code:
pub l ic vo id in it( ) {
s e tTitl e() ;
s e tLayo ut( );
g e tCont ent Pa ne( ). add( init List P a ne( ) ) ;
g e tCont ent Pa ne( ). add( init Deta i l Pan e ( ));
g e tCont ent Pa ne( ). add( init Butt o n Pan e ( ));
p a ck();

}
pri v ate J Pan el in it List Pane () {
J P anel lis tP ane = new JPa nel( ) ;
l i stPan e.s et Lay ou t(ne w Bo xLay o u t(l i s tPa n e , Bo xL a yo u t. Y A XI S) ) ;
l i stPan e.s et Bor de r(Bo rder Fact o r y.c r e ate E m pt yB or d er ( 10 , 1 0, 1 0 , 10 ) );
l i stPan e.a dd (in it Cate gory Filt e r Fie l d ()) ;
l i stPan e.a dd (Bo x. crea teRi gidA r e a(n e w Di m e ns io n( 0 , 5 )) );
l i stPan e.a dd (in it List ());
r e turn lis tP ane ;
}
pri v ate J Pan el in it Deta ilPa ne() {
J P anel det ai lPa ne = n ew J Pane l ( );
d e tailP ane .s etL ay out( new BoxL a y out ( d eta i l Pa ne , B ox L ay ou t .Y A X IS )) ;
d e tailP ane .s etB or der( Bord erFa c t ory . c rea t e Em pt yB o rd e r( 1, 10 , 1 0, 1 0 )) ;
d e tailP ane .a dd( in itNa meFi eld( ) ) ;
d e tailP ane .a dd( Bo x.cr eate Rigi d A rea ( n ew D i me ns io n (0 , 5 )) ) ;
d e tailP ane .a dd( in itRa ting Comb o ( ));
d e tailP ane .a dd( Bo x.cr eate Rigi d A rea ( n ew D i me ns io n (0 , 5 )) ) ;
d e tailP ane .a dd( in itCa tego ryFi e l d() ) ;
r e turn det ai lPa ne ;
}

pri v ate J Pan el in it Butt onPa ne() {


J P anel but to nPa ne = n ew J Pane l ( );
b u ttonP ane .s etL ay out( new BoxL a y out ( b utt o n Pa ne , B ox L ay ou t .X A X IS )) ;
b u ttonP ane .s etB or der( Bord erFa c t ory . c rea t e Em pt yB o rd e r( 1, 10 , 1 0, 1 0 )) ;
b u ttonP ane .a dd( in itAd dBut ton( ) ) ;
b u ttonP ane .a dd( Bo x.cr eate Rigi d A rea ( n ew D i me ns io n (5 , 0 )) ) ;
b u ttonP ane .a dd( in itUp date Butt o n ()) ;
r e turn but to nPa ne ;
}

The component initialization methods are tweaked to return the component rather than add it to the
main window. Here's an example:
pri v ate J Com bo Box i nitC ateg oryF i e ld( ) {
c a tegor yFi el d = n ew J Comb oBox ( C ate g o ry. c a te go ri e s( ) );
c a tegor yFi el d.s et Name ("ca tego r y ");
c a tegor yFi el d.s et Sele cted Item ( C ate g o ry. U N CA TE GO R IZ E D) ;
r e turn cat eg ory Fi eld;
}

That's all there is to it. The result is shown in Figure 15.4.

Figure 15.4. The current nested

BB o xx LL aa yy o uu t -based

GUI.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
We started of by considering possible solutions to the problem of presenting a filtered list to the user.
We explored one model that used the Decorator pattern to compose filters. In the end we decided that
that was overkill at the moment, and did something much simpler.
We did more work on the interface to support category filtering. In the process we moved to a more
sophisticated way of having Jemmy find components. We finished of by refactoring the structure of the
GUI, using a more elaborate layout approach by that of nested B ox La y ou t s.
There's a bit of a smell to the code. It feels like Mov i e Li st Ed i to r knows too much about the filtering
of the list. Maybe that knowledge should be extracted into a class that sits between the
Mov i eList Edi to r and the Mov ieLis t . That's starting to sound like the decorator-based solution we
thought about at the beginning of this chapter. I'll resist pursuing that now. If we get pushed farther in
that direction, we'll have to stop and do some big refactoring to move forward. Stay tuned.

Amazon

Prev

don't be afraid of buying books

Chapter 16. PERSISTENCE


Movie data is persistent across application sessions.
We need to persist data. After some discussion, it was decided that the way in which the data was
stored didn't matter much...the choice is ours. Since our data is structurally very simple, we'll use a
simple solution: a flat ASCII file.

Next

Amazon

Prev

don't be afraid of buying books

Next

WRITE TO A FLAT FILE


Write movie collection to a flat text file.
Java has a lovely IO abstraction facility that we will use to decouple our persistence facility from physical
files: streams. Not only does this make our design cleaner, but it also makes writing tests easier. Here
are the tests we'll want:
Test 50. Writing an empty list to a stream writes nothing.

Test 51. Writing a list of one movie should write that movie to the stream in the format
<name> | < category> | < rating>.

Test 52. Writing a list containing several movies writes each movie to the stream, one
per line, in the format of the previous test.

Our first task is to write a Mo v ieLi st to a S t r e a m . We begin, of course, with a test. Where should
the test go? My feeling is that since we are starting on a new area of functionality we should start with
a new fixture.

Test 50: Writing an empty list to a stream writes nothing


The first test will be for writing an empty M o v i e L i s t :
p ub l ic v oid test W riti n gEmp tyLi s t ( ) {
S t ri ng Wri t er d e stin a tion = n e w S t r i n g W r i t e r ( ) ;
M o vi eL ist empt y List =new Movi e L i s t ( ) ;
e m pt yL ist . writ e To(d e stin atio n ) ;
a s se rt Equ a ls(" W riti n g an emp t y l i s t s h o u l d p r o d u c e n o t h i n g . " ,
"",
d e stin a tion .toS t r i n g ( ) ) ;
}

To get this to compile, we need the Mov i e L i s t . w r i t e T o ( ) class:


p ub l ic v oid writ e To(W r iter des t i n a t i o n ) {

With this in place, the test compiles and, somewhat surprisingly, passes. Maybe it shouldn't be so
surprising. Writing an empty list results in empty output. Currently, w r i t e T o ( ) does nothing at all, so
the output is empty.
The next test will require us to do some real programming.

Test 51: Writing a list of one movie should write that movie to the
stream in the format <name> | <category> | <rating>
p ub l ic v oid test W riti n gOne Movi e ( ) t h r o w s E x c e p t i o n {
S t ri ng st a rWar s Outp u t = "Sta r W a r s | S c i e n c e F i c t i o n | 4 \ n " ;
S t ri ng Wri t er d e stin a tion = n e w S t r i n g W r i t e r ( ) ;
M o vi eL ist movi e List = ne w Mo v i e L i s t ( ) ;
M o vi e sta r Wars = ne w Mov ie(" S t a r W a r s " , C a t e g o r y . S C I F I , 4 ) ;
m o vi eL ist . add( s tarW a rs);

m o vi eL ist . writ e To(d e stin atio n ) ;


a s se rt Equ a ls(" W rong outp ut f r o m w r i t i n g a s i n g l e m o v i e l i s t . " ,
s t arWa r sOut put,
d e stin a tion .toS t r i n g ( ) ) ;

Notice how we've made a design decision while specifying the test. We've defined and specified the file
format that will be used to store our movie data.
Red bar right away! Now we implement M o v i e L i s t . w r i t e T o ( ) as simply as we can get away with.
p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {
i f ( si ze( ) > 0 ) {
M ov ie m ovie T oWri t e = getM o v i e ( 0 ) ;
d es tin a tion . writ e (mov ieTo W r i t e . g e t N a m e ( ) ) ;
d es tin a tion . writ e ('|' );
d es tin a tion . writ e (mov ieTo W r i t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
d es tin a tion . writ e ('|' );

t ry {
d est i nati o n.wr i te(I nteg e r . t o S t r i n g ( m o v i e T o W r i t e . g e t R a t i n g ( ) ) ) ;
} c atc h (Un r ated E xcep tion e x ) {
d est i nati o n.wr i te(" -1") ;
}
d es tin a tion . writ e ('\n ');
d es tin a tion . flus h ();

This is about as simple as we can get. Well, ok. . . we could have written a string literal. The test only
requires that the first (and only) movie in the collection is written, so that's all we write code to do.
Notice that we need the if ( size () > 0 ) check to keep t e s t W r i t i n g E m p t y L i s t ( ) passing.
Green, but not yet clean. First, look at the two tests. We can rename e m p t y L i s t and extract a fixture
to s e t Up () .

Exercises
12.

Extract the fixture code from the two tests into s e t U p ( ) .

Now let's turn our attention to Movi eLi s t . w r i t e T o ( ) . The way we have M o v i e L i s t dealing with
the internal details of Movi e stinks of Inappropriate Intimacy. That code should be moved to M o v i e
where the structural details can be encapsulated. First, we will extract the M o v i e writing code to a
separate method, then move it to M ovi e . First to extract it:
p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {
i f ( si ze( )
> 0) {
Mo vi e m o vieT o Writ e = g etMo v i e ( 0 ) ;
wr it eMo v ieTo ( movi e ToWr ite, d e s t i n a t i o n ) ;
de st ina t ion. f lush ( );
}
}
p ub l ic v oid writ e Movi e To(M ovie m o v i e T o W r i t e , W r i t e r d e s t i n a t i o n )
thro w s IO E xcep tion {
d e st in ati o n.wr i te(m o vieT oWri t e . g e t N a m e ( ) ) ;
d e st in ati o n.wr i te(' | ');
d e st in ati o n.wr i te(m o vieT oWri t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
d e st in ati o n.wr i te(' | ');

try {
de st ina t ion. w rite ( Inte ger. t o S t r i n g ( m o v i e T o W r i t e . g e t R a t i n g ( ) ) ) ;
} ca tc h ( U nrat e dExc e ptio n ex ) {
de st ina t ion. w rite ( "-1" );
}
d e st in ati o n.wr i te(' \ n');

Now move it to Movi e . In Movi eList :


p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {
i f ( si ze( ) > 0 ) {
Mo vi e m o vieT o Writ e = g etMo v i e ( 0 ) ;
mo vi eTo W rite . writ e To(d esti n a t i o n ) ;
de st ina t ion. f lush ( );
}
}

And in Mo vie :
p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {
d e st in ati o n.wr i te(g e tNam e()) ;
d e st in ati o n.wr i te(' | ');
d e st in ati o n.wr i te(g e tCat egor y ( ) . t o S t r i n g ( ) ) ;
d e st in ati o n.wr i te(' | ');

try {
de st ina t ion. w rite ( Inte ger. t o S t r i n g ( g e t R a t i n g ( ) ) ) ;
} ca tc h ( U nrat e dExc e ptio n ex ) {
de st ina t ion. w rite ( "-1" );
}
d e st in ati o n.wr i te(' \ n');

Test 52: Writing a list containing several movies writes each movie to
the stream, one per line, in the format of the previous test
Now we need a test for writing multiple M o v i e s:

p ub l ic v oid test W riti n gMul tipl e M o v i e s ( ) t h r o w s E x c e p t i o n {


S t ri ng ex p ecte d Resu l t = "Sta r W a r s | S c i e n c e F i c t i o n | 4 \ n " +
"The P r i n c e s s B r i d e | F a n t a s y | 5 \ n " ;
M o vi e sta r Wars = ne w Mov ie(" S t a r W a r s " , C a t e g o r y . S C I F I , 4 ) ;
M o vi e pri n cess B ride = ne w Mo v i e ( " T h e P r i n c e s s B r i d e " , C a t e g o r y . F A N T A S Y , 5 ) ;
m o vi eL ist . add( s tarW a rs);
m o vi eL ist . add( p rinc e ssBr ide) ;
m o vi eL ist . writ e To(d e stin atio n ) ;
a s se rt Equ a ls(" W rong outp ut f r o m w r i t i n g m u l t i p l e m o v i e s . " ,
e xpec t edRe sult ,
d esti n atio n.to S t r i n g ( ) ) ;
}

This test fails, which drives us to generalize M o v i e L i s t . w r i t e T o ( ) to write the entire collection of
M ov i es:
p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {
I t er at or m ovie I tera t or = mov i e s . i t e r a t o r ( ) ;
w h il e (mo v ieIt e rato r .has Next ( ) ) {
Mo vi e m o vieT o Writ e = ( Movi e ) m o v i e I t e r a t o r . n e x t ( ) ;
mo vi eTo W rite . writ e To(d esti n a t i o n ) ;
}
d e st in ati o n.fl u sh() ;
}

Green! Now there's a bit of cleanup to do on M o v i e . w r i t e T o ( ) : we should extract the writing of the
separator to its own method. This is because:
we can name it and make clear what it is doing, and
it will encapsulate the separator character.
Here's the new code:
p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {
d e st in ati o n.wr i te(g e tNam e()) ;
w r it eS epa r ator ( dest i nati on);
d e st in ati o n.wr i te(g e tCat egor y ( ) . t o S t r i n g ( ) ) ;
w r it eS epa r ator ( dest i nati on);
try {
de st ina t ion. w rite ( Inte ger. t o S t r i n g ( g e t R a t i n g ( ) ) ) ;
} ca tc h ( U nrat e dExc e ptio n ex ) {
de st ina t ion. w rite ( "-1" );
}
d e st in ati o n.wr i te(' \ n');

}
p ri v at e voi d wri t eSep a rato r(Wr i t e r d e s t i n a t i o n ) t h r o w s I O E x c e p t i o n {
d e st in ati o n.wr i te(' | ');
}

Theregreen and clean!

Amazon

Prev

don't be afraid of buying books

Next

SAVE-AS IN GUI
Provide, in the GUI, the capability to save the movie collection to a specific file.
For the GUI, we will start with the "Save As" functionality. We are ordering our tasks so that we do this task before
simply saving since that needs to be a place to which the list was previously saved (or, as we will see, from which it
was loaded). Here are the tests:
Test 53. Telling the logical layer to "saveas" causes it to ask the presentation layer to specify a file
into which it writes the movie list.

Test 54. Selecting "Save As" from the "File" menu prompts for a file using the standard file chooser.
The list is written into the selected file.

Test 55. If the file selection is cancelled, nothing is written to any file.

Test 53: Telling the logical layer to "saveas" causes it to ask the presentation
layer to specify a file into which it writes the movie list
p u b l i c v o i d testSaving ( ) t h r o w s E x c e p t io n {
S t r i n g e x pected = "S t a r W a r s | S c i e n c e F ic ti on |5 \n " +
"S t a r T r e k | S c i e n c e F ic ti on |3 \n " +
"S t a r g a t e | S c i e n c e Fi ct io n| -1 \n " +
"T h e S h i n i n g | H o r r o r| 2\ n" ;
F i l e o u t p utFile = Fi l e . c r e a t e T e m p F i l e( "t es tS av eA s" , " . d a t " ) ;
o u t p u t F i l e.deleteOnE x i t ( ) ;
m o c k V i e w . setMovies(m o v i e s ) ;
c o n t r o l . s etVoidCalla b l e ( 1 ) ;
m o c k V i e w . getFile("*. d a t " ) ;
c o n t r o l . s etReturnVal u e ( o u t p u t F i l e , 1 );
c o n t r o l . a ctivate();

M o v i e L i s t Editor edit o r = n e w M o v i e L i st Ed it or (m ov ie L i s t , m o c k V i e w ) ;
e d i t o r . s a veAs();
c o n t r o l . v erify();
a s s e r t F i l eEquals("Sa v e d f i l e " , e x p e c te d, o ut pu tF il e ) ;

p r i v a t e v o i d assertFil e E q u a l s ( S t r i n g m es sa ge , St ri ng e x p e c t e d , F i l e o u tp u t F i l e )
t h r o w s Exception {
a s s e r t E q u als(message + " i s w r o n g s i z e. ",
expect e d . l e n g t h ( ) ,
output F i l e . l e n g t h ( ) ) ;
F i l e R e a d e r reader = n e w F i l e R e a d e r ( o ut pu tF il e) ;
f o r ( i n t charIndex = 0 ; c h a r I n d e x
< e x p e c t e d . length(); c h a r I n d e x + + ) {
c h a r c h aracterRead = ( c h a r ) r e a d e r . re ad () ;
a s s e r t E quals(messa g e + " h a s w r o n g ch ar ac te r at i n d e x " + c h a r I n d e x + " . " ,
expec t e d . c h a r A t ( c h a r I nd ex ),
chara c t e r R e a d ) ;
}
}

Exercises
13.

Add the method stubs and declarations we need in order to get the test compiling.

We fail because the mock isn't getting the expected call to g e t F i l e ( ) . Let's add some code to
M o v i e L i s t E d itor.saveAs ( ) :
p u b l i c v o i d saveAs() {
F i l e o u t p utFile = vi e w . g e t F i l e ( " * . d a t" );
}

Now the failure is due to the saved file having a size of 0. We need to add more code to write out the list to the
specified file:
p u b l i c v o i d saveAs() t h r o w s I O E x c e p t i o n {
F i l e o u t p utFile = vi e w . g e t F i l e ( " * . d a t" );
F i l e W r i t e r writer = n e w F i l e W r i t e r ( o ut pu tF il e) ;
m o v i e s . w r iteTo(write r ) ;
w r i t e r . c l ose();
}

That does it. Before we move on, there is some refactoring we should do to our tests. I'd like to extract the fileoriented tests into a separate test case since it's inherently different than the rest of the tests. However, we need an
extension of the existing fixture. Do we duplicate the fixture? Do we extend the existing test case? No to both:
Duplication is to be avoided as much as possible, and if we extend a test case, we inherit all of its tests, which isn't
what we want. The solution is to extract the fixture into a common, abstract superclass and extend it as required in
the subclasses. So here's the superclass that houses the common fixture:
p u b l i c a b s t ract class C o m m o n T e s t M o v i e L is tE di to r ex te n d s T e s t C a s e {
p r o t e c t e d MockContro l c o n t r o l = n u l l ;
p r o t e c t e d MovieListE d i t o r V i e w m o c k V i ew = n ul l;
p r o t e c t e d Vector mov i e s = n u l l ;
p r o t e c t e d Movie star W a r s = n u l l ;
p r o t e c t e d Movie star T r e k = n u l l ;
p r o t e c t e d Movie star g a t e = n u l l ;
p r o t e c t e d Movie theS h i n i n g = n u l l ;
p r o t e c t e d MovieList m o v i e L i s t = n u l l ;
p r o t e c t e d void setUp ( ) t h r o w s E x c e p t io n {
s t a r W a r s = new Mov i e ( " S t a r W a r s " , C at eg or y. SC IF I, 5 ) ;
s t a r T r e k = new Mov i e ( " S t a r T r e k " , C at eg or y. SC IF I, 3 ) ;
s t a r g a t e = new Mov i e ( " S t a r g a t e " , C a te go ry .S CI FI , - 1 ) ;
t h e S h i n ing = new M o v i e ( " T h e S h i n i n g" ,C at eg or y. HO R R O R , 2 ) ;
m o v i e s =newVector( ) ;
m o v i e s . add(starWar s ) ;
m o v i e s . add(starTre k ) ;
m o v i e s . add(stargat e ) ;
m o v i e s . add(theShin i n g ) ;
m o v i e L i st = newMov i e L i s t ( ) ;
m o v i e L i st.add(star W a r s ) ;
m o v i e L i st.add(star T r e k ) ;
m o v i e L i st.add(star g a t e ) ;
m o v i e L i st.add(theS h i n i n g ) ;
c o n t r o l = EasyMock . c o n t r o l F o r ( M o v i eL is tE di to rV ie w . c l a s s ) ;
m o c k V i e w = (MovieL i s t E d i t o r V i e w ) c o nt ro l. ge tM oc k( ) ;

m o c k V i e w.setEditor ( n u l l ) ;
c o n t r o l .setDefault V o i d C a l l a b l e ( ) ;

Our previous test case is the same except that now it extends Co mm on Te s t M o v i e L i s t E d i t o r and doesn't have a
fixture (i.e., no instance variables or setUp() method). We create a new T e s tC a s e for the file-related tests and
move the testSaving ( ) and as s e rt F i l e E q u a l s ( ) to it:
p u b l i c c l a s s TestMovie L i s t E d i t o r F i l e O p er at io ns e xt en d s C o m m o n T e s t M o v i e Li s t E d i t o r {
p r i v a t e S tring expec t e d ;
p r i v a t e F ile outputF i l e ;
p r o t e c t e d void setUp ( ) t h r o w s E x c e p t io n {
s u p e r . s etUp();
e x p e c t e d = "Star W a r s | S c i e n c e F i c t io n| 5\ n" +
"Star T r e k | S c i e n c e F i c t io n| 3\ n" +
"Starga t e | S c i e n c e F i c t i on |- 1\ n" +
"The Sh i n i n g | H o r r o r | 2 \ n ";
o u t p u t F ile = File. c r e a t e T e m p F i l e ( " te st Sa ve As ", " . d a t " ) ;
o u t p u t F ile.deleteO n E x i t ( ) ;
}
p u b l i c v o id testSavi n g ( ) t h r o w s E x c e pt io n {
m o c k V i e w.setMovies ( m o v i e s ) ;
c o n t r o l .setVoidCal l a b l e ( 1 ) ;
m o c k V i e w.getFile(" * . d a t " ) ;
c o n t r o l .setReturnV a l u e ( o u t p u t F i l e , 1 );
c o n t r o l .activate() ;

M o v i e L i stEditor ed i t o r = n e w M o v i e Li st Ed it or (m ov i e L i s t , m o c k V i e w ) ;
e d i t o r . saveAs();
a s s e r t F ileEquals(" S a v e d f i l e " , e x p ec te d, o ut pu tF i l e ) ;
c o n t r o l .verify();

p r i v a t e v oid assertF i l e E q u a l s ( S t r i n g m es sa ge , St ri n g e x p e c t e d , F i l e ou t p u t F i l e )
t h r o ws Exceptio n {
a s s e r t E quals(messa g e + " i s w r o n g s iz e. ",
expe c t e d . l e n g t h ( ) ,
outp u t F i l e . l e n g t h ( ) ) ;
F i l e R e a der reader = n e w F i l e R e a d e r (o ut pu tF il e) ;
f o r ( i n t charIndex = 0 ; c h a r I n d e x
< e x p e c t e d . length(); c h a r I n d e x + + ) {
c h a r characterRe a d = ( c h a r ) r e a d e r. re ad () ;
a s s e r tEquals(mes s a g e + " h a s w r o n g ch ar ac te r at " + c h a r I n d e x + " . ",
ex p e c t e d . c h a r A t ( c h ar In de x) ,
ch a r a c t e r R e a d ) ;
}
}

p u b l i c s t atic void m a i n ( S t r i n g [ ] a r gs ) {
j u n i t . t extui.TestR u n n e r . r u n ( T e s t M o vi eL is tE di to rF i l e O p e r a t i o n s . c l a s s) ;
}

Test 54: Selecting "Save As" from the "File" menu prompts for a file using the
standard file chooser. The list is written into the selected file
Now to add support for the save as functionality to the Swing GUI. We're going to gear down for a bit and take some
smaller steps again. Here's the start of the test:
p u b l i c v o i d testSaving ( ) {
F i l e A s s e r t.assertSiz e ( " S a v e d l i s t h a s wr on g si ze ." ,
s a v e d T e x t . l e n gt h( ),
outputFile);
F i l e A s s e r t.assertEqu a l s ( " S a v e d f i l e " ,s av ed Te xt ,o ut p u t F i l e ) ;
}

Notice that we've started with what we want to be true, written as assertions. Also notice that we're using a class that
doesn't exist yet: F i l eAssert. Out next step is to create that class and the methods in it that we call. The code for
those asserts are derived from the assertFi le E q u a l s ( ) method that we wrote for a previous test. Before we go
farther with this test, we'll revisit that previous one to refactor and extract the file-related assertion method into a
helper class. We're doing this because we see utility in having those assertions generally available, not buried in a
TestCase.

First, extract the method into a separate class:


p u b l i c c l a s s FileAsser t e x t e n d s A s s e r t {
p u b l i c s t atic void a s s e r t F i l e E q u a l s ( St ri ng m es sa ge , S t r i n g e x p e c t e d , F i l e o u t p u t F i l e )
t h r o w s Exception {
a s s e r t E q u als(message + " i s w r o n g s i z e. ",
expect e d . l e n g t h ( ) ,
output F i l e . l e n g t h ( ) ) ;
F i l e R e a d e r reader = n e w F i l e R e a d e r ( o ut pu tF il e) ;
f o r ( i n t charIndex = 0 ; c h a r I n d e x
< e x p e c t e d . length(); c h a r I n d e x + + ) {
c h a r c h aracterRead = ( c h a r ) r e a d e r . re ad () ;
a s s e r t E quals(messa g e + " h a s w r o n g ch ar ac te r at " + c h a r I n d e x + " . " ,
expe c t e d . c h a r A t ( c h a r In de x) ,
char a c t e r R e a d ) ;
}
}
}

Next, we change the test that called the original version so that it calls this version:
p u b l i c v o i d testSaving ( ) t h r o w s E x c e p t io n {
m o c k V i e w . setMovies(m o v i e s ) ;
c o n t r o l . s etVoidCalla b l e ( 1 ) ;
m o c k V i e w . getFile("*. d a t " ) ;
c o n t r o l . s etReturnVal u e ( o u t p u t F i l e , 1 );
c o n t r o l . a ctivate();

M o v i e L i s t Editor edit o r = n e w M o v i e L i st Ed it or (m ov ie L i s t , m o c k V i e w ) ;
e d i t o r . s a veAs();
F i l e A s s e r t.assertFil e E q u a l s ( " S a v e A s -e d fi le " ,e xp e c t e d , o u t p u t F i l e ) ;
c o n t r o l . v erify();

Green. Now we can delete the original copy in T e s tM ov ie Li st Ed it o r F i l e O p e r a t i o n s . Still green. This method
was fine as a helper method, but now it smells like it's doing too much. Let's split up the size and content checks:
p u b l i c c l a s s FileAsser t e x t e n d s A s s e r t {

p u b l i c s t atic void a s s e r t F i l e E q u a l s ( St ri ng m es sa ge , S t r i n g e x p e c t e d , F i l e o u t p u t F i l e )
t h r o w s Exception {
a s s e r t S ize(message , e x p e c t e d . l e n g t h( ), o ut pu tF il e ) ;
a s s e r t E quals(messa g e , e x p e c t e d , o u tp ut Fi le );
}
p u b l i c s t atic void a s s e r t E q u a l s ( S t r i ng m es sa ge , St r i n g e x p e c t e d , F i l e o u t p u t F i l e )
t h r o w s FileNotFo u n d E x c e p t i o n , I O Ex ce pt io n {
F i l e R e a der reader = n e w F i l e R e a d e r (o ut pu tF il e) ;
f o r ( i n t charIndex = 0 ; c h a r I n d e x < ex pe ct ed .l en g t h ( ) ; c h a r I n d e x + + ) {
c h a r characterRe a d = ( c h a r ) r e a d e r. re ad () ;
a s s e r tEquals(mes s a g e + " h a s w r o n g ch ar ac te r at " + c h a r I n d e x + " . ",
ex p e c t e d . c h a r A t ( c h ar In de x) ,
ch a r a c t e r R e a d ) ;
}
}
p u b l i c s t atic void a s s e r t S i z e ( S t r i n g m es sa ge , in t e x p e c t e d S i z e , F i l e o u t p u t F i l e ) {
a s s e r t E quals(messa g e + " i s w r o n g s iz e. ", ex pe ct ed S i z e , o u t p u t F i l e . l en g t h ( ) ) ;
}

Compile, test, green! The message handling is messy; let's clean that up so that the two nested asserts act more like
the built-in ones:
p u b l i c c l a s s FileAsser t e x t e n d s A s s e r t {
p u b l i c s t atic void a s s e r t F i l e E q u a l s ( St ri ng m es sa ge , S t r i n g e x p e c t e d , F i l e o u t p u t F i l e )
t h r o w s Exception {
a s s e r t S ize(message + " i s w r o n g s i z e. ", ex pe ct ed .l e n g t h ( ) , o u t p u t F i l e) ;
a s s e r t E quals(messa g e + " h a s w r o n g co nt en ts ." ,e xp e c t e d , o u t p u t F i l e ) ;
}
p u b l i c s t atic void a s s e r t E q u a l s ( S t r i ng m es sa ge , St r i n g e x p e c t e d , F i l e o u t p u t F i l e )
t h r o w s FileNotFo u n d E x c e p t i o n , I O Ex ce pt io n {
F i l e R e a der reader = n e w F i l e R e a d e r (o ut pu tF il e) ;
f o r ( i n t charIndex = 0 ; c h a r I n d e x
< e x p e c t e d . length(); c h a r I n d e x + + ) {

c h a r characterRe a d = ( c h a r ) r e a d e r. re ad () ;
a s s e r tEquals(mes s a g e + " a t i n d e x : " +c ha rI nd ex ,
ex p e c t e d . c h a r A t ( c h ar In de x) ,
ch a r a c t e r R e a d ) ;

}
}
p u b l i c s t atic void a s s e r t S i z e ( S t r i n g m es sa ge , in t e x p e c t e d S i z e , F i l e o u t p u t F i l e ) {
a s s e r t E quals(messa g e , e x p e c t e d S i z e , ou tp ut Fi le .l e n g t h ( ) ) ;
}

Still green. The next step is to rewrite the original test method to use the two new asserts:
p u b l i c v o i d testSaving ( ) t h r o w s E x c e p t io n {
m o c k V i e w . setMovies(m o v i e s ) ;
c o n t r o l . s etVoidCalla b l e ( 1 ) ;
m o c k V i e w . getFile("*. d a t " ) ;
c o n t r o l . s etReturnVal u e ( o u t p u t F i l e , 1 );
c o n t r o l . a ctivate();

M o v i e L i s t Editor edit o r = n e w M o v i e L i st Ed it or (m ov ie L i s t , m o c k V i e w ) ;
e d i t o r . s a veAs();
F i l e A s s e r t.assertSiz e ( " S a v e A s - e d f i le h as w ro ng s i z e . " ,
e x p e c t e d . l e n g th () ,
outputFile);
F i l e A s s e r t.assertEqu a l s ( " S a v e A s - e d fi le h as w ro ng c o n t e n t s " ,
expected,
outputFile);
c o n t r o l . v erify();

It still gives us a green bar, so we can delete as se rt F i l e E q u a l s ( ).Everything is still green, so we're done. Now
we can go back to the Swing test we were writing, with the required assertions written and available. The next thing
we need to do is build the fixture that we'll need. This involves creating the list of movies that we will be saving, the
expected contents of the saved file, a file to save to, a running view, and a M o v i e L i s t E d i t or instance:
p r o t e c t e d v oid setUp() t h r o w s E x c e p t i o n {
S w i n g M o v i eListEditor V i e w . s t a r t ( ) ;
m o v i e L i s t = newMovie L i s t ( ) ;
m o v i e L i s t .add(new Mo v i e ( " S t a r W a r s " , Ca te go ry .S CI FI , 5 ) ) ;
m o v i e L i s t .add(new Mo v i e ( " S t a r T r e k " , Ca te go ry .S CI FI , 3 ) ) ;
m o v i e L i s t .add(new Mo v i e ( " S t a r g a t e " , C at eg or y. SC IF I,
-1));
s a v e d T e x t = "Star Wa r s | S c i e n c e F i c t i on |5 \n " +
"Star Tr e k | S c i e n c e F i c t i on |3 \n " +
"Stargat e | S c i e n c e F i c t i o n| -1 \n ";
o u t p u t F i l e = File.cr e a t e T e m p F i l e ( " t e st Sa vi ng ", " .d a t " ) ;
o u t p u t F i l e.deleteOnE x i t ( ) ;

m a i n W i n d o w = new JFr a m e O p e r a t o r ( " M o v ie L is t" );


e d i t o r = new MovieLi s t E d i t o r ( m o v i e L i st ,
( S w i n g M ov ie Li st Ed it or V i e w ) m a i n W i n d o w . g e tW i n d o w ( ) ) ;

Now we have a compiling test with fixture and assertions in place. As expected, it fails. Now we need to add to the
test to manipulate the GUI to set things up for the assertions. This is fairly simple in this case. We just need to select
Save As... from the File menu, and enter the name of the temporary file we created in the filename field in the
resulting F i l e C h o o s e r dialog:
p u b l i c v o i d testSaving ( ) t h r o w s E x c e p t io n {
J M e n u B a r O perator men u b a r = n e w J M e n u Ba rO pe ra to r( ma i n W i n d o w ) ;
J M e n u O p e r ator fileMe n u = n e w J M e n u O p er at or (m en ub ar , " F i l e " ) ;
f i l e M e n u . push();
J M e n u I t e m Operator sa v e A s I t e m = n e w J M en uI te mO pe ra to r ( m a i n W i n d o w ,
" S a v e A s . . . " );
s a v e A s I t e m.pushNoBlo c k ( ) ;
J F i l e C h o o serOperator f i l e C h o o s e r = n ew J Fi le Ch oo se r O p e r a t o r ( ) ;
f i l e C h o o s er.setSelec t e d F i l e ( o u t p u t F i le );
J B u t t o n O p erator save B u t t o n = n e w J B u tt on Op er at or (f i l e C h o o s e r , " S a v e " );

s a v e B u t t o n.push();

F i l e A s s e r t.assertSiz e ( " S a v e d l i s t h a s wr on g si ze ." ,


s a v e d T e x t . l e ng th () ,
F i l e A s s e r t .a ss er tE qu al s( " S a v e d f i l e " , s a v e dT e x t , o u t p u t F i l e ) ;

This fails immediately when trying to find the menu bar, and we don't have one yet. Let's add a menu bar to our GUI:
p u b l i c v o i d init() {
s e t T i t l e ( );
s e t L a y o u t ();
s e t J M e n u B ar(initMenu B a r ( ) ) ;
//...
}
p r i v a t e J M e nuBar initM e n u B a r ( ) {
J M e n u B a r menuBar = n e w J M e n u B a r ( ) ;
J M e n u f i l eMenu = new J M e n u ( " F i l e " ) ;
m e n u B a r . a dd(fileMenu ) ;
J M e n u I t e m saveAsItem = n e w J M e n u I t e m (" Sa ve A s. . . " ) ;
f i l e M e n u . add(saveAsI t e m ) ;
r e t u r n m e nuBar;
}

Now the test gets to the point of waiting for the JF il e C h o o s e r . What causes the file chooser to open? Remember
that the s aveAs() method in M ovieListEd it o r called ge tF i l e ( ) on its view. That's what should use a
J F i l e C h o o s e r to get a F i l e from the user. Furthermore, the Save As... menu item should cause the sa v e A s ( )
method to be called on the editor. Here's the menu item handler:
s a v e A s I t e m . addActionLi s t e n e r ( n e w A c t i o nL is te ne r( ) {
p u b l i c v o id actionPe r f o r m e d ( A c t i o n E v en t e) {
try {
m y E d i tor.saveAs( ) ;
} c a t c h (IOExcepti o n e x ) {
/ / T O DO: deal wi t h t h i s
}
}});

And the implementation of g e t F ile():


p u b l i c F i l e getFile(St r i n g p a t t e r n ) {
i n t r e t u r nVal = file C h o o s e r . s h o w S a v e Di al og (t hi s) ;
i f ( r e t u r nVal == JFi l e C h o o s e r . A P P R O V E OP TI ON ) {
r e t u r n fileChooser . g e t S e l e c t e d F i l e () ;
} else {
r e t u r n null;
}
}

Green. Notice how we are handling the cancelling of the file chooser dialog: we return n u l l to the editor. It's time to
revisit M o v i e L istEditor's s a v e As() method to account for this. But first, of course, a test that will fail because
we aren't handling with getF i l e ( ) returning n u l l when the file chooser is cancelled.

Test 55: If the file selection is cancelled, nothing is written to any file
p u b l i c v o i d testCancel l e d S a v i n g ( ) t h r o ws E xc ep ti on {
m o c k V i e w . setMovies(m o v i e s ) ;
c o n t r o l . s etVoidCalla b l e ( 1 ) ;
m o c k V i e w . getFile();
c o n t r o l . s etReturnVal u e ( n u l l , 1 ) ;
c o n t r o l . a ctivate();

M o v i e L i s t Editor edit o r = n e w M o v i e L i st Ed it or (m ov ie L i s t , m o c k V i e w ) ;
a s s e r t F a l se("Editor s h o u l d n o t h a v e sa ve d. ", ed it or . s a v e A s ( ) ) ;
c o n t r o l . v erify();

This fails with an error: a N u l l P o interExce pt io n is thrown when it tries to create a F i l e W r i t er around n u l l.
Something else we've done is expect a b o o l e a n value to be returned from s a v e A s ( ) indicating whether saving took
place successfully. It would be a good idea to take advantage of this in our earlier t e s t S a v i ng ( ) in
T e s t M o v i e L i stEditorFil e O p e r a t i o n s .

Exercises
14.

Make the required change to T es tM ov i e L i s t E d i t o rF i l e O p er a t i o n s . t e s t S a v i n g ( ) .

We'll fix it and get back to green by checking for nu ll being returned from g e t F i le ( ) (and returning the
appropriate value):
p u b l i c b o o l ean saveAs( ) t h r o w s I O E x c e p t io n {
F i l e o u t p utFile = vi e w . g e t F i l e ( ) ;
i f ( o u t p u tFile == nu l l ) {
r e t u r n false;
}

F i l e W r i t e r writer = n e w F i l e W r i t e r ( o ut pu tF il e) ;
m o v i e s . w r iteTo(write r ) ;
w r i t e r . c l ose();
r e t u r n t r ue;

Now, for completeness, we'll go back to the Swing-based tests and add a t e s t C a n c e l l ed S a v e ( ) for the GUI level:
p u b l i c v o i d testCancel l e d S a v i n g ( ) t h r o ws E xc ep ti on {
J M e n u B a r O perator men u b a r = n e w J M e n u Ba rO pe ra to r( ma i n W i n d o w ) ;
J M e n u O p e r ator fileMe n u = n e w J M e n u O p er at or (m en ub ar , " F i l e " ) ;
f i l e M e n u . push();
J M e n u I t e m Operator sa v e A s I t e m = n e w J Me nu It em Op er at o r ( m a i n W i n d o w ,
"Save As.. .");
s a v e A s I t e m.pushNoBlo c k ( ) ;
J F i l e C h o o serOperator f i l e C h o o s e r = n ew J Fi le Ch oo se r O p e r a t o r ( ) ;
f i l e C h o o s er.setSelec t e d F i l e ( o u t p u t F i le );
J B u t t o n O p erator canc e l B u t t o n = n e w J Bu tt on Op er at or ( f i l e C h o o s e r , " C a n ce l " ) ;
c a n c e l B u t ton.push();
F i l e A s s e r t.assertSiz e ( " S a v e d l i s t h a s wr on g si ze ." , 0 , o u t p u t F i l e ) ;
}

This also tests that we can select a file in the file chooser and cancel. It verifies that nothing happens to the file.

Amazon

Prev

don't be afraid of buying books

Next

SAVE IN GUI
Provide, in the GUI, the capability to save the movie collection to the same file it was
previously saved to.
We took it slowly for the last task since there were a few new ideas involved. Now we'll pick it up a bit
since this task requires only a refinement of what we've done so far on this story. Specifically, we need
to add the ability to resave to the same file without asking the user for a filename again.
Test 56. Telling the logical layer to "Save" causes it to save the current list to the same
file as the previous "Save As" operation.

Test 57. Selecting "Save" from the "File" menu causes the list to be written into the
previously selected file.

Since the required setup for testing this involves what we've already done for testing the Save As
functionality, we'll extend the te stSa vi ng ( ) tests.

Test 56: Telling the logical layer to "Save" causes it to save the current
list to the same file as the previous "Save As" operation
We'll start in T est Mo vie Li stEd itor Fi le O p era t i ons :
pub l ic vo id te stS av ing( ) th rows E xce p t ion {
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.g et Fil e( );
c o ntrol .se tR etu rn Valu e(ou tput F i le, 1 );
m o ckVie w.g et Nam eF ield ();
c o ntrol .se tR etu rn Valu e(fo tr.g e t Nam e ( ), 1 ) ;
m o ckVie w.g et Cat eg oryF ield ();
c o ntrol .se tR etu rn Valu e(fo tr.g e t Cat e g ory ( ) , 1) ;
m o ckVie w.g et Rat in gFie ld() ;
c o ntrol .se tR etu rn Valu e(fo tr.g e t Rat i n g() + 1 , 1) ;
m o ckVie w.s et Mov ie s(ex tend edMo v i es) ;
c o ntrol .se tV oid Ca llab le(1 );
c o ntrol .ac ti vat e( );
M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, mo ck V ie w) ;
a s sertT rue (" Edi to r sh ould hav e sav e d ",e d i to r. sa v eA s () );
F i leAss ert .a sse rt Size ("Sa ve A s - ed f i le h a s wr on g s i ze ." ,
expe cted . l eng t h (),
outp utFi l e );
F i leAss ert .a sse rt Equa ls(" Save A s-e d fil e ha s wr o ng co nt e nt s " ,
exp ecte d ,
out putF i l e);
e d itor. add () ;

a s sertT rue (" Edi to r sh ould hav e res a v ed" , ed it or . sa v e( )) ;


F i leAss ert .a sse rt Equa ls(" Save d fil e ", e x te nd ed E xp e ct ed , o ut p ut Fi l e) ;
c o ntrol .ve ri fy( );

Here we've added to the test to add a movie to the list, resave it, and verify the result. To support this,
we need to extend the fixture:
fot r = ne w M ov ie( "T he F ello wshi p of T h e R i n g" , Ca t eg o ry .F A NT AS Y , 5) ;
ext e ndedE xpe ct ed = expe cted + " T h e F e l low s h ip o f T he Ri ng | Fa nt a sy |5 \ n" ;
ext e ndedM ovi es = ne w Ve ctor (mov i e s);
ext e ndedM ovi es .ad d( fotr );

Add the required stub for Mov ieLi stEdi t or. s a ve( ) and give it a try:
pub l ic bo ole an sav e( ) {
r e turn fal se ;
}

Of course it fails because sav e() doesn't do anything yet. Most notably, it just returns f al se . We need
to write some code for s av e():
pub l ic bo ole an sav e( ) {
i f (out put Fi le == nul l) {
retur n f al se;
}

F i leWri ter w rit er = n ew F ileW r i ter ( o utp u t Fi le );


m o vies. wri te To( wr iter );
w r iter. clo se ();
r e turn tru e;

To make this compile we need to have an instance variable for o ut p ut F il e. Add it, compile, and run
the tests. It still fails because we don't initialize the o utp u tF i le instance variable. We'll do this in
sav e As(); we need to use the new instance variable rather than the local we had before:
pub l ic bo ole an sa ve As() thr owsI O E xce p t ion {
o u tputF ile = vi ew .get File ();
i f (out put Fi le == nul l) {
retur n f al se;
}

F i leWri ter w rit er = n ew F ileW r i ter ( o utp u t Fi le );


m o vies. wri te To( wr iter );
w r iter. clo se ();
r e turn tru e;

Now the test fails because the resulting file is wrong. Hmm. . . that seems unlikely. Everything looks
good. Run the test in the debugger (set a breakpoint on entry to Mo v ie Li s tE di t or .s a ve () ) and we
find that the Mo vie that got added is uncategorized and unrated. Hmm. . . a quick look at
Mov i eList Edi to r.a dd () reveals the problem. It was creating a movie with just a name. Ponder,
ponder, scratch, scratch. . . oh, yes. Our thought was to create a template movie to probe for a
duplicate. We missed going back and filling in the rest of the data. A quick look at the test for adding

(which was written when we had only a name) shows that the mock has no expectations regarding the
category and rating being fetched during an add. Not a nice feeling, discovering thatbut it happens.
Because we are practicing TDD, we have a better chance of finding that sort of thing early.
First things first, though. We'll fix add () to get our current test passing:
pub l ic vo id ad d() {
S t ring new Na me = view .get Name F i eld ( ) ;
M o vie n ewM ov ie = new Movi e(ne w N ame ,
vi e w .ge t C ate g o ry Fi el d () ,
vi e w .ge t R ati n g Fi el d( ) );
try {
movie s.a dd (ne wM ovie );
updat eMo vi eLi st ();
} catch (D up lic at eMov ieEx cept i o n e ) {
view. dup li cat eE xcep tion (new N a me) ;
}
}

Rerun the test. Green. It's not clean yet.

Exercises
15.

Fix the a dd () related tests in T est M o vie L is t Ed it o r.

Now to clean up Mo vie Li stEd itor . If we compare sav e () and sa v eA s( ) we see that other than
the initial fetch of o utp ut File in saveA s (), they are identical. That's an easy cleanup: we just have
to have s a veA s( ) set ou tput File and call s a ve( ) :
pub l ic bo ole an sa ve As() thr owsI O E xce p t ion {
o u tputF ile = vi ew .get File ();
r e turn sav e( );
}

Green and clean!

Test 57: Selecting "Save" from the "File" menu causes the list to be
written into the previously selected file
The next step is to do something very similar to the GUI t es t Sa vi n g( ) . But first we'll do some
damage control and fix up the add-related GUI tests which we see are also out of date. Actually, I'll
make that an exercise for you.

Exercises
16.

Fix up the add related tests in T e s tSw i n gM ov ie L is t Ed it o rV ie w .

Here's the new t es tSa vi ng() GUI test:


pub l ic vo id te stS av ing( ) th rows E xce p t ion {
J M enuBa rOp er ato r menu bar = ne w JMe n u Bar O p er at or ( ma i nW in d ow );
J M enuOp era to r f il eMen u = new J M enu O p era t o r( me nu b ar , " Fi l e" );
f i leMen u.p us h() ;
J M enuIt emO pe rat or sav eAsI tem =
new J Men uI temO pera tor( m a inW i n dow ,
new N ame B a sed C h oo se r( " sa v ea s" ) );
s a veAsI tem .p ush No Bloc k();
J F ileCh oos er Ope ra tor file Choo s e r = n ew J F il eC ho o se r Op er a to r( ) ;
f i leCho ose r. set Se lect edFi le(o u t put F i le) ;
J B utton Ope ra tor s aveB utto n = n e w J B u tto n O pe ra to r (f i le Ch o os er , " Sa v e" );
s a veBut ton .p ush () ;
F i leAss ert .a sse rt Size ("Sa ve-A s e d l i s t h a s w ro ng si z e. ",
sa vedT e x t.l e n gth ( ) ,
ou tput F i le) ;
F i leAss ert .a sse rt Equa ls(" Save - A sed f ile " , s av ed T ex t , ou t pu tF i le );
J T extFi eld Op era to r ne wMov ieFi e l d =
n ew JT ex tFie ldOp erat o r (ma i n Win d o w,
new N ame B a se dC ho o se r (" na m e" )) ;
n e wMovi eFi el d.e nt erTe xt(t heSh i n ing . g etN a m e( )) ;
J C omboB oxO pe rat or rat ingC ombo =
n ew JC omb oB oxOp erat or(m a i nWi n d ow,
n ew N a m eBa s e dCh o o se r( "r a ti n g" )) ;
r a tingC omb o. set Se lect edIn dex( t h eSh i n ing . g et Ra ti n g( ) + 1 ) ;
J C omboB oxO pe rat or cat egor yCom b o =
n ew JC omb oB oxOp erat or(m a i nWi n d ow,
n ew N a m eBa s e dCh o o se r( "c a te g or y" ) );
c a tegor yCo mb o.s et Sele cted Inde x ( 2);
J B utton Ope ra tor a ddBu tton =
n ew JB utt on Oper ator (mai n W ind o w ,
ne w Na m e Bas e d Cho o s er (" ad d ") ) ;
a d dButt on. do Cli ck ();
f i leMen u.p us h() ;
J M enuIt emO pe rat or sav eIte m =
new J Men uI temO pera tor( m a inW i n dow ,
ne w Nam e B ase d C ho os er ( "s a ve ") ) ;
s a veIte m.p us h() ;
F i leAss ert .a sse rt Size ("Sa ved l i st h a s w r o ng s iz e ." ,
ex tend e d Sav e d Tex t . le ng th ( ),
ou tput F i le) ;

F i leAss ert .a sse rt Equa ls(" Save d fil e " ,


exte n d edS a v edT e x t,
outp u t Fil e ) ;

We need to also extend the fixture somewhat:


the S hinin g = n ew Mo vie( "The Shi n i ng" , Cat e g or y. HO R RO R , 2) ;
ext e ndedS ave dT ext = sav edTe xt + " The S hin i n g| Ho rr o r| 2 \n ";

This compiles but fails because there is no Save item. Easy enough to add:
pri a te JM enu Ba r i ni tMen uBar () {
J M enuBa r m en uBa r = ne w JM enuB a r ();
J M enu f ile Me nu = new JMen u("F i l e") ;
m e nuBar .ad d( fil eM enu) ;
J M enuIt em sa veA sI tem = ne w JM e n uIt e m ("S a v e As .. ." ) ;
s a veAsI tem .s etN am e("s avea s");
s a veAsI tem .a ddA ct ionL iste ner( n e w A c t ion L i st en er ( ) {
publi c v oi d a ct ionP erfo rmed ( A cti o n Eve n t e ) {
try {
myE di tor .s aveA s();
} c atc h (IO Ex cept ion ex) {
/ / T OD O: de al w ith this
}
}});
f i leMen u.a dd (sa ve AsIt em);

J M enuIt em sa veI te m = new JMen u I tem ( " Sav e " );


s a veIte m.s et Nam e( "sav e");
f i leMen u.a dd (sa ve Item );
r e turn men uB ar;

Now the test fails because the file is not being updated. We next need to call s a ve () in the associated
Mov i eList Edi to r in response to the Save item being selected:
pri v ate J Men uB ar in itMe nuBa r() {
J M enuBa r m en uBa r = ne w JM enuB a r ();
J M enu f ile Me nu = new JMen u("F i l e") ;
m e nuBar .ad d( fil eM enu) ;
J M enuIt em sa veA sI tem = ne w JM e n uIt e m ("S a v e As .. ." ) ;
s a veAsI tem .s etN am e("s avea s");
s a veAsI tem .a ddA ct ionL iste ner( n e w A c t ion L i st en er ( ) {
publi c v oi d a ct ionP erfo rmed ( A cti o n Eve n t e ) {
try {
m yEd it or. sa veAs ();
} c atc h (IO Ex cept ion ex) {
/ / T OD O: de al w ith this
}
}});
f i leMen u.a dd (sa ve AsIt em);

J M enuIt em sa veI te m = new JMen u I tem ( " Sav e " );


s a veIte m.s et Nam e( "sav e");
s a veIte m.a dd Act io nLis tene r(ne w Act i o nLi s t en er () {
publi c v oi d a ct ionP erfo rmed ( A cti o n Eve n t e ) {
try {
m yEd it or. sa ve() ;
} c atc h (IO Ex cept ion ex) {
/ / T OD O: de al w ith this
}
}});
f i leMen u.a dd (sa ve Item );
r e turn men uB ar;

The test now runs green. However, ini t Men u B ar has grown too large and is starting to smell. A
couple of Extract Method applications (one to each JMe n u It em setup) will take care of it:
pri v ate J Men uB ar in itMe nuBa r() {
J M enuBa r m en uBa r = ne w JM enuB a r ();

J M enu f ile Me nu = new JMen u("F i l e") ;


m e nuBar .ad d( fil eM enu) ;
f i leMen u.a dd (in it Save AsIt em() ) ;
f i leMen u.a dd (in it Save Item ());
r e turn men uB ar;

pri v ate J Men uI tem i nitS aveA sIte m ( ) {


J M enuIt em sa veA sI tem = ne w JM e n uIt e m ("S a v e As . . . " );
s a veAsI tem .s etN am e("s avea s");
s a veAsI tem .a ddA ct ionL iste ner( n e w A c t ion L i st en er ( ) {
publi c v oi d a ct ionP erfo rmed ( A cti o n Eve n t e ) {
try {
m yEd it or. sa veAs ();
} c atc h (IO Ex cept ion ex) {
/ / T OD O: de al w ith this
}
}});
r e turn sav eA sIt em ;
}
pri v ate J Men uI tem i nitS aveI tem( ) {
J M enuIt em sa veI te m = new JMen u I tem ( " Sav e " );
s a veIte m.s et Nam e( "sav e");
s a veIte m.a dd Act io nLis tene r(ne w Act i o nLi s t en er () {
publi c v oi d a ct ionP erfo rmed ( A cti o n Eve n t e ) {
try {
m yEd it or. sa ve() ;
} c atc h (IO Ex cept ion ex) {
// TO DO : d ea l wi th t his
}
}});
r e turn sav eI tem ;
}

OK. Green and clean! There's still some duplication in the menu item creation and setup code, but that's
largely unavoidable in GUI construction code where you are creating multiples of the same type of
component.

Amazon

Prev

don't be afraid of buying books

Next

READ FROM A FLAT FILE


Read a movie collection from a flat text file.
This task requires us to add support for reading a previously written Mo v ie L is t back into the system.
Test 58. Reading from an empty stream results in an empty list of movies.

Test 59. Reading from a stream containing the data for a single movie results in a list
containing the single movie.

Test 60. Reading from a stream containing the data for several movies results in a list
containing those movies.

We'll start with reading a Mov ieLi st from a Rea d e r. This is a different style of test than the writing
tests and will need a different fixture, so we need a new T e st C as e.

Test 58: Reading from an empty stream results in an empty list of movies
We start simplywith a test for reading an empty list:
p ub lic cla ss Te st Mov ie List Read in ge x t end s Tes t Ca s e {
p riv a te St rin g emp ty Stri ng;
p rot e cte d voi d set Up () t hrow s Ex c e pti o n {
em p tyS tr ing = "" ;
}
p ubl i c v oi d t es tRe ad ingE mpty Li st ( ) {
Re a der r ead er = ne w St ring Re ad e r (em p t ySt r in g );
Mo v ieL is t m ov ies = Mov ieLi st .r e a dFr o m (re a de r );
as s ert Eq ual s( "Em pt y li st s ho ul d n 't h a ve e nt r ie s. " ,0 , mo vi e s. si z e( )) ;
}

p ubl i c s ta tic v oid m ain( Stri ng [ ] arg s ) {


ju n it. te xtu i. Tes tR unne r.ru n( Te s t Mov i e Lis t Re a di ng . cl a ss );
}

To make this compile, we need to stub M o vie L i st. r e ad( ) in the simplest way possible:
p ub lic sta ti c M ov ieL is t re adFr om (R e a der r ead e r) {
r etu r n n ul l;
}

The test now redbars with a Null Point e rEx c e pti o n because the stub returns n ul l .
While we make a practice of writing initial stubs to return the simplest
thing (i.e., 0, fals e, n ull , etc.),it's generally frowned on to return
n ul l in real code if that would cause you to have code similar to:
S om eCl as s an Obje ct = s ome M e tho d () ;
i f (an Ob ject != nu ll ) {
a nOb je ct.d oSom et hi n g ();
}

In this case, you should return what's called a null object. Examples of
this include empty collections.

So, we rewrite the stub to return an empty M o v ieL i s t:


p ub lic sta ti c M ov ieL is t re adFr om (R e a der r ead e r) {
r etu r n n ew Mo vi eLi st ();
}

Green.

Test 59: Reading from a stream containing the data for a single movie
results in a list containing the single movie
The next test will be to read a single movie. We start simply by verifying that exactly one Mo v ie was
read:
p ub lic voi d tes tR ead in gOne Movi e( ) t h row s Exc e pt i on {
R ead e r r ea der = ne w Stri ngRe ad er ( o neM o v ieS t ri n g) ;
M ovi e Lis t mov ie s = M ovie List .r ea d F rom ( r ead e r) ;
a sse r tEq ua ls( "R ead in g a one mo vi e lis t sho u ld re su l t i n a o ne i t em l i st ." ,
1,
m ovi es .siz e()) ;
}

We need to add the string being read to the fixture:


o ne Mov i eSt ri ng = "St ar War s|Sc ie nc e Fic t i on| 4 \n " ;

Now we need to write some code to read exactly one M o vi e. We will take a similar approach to the one
we ended up with in the writing code: We will delegate the reading of a Mo v ie to the Mo v ie class:
p ub lic sta ti c M ov ieL is t re adFr om (R e a der r ead e r) th ro w s D up li c at eM o vi eE x ce pt i on {

B uff e red Re ade r buf fe redR eade r = n e w B u f fer e dR e ad er ( re a de r) ;


M ovi e Lis tn ewL is t = ne wMov ieLi st () ;
M ovi e ne wM ovi e = M ov ie.r eadF ro m( b u ffe r e dRe a de r );

i f ( n ewM ov ie != nu ll ) {
ne w Lis t. add (n ewM ov ie);
}
r etu r n n ew Lis t;

Notice the interface design we've done while writing this method. We've decided that if a Mo vi e can't be
read, nul l is returned instead of a Mov ie instance. We need this (and the conditional addition to the list)
in order to keep the previous test from failing.
The standard stub for Mo vi e.re adFr om() satisfies this:
p ub lic sta ti c M ov ie re adFr om(R ea de r rea d e r) {
r etu r n n ul l;
}

Next, we need to implement code to actually read a M o v ie :


p ub lic sta ti c M ov ie re adFr om(B uf fe r e dRe a d er r ea d er ) t hr o ws I O Ex ce p ti on {
S tri n g o ne Lin e= rea de r.re adLi ne () ;
i f ( o neL in e = = nul l) {
re t urn n ull ;
}

S tri n gTo ke niz er to ke nize dLin e = n e w S t r ing T ok e ni ze r (o n eL in e , DE L IM IT E R) ;


t ry {
St r ing n ame = to ke nize dLin e. ne x t Tok e n ();
Ca t ego ry ca te gor y = Ca tego ry .g e t Cat e g ory N am e d( to k en i ze dL i ne .n e xt To k en () ) ;
in t ra ti ng= I nte ge r.pa rseI nt (t o k eni z e dLi n e. n ex tT o ke n () );
re t urn n ew Mo vie (n ame, cat eg or y , ra t i ng) ;
} ca t ch (N oSu ch Ele me ntEx cept io n e x ) {
th r ow ne w I OE xce pt ion( "Bad ly f o r mat t e d m o vi e c ol l ec t io n" ) ;
}

We need to convert from the name of a category (that of the stored Mo v ie )to an instance of the
C at ego r y class. We can't just create an instance with the name because C at e go ry is a type-safe enum
and as such has a constraint set of instances. If you recall, this is enforced by having a private
constructor. So we need a static method in C ate g o ry to return the instance corresponding to a name.
This is simply a matter of using a map to store a mapping between the names and the instances:
p ub lic cla ss Ca te gor y {
p riv a te st ati c Map c ateg orie sB yN a m e = n ew H as h Ma p( ) ;
/ /. . .

p riv a te Ca teg or y(S tr ing cate go ry N a me) {


na m e = c ate go ryN am e;
ca t ego ri esB yN ame .p ut(n ame, t hi s ) ;
al l Cat eg ori es .ad d( this );
}
/ /. . .

And adding the lookup method:


p ub lic sta ti c C at ego ry get Cate go ry N a med ( S tri n g c at eg o ry N am e) {
C ate g ory t heC at ego ry = ( Cate go ry ) c ate g o rie s By N am e. g et ( ca te g or yN a me );
r etu r n ( th eCa te gor y == n ull) ? C a t ego r y .UN C AT E GO RI Z ED : th eC a te go r y;
}

Now we can compile everything (most notably Mo v i e.r e a dF ro m( ) )and run the tests: green bar!
Next, we have to extend the test to make sure that the Mo vi e that was read was what was expected:
p ub lic voi d tes tR ead in gOne Movi e( ) t h row s Exc e pt i on {
R ead e r r ea der = ne w Stri ngRe ad er ( o neM o v ieS t ri n g) ;
M ovi e Lis t mov ie s = M ovie List .r ea d F rom ( r ead e r) ;
a sse r tEq ua ls( "R ead in g a one mo vi e lis t sho u ld re su l t i n a o ne i t em l i st ." ,
1,
m ovi es .siz e()) ;
M ovi e th eM ovi eR ead = mov ies. ge tM o v ie( 0 ) ;
a sse r tEq ua ls( "R ead m ovie has w ro n g na m e .",
" Sta r Wars ",
t heM ov ieRe ad.g et Na m e ()) ;
a sse r tEq ua ls( "R ead m ovie has w ro n g ca t e gor y ." ,
C ate go ry.S CIFI ,
t heM ov ieRe ad.g et Ca t e gor y ( ));

a sse r tEq ua ls( "R ead m ovie has w ro n g ra t i ng. " ,


4,
t heM ov ieRe ad.g et Ra t i ng( ) ) ;

Green bar. We just had to be sure. Also, this will guard us against unintentional changes to the behavior
of the reading code in the future.

Test 60: Reading from a stream containing the data for several movies
results in a list containing those movies
We now need a test to drive the reading of a collection of multiple M ov i es:
p ub lic voi d tes tR ead in gMul tipl eM ov i e () t h row s E x ce pt i on {
R ead e r r ea der = ne w Stri ngRe ad er ( m ult i M ovi e St r in g) ;
M ovi e Lis t mov ie s = M ovie List .r ea d F rom ( r ead e r) ;
a sse r tEq ua ls( "R ead in g a mult ip le m ovi e lis t s h ou ld re s ul t i n 3 i te ms . ",
3,
m ovi es .siz e()) ;
}

As before, we need to extend the fixture:


m ul tiM o vie St rin g = o ne Movi eStr in g +
"S ta r Tr ek|S ci en c e Fi c t ion | 3\ n " +
"S ta rgat e|Sc ie nc e Fic t i on| - 1\ n ";

Compile, run, red bar. Of course it fails; it only reads the first movie. Now we need to add just enough
code to make it read as many movies as are present. We only need to work on the re a dF ro m () method
of M ovi e Lis t:
p ub lic sta ti c M ov ieL is t re adFr om (R e a der r ead e r)
th ro ws Du pli ca teMo vieE xc ep t i on, I OEx c ep t io n {
B uff e red Re ade r buf fe redR eade r = n e w B u f fer e dR e ad er ( re a de r) ;
M ovi e Lis tn ewL is t = ne wMov ieLi st () ;

f or ( Mov ie ne wM ovi e = Mo vie. re ad F r om( b u ffe r ed R ea de r );


ne wM ovi e != nu ll;
ne wM ovi e = M ov ie.r eadF ro m( b u ffe r e dRe a de r )) {
n e wLi st .ad d( new Mo vie) ;
}
r etu r n n ew Lis t;

That does it. Green.

Amazon

Prev

don't be afraid of buying books

Next

LOAD IN GUI
Provide, in the GUI, the capability to load the movie collection from a file.
Test 61. With the list empty, telling the logical layer to load from a file that contains
data for a set of movies results in the list containing those movies.

Test 62. Choosing "Load" from the "File" menu and selecting a file containing a specific
set of movie data causes the corresponding movies to be placed in the list.

Test 63. With a set of movies loaded, cancelling the load of a different set leaves the
originally loaded set unchanged.

Now we move up a level and add loading capabilities to the M o v i e L i s t E d i t o r .

Test 61: With the list empty, telling the logical layer to load from a file
that contains data for a set of movies results in the list containing those
movies
Our first test starts with an empty list, loads a file, and verifies that the expected list was sent to the
view:
p ub l ic v oid test L oadi n g() {
m o ck Vi ew. s etMo v ies( e mpty Movi e s ) ;
c o nt ro l.s e tVoi d Call a ble( 1);
m o ck Vi ew. g etFi l eToL o ad() ;
c o nt ro l.s e tRet u rnVa l ue(i nput F i l e , 1 ) ;
m o ck Vi ew. s etMo v ies( m ovie s);
c o nt ro l.s e tVoi d Call a ble( 1);
c o nt ro l.a c tiva t e();
M o vi eL ist E dito r edi t or = new M o v i e L i s t E d i t o r ( n e w M o v i e L i s t ( ) , m o c k V i e w ) ;
a s se rt Tru e ("Ed i tor s houl d ha v e l o a d e d . " , e d i t o r . l o a d ( ) ) ;
}

c o nt ro l.v e rify ( );

After adding the required fixture and stubs, we get a red bar due to the f a l s e returned by the stub
l oa d () .Let's fake it and have load () return t r u e . The bar's still red, but we have a more helpful
failure:
j un i t. fr ame w ork. A sser t ionF aile d E r r o r :
E a sy Mo ck f or i n terf a ce c om.s a o r s a . n o w p l a y i n g . M o v i e L i s t E d i t o r V i e w :
E x pe ct ati o n fa i lure on v erif y :
me th od c all g etFi l eToL oad( ) :

ca ll s e x pect e d: 1 ,
re ce ive d : 0
m e th od ca l l se t Movi e s([S tar W a r s , S t a r T r e k , S t a r g a t e , T h e S h i n i n g ] ) :
ca ll s e x pect e d: 1 ,
re ce ive d : 0

Now we can go to work on implementing l o a d ( ) :


p ub l ic b ool e an l o ad()
th ro ws F ileN o tFou n dExc epti o n , I O E x c e p t i o n , D u p l i c a t e M o v i e E x c e p t i o n {
F i le i npu t File = vi e w.ge tFil e T o L o a d ( ) ;
i f ( in put F ile = = nu l l) {
re tu rn f alse ;
}

F i le Re ade r rea d er = new File R e a d e r ( i n p u t F i l e ) ;


m o vi es = M ovie L ist. r eadF rom( r e a d e r ) ;
f i lt er OnC a tego r y(Ca t egor y.AL L ) ;
r e tu rn tr u e;

OK, now the test fails with:


j un i t. fr ame w ork. A sser t ionF aile d E r r o r :
E a sy Mo ck f or i n terf a ce c om.s a o r s a . n o w p l a y i n g . M o v i e L i s t E d i t o r V i e w :
m e th od ca l l se t Movi e s([] ):
ca ll s e x pect e d: 1 ,
re ce ive d : 2

We haven't provided any data to be loaded, so l o a d ( ) results in an empty list. This is good, but not
what we're interested in testing here.
Hmm. This is starting to look like a completely different fixture. Maybe we should split
T es t Mo vi eLi s tEdi t orFi l eOpe rati o n s into two test cases, one for saving, and one for loading.

Exercises
17.

Do the split: T estM ovi e L i s t E d i t o r F i l e O p e r a t i o n s into


Te s tMo v ieLis tEdi to r S a v i n g and T e s t M o v i e L i s t E d i t o r L o a d i n g .

Now let's provide some data to load by setting up the input file in s e t U p ( ) :
m ov i eL is t.w r iteT o (new File Writ e r ( i n p u t F i l e ) ) ;

That does it. Green bar.


To finish off we take loading to the GUI.

Test 62: Choosing "Load" from the "File" menu and selecting a file
containing a specific set of movie data causes the corresponding
movies to be placed in the list

Exercises
18.

In looking at adding another test to


Te s tSw i ngMov ieLi st E d i t o r F i l e O p e r a t i o n s , we notice that we have some
code debt. The Rule of Three has kicked in. This is the third test, and as we think
about it we see that there are a few lines of code duplicated in each test. Time to
refactor. What do we need to do?

So, here's the first test:


p ub l ic v oid test L oadi n g() {
J M en uI tem O pera t or l o adIt em =
ne w JMe n uIte m Oper ator ( m a i n W i n d o w , " O p e n . . . " ) ;
l o ad It em. p ushN o Bloc k ();
J F il eC hoo s erOp e rato r fil eCho o s e r = n e w J F i l e C h o o s e r O p e r a t o r ( ) ;
f i le Ch oos e r.se t Sele c tedF ile( i n p u t F i l e ) ;
J B ut to nOp e rato r loa d Butt on = n e w J B u t t o n O p e r a t o r ( f i l e C h o o s e r , " O p e n " ) ;
l o ad Bu tto n .pus h ();

J L is tO per a tor m ovie L ist =


n ew J List O pera t or(m ainW i n d o w , n e w N a m e B a s e d C h o o s e r ( " m o v i e L i s t " ) ) ;
L i st Mo del list M odel = mo vieL i s t . g e t M o d e l ( ) ;
a s se rt Equ a ls(" M ovie list is t h e w r o n g s i z e " , 4 , l i s t M o d e l . g e t S i z e ( ) ) ;

The first thing we need to do is add a stub for g e t F i l e T o L o a d ( ) :


p ub l ic F ile getF i leTo L oad( ) {
r e tu rn nu l l;
}

If we're calling this method g e t F i l e T o L o a d ( ) , it makes sense to


rename getF ile( ) to g e t F i l e T o S a v e ( ) .It's just are name, so we
won't spend any more time or text on it. Just remember that it
appears in an interface and several mocks as well.

Next, we need to add an Open option to the File menu (in S w i n g M o v i e L i s t E d i t o r V i e w ). Since the
format of this will be almost identical to the two existing menu items, we'll take a big step and just do
it:
p ri v at e JMe n uBar init M enuB ar() {
J M en uB ar m enuB a r = n ew J Menu B a r ( ) ;
J M en u fil e Menu = ne w JMe nu(" F i l e " ) ;
m e nu Ba r.a d d(fi l eMen u );
f i le Me nu. a dd(i n itOp e nIte m()) ;
f i le Me nu. a dd(i n itSa v eAsI tem( ) ) ;
f i le Me nu. a dd(i n itSa v eIte m()) ;
r e tu rn me n uBar ;
}
p ri v at e JMe n uIte m ini t Open Item ( ) {
J M en uI tem open I tem = new JMe n u I t e m ( " O p e n . . . " ) ;

o p en It em. s etNa m e("o p en") ;


o p en It em. a ddAc t ionL i sten er(n e w A c t i o n L i s t e n e r ( ) {
pu bl ic v oid a ctio n Perf orme d ( A c t i o n E v e n t e ) {
tr y {
my E dito r .loa d ();
} cat c h (E x cept i on e x) {
// T ODO: deal with thi s
}
}} );
r e tu rn op e nIte m ;

Now the test is looking for a file chooser. Time to fill in g e t F i l e T o L o a d ( ) :


p ub l ic F ile getF i leTo L oad( ) {
i n t re tur n Val = fil e Choo ser. s h o w O p e n D i a l o g ( t h i s ) ;
i f ( re tur n Val = = JF i leCh oose r . A P P R O V E O P T I O N ) {
re tu rn f ileC h oose r .get Sele c t e d F i l e ( ) ;
} el se {
re tu rn n ull;
}
}

Green!

Test 63: With a set of movies loaded, cancelling the load of a different
set leaves the originally loaded set unchanged
p ub l ic v oid test C ance l ledL oadi n g ( ) {
J M en uI tem O pera t or o p enIt em =
n e w JM e nuIt e mOpe rato r ( m a i n W i n d o w , n e w N a m e B a s e d C h o o s e r ( " o p e n " ) ) ;
o p en It em. p ushN o Bloc k ();
J F il eC hoo s erOp e rato r fil eCho o s e r = n e w J F i l e C h o o s e r O p e r a t o r ( ) ;
f i le Ch oos e r.se t Sele c tedF ile( i n p u t F i l e ) ;
J B ut to nOp e rato r loa d Butt on = n e w J B u t t o n O p e r a t o r ( f i l e C h o o s e r , " C a n c e l " ) ;
l o ad Bu tto n .pus h ();

J L is tO per a tor m ovie L ist =


n ew J List O pera t or(m ainW i n d o w , n e w N a m e B a s e d C h o o s e r ( " m o v i e L i s t " ) ) ;
L i st Mo del list M odel = mo vieL i s t . g e t M o d e l ( ) ;
a s se rt Equ a ls(" M ovie list is t h e w r o n g s i z e " , 3 , l i s t M o d e l . g e t S i z e ( ) ) ;

Green and clean enough! Time to move on.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
In this chapter we added persistence to our application. We did it in the simplest way that could possibly
work: a plain ASCII file, one movie per line, with movie instance variables separated by a delimiter
character. This is basically the same as a Comma Separated Value file except that commas could
reasonably appear in a movie title, so we chose something more unlikely. This let us avoid having to do
something like wrap movie titles in quote characters.
In the process, we've added a File menu to the GUI. Figure 16.1 shows it.

Figure 16.1. The new File menu.

Maybe later we will have need for structured storage or even a relational database. But that is maybe
and laterYAGNI. We did what was barely sufficient for today. Maybe we won't need anything more.
"But," you say, "there's a story for it." So. . . we may not get to that. The customer might decide that
they don't need it; the project may be cancelled before then. Things change. Don't make things more
complex than you have to...until you have to. That's one of the secrets of simple design.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 17. SORTING


In the GUI, allow the movie list to be sorted by selecting from a set of options on a
menu. Two orderings are required: ascending by name, and descending by rating.
The first step is to start with the innermost stuff. In this case, we are talking about the user being able
to sort the list of movies they see. As we peel off the layers of the onion, we pass through the GUI
layer being able to change the order of the displayed list, the Mo vi e Li s tE di t or updating its view's list
in response to a change in the sorting requirements, the sorting of a Mo v ie Li s t, and finally we arrive
at comparing two Mo vie s. Now we'll back out through the layers, testing and implementing as we go.

Amazon

Prev

don't be afraid of buying books

Next

COMPARE MOVIES
Create the ability to compare two movies based on either name (ascending order) or
rating (descending order). This is needed for sorting.
The first step, at the innermost layer, is adding the ability to compare two M o v i e s. We'll use the
standard Java approach to this. Since, in this case, we need to be able to sort on different attributes
(i.e., name and rating), making Mo vie implement C o m p a r a b l e doesn't suffice. We will need to create a
C om p ar at or for each ordering we need. Here are the tests:
Test 64. Comparing two movies with the same name, based on name, should answer 0.

Test 65. Comparing two movies, the first having a lexically smaller name, based on
name, should answer a negative integer.

Test 66. Comparing two movies, the first having a lexically greater name, based on
name, should answer a positive integer.

Test 67. Comparing two movies with the same rating, based on rating, should answer 0.

Test 68. Comparing two movies, the first having a lower rating, based on rating, should
answer a negative integer.

Test 69. Comparing two movies, the first having a higher rating, based on rating, should
answer a positive integer.

Test 70. For the purposes of comparison, an unrated movie always has a lower rating.

Let's start with ordering by name. Here's a test:

Test 64: Comparing two movies with the same name, based on name,
should answer 0
p ub l ic v oid test C ompa r eSam e() {
M o vi e sta r Trek = ne w Mov ie(" S t a r T r e k " , C a t e g o r y . S C I F I , 3 ) ;

M o vi e ano t herS t arTr e k = new M o v i e ( " S t a r T r e k " , C a t e g o r y . S C I F I , 3 ) ;


M o vi eN ame C ompa r ator comp arat o r T o T e s t = n e w M o v i e N a m e C o m p a r a t o r ( ) ;
a s se rt Equ a ls(" C ompa r ing equa l m o v i e s s h o u l d r e t u r n 0 . " ,
0,
c o mpar a torT oTes t . c o m p a r e ( s t a r T r e k , a n o t h e r S t a r T r e k ) ) ;

Create a stub for M o vie Na meCo mpara t o r :


p ub l ic c las s Mov i eNam e Comp arat o r i m p l e m e n t s C o m p a r a t o r {
p u bl ic Mo v ieNa m eCom p arat or() {
su pe r() ;
}

p u bl ic in t com p are( O bjec t o1 , O b j e c t o 2 ) {


re tu rn 0 ;
}

Compile, run, green! The test is expecting 0 to be returned, which is what the stub does. The next test
will drive us to generalize.

Test 65: Comparing two movies, the first having a lexically smaller
name, based on name, should answer a negative integer
p ub l ic v oid test C ompa r eBef ore( ) {
M o vi e sta r Trek = ne w Mov ie(" S t a r T r e k " , C a t e g o r y . S C I F I , 3 ) ;
M o vi e ali e n = n ew M o vie( "Ali e n " , C a t e g o r y . S C I F I , 5 ) ;
M o vi eN ame C ompa r ator comp arat o r T o T e s t = n e w M o v i e N a m e C o m p a r a t o r ( ) ;
a s se rt Tru e ("Co m pari n g a less e r m o v i e s h o u l d r e t u r n < 0 . " ,
com p arat o rToT est. c o m p a r e ( a l i e n , s t a r T r e k ) < 0 ) ;
}

This test, of course, fails. This is good (have I emphasized this enough?) because it lets us drive the
implementation. Now we need to do some work on M o v i e N a m e C o m p a r a t o r . c o m p a r e T o ( ) . Since we
are ordering by name, and name is a S t r i n g , and S t r i n g implements C o m p a r a b l e , it makes sense
to delegate the comparison to the name:
p ub l ic i nt c ompa r e(Ob j ect o1, O b j e c t o 2 ) {
r e tu rn (( M ovie ) o1). g etNa me() . c o m p a r e T o ( ( ( M o v i e ) o 2 ) . g e t N a m e ( ) ) ;
}

Green. Now let's do some refactoring. First, we've introduced some duplication in the test class. We can
extract a common fixture:
p ro t ec te d v o id s e tUp( ) {
s t ar Tr ek = new Movi e ("St ar T r e k " , C a t e g o r y . S C I F I , 3 ) ;
c o mp ar ato r ToTe s t = n ew M ovie N a m e C o m p a r a t o r ( ) ;
}

Next, have a look at co m pare( ).Something smells very obtuse and ugly. Let's clean it up by extracting
some explaining variables, running the tests after each change. Here's the end result:
p ub l ic i nt c ompa r e(Ob j ect o1, O b j e c t o 2 ) {
M o vi e fir s tMov i e = ( Movi e)o1 ;
M o vi e sec o ndMo v ie = (Mov ie)o 2 ;
S t ri ng na m eOfF i rstM o vie = fi r s t M o v i e . g e t N a m e ( ) ;
S t ri ng na m eOfS e cond M ovie = s e c o n d M o v i e . g e t N a m e ( ) ;
r e tu rn na m eOfF i rstM o vie. comp a r e T o ( n a m e O f S e c o n d M o v i e ) ;
}

There, that's better. Next, a test to confirm the remaining class of comparison.

Test 66: Comparing two movies, the first having a lexically greater
name, based on name, should answer a positive integer
p ub l ic v oid test C ompa r eAft er() {
M o vi e sta r Wars = ne w Mov ie(" S t a r W a r s " , C a t e g o r y . S C I F I , 4 ) ;
a s se rt Tru e ("Co m pari n g a grea t e r m o v i e s h o u l d r e t u r n > 0 . " ,
com p arat o rToT est. c o m p a r e ( s t a r W a r s , s t a r T r e k ) > 0 ) ;
}

As expected, this passes right away. Next, let's look at the other ordering we need: by rating. This is
very similar to what we've just done. The only twist is that the ordering is descending rather than
ascending. First, the fixture:
p ub l ic c las s Tes t Movi e Comp arin g B y R a t i n g e x t e n d s T e s t C a s e {
p r iv at e M o vieR a ting C ompa rato r c o m p a r a t o r T o T e s t = n u l l ;
p r iv at e M o vie s tarT r ek = nul l ;
p r ot ec ted void setU p () t hrow s E x c e p t i o n {
st ar Tre k = n e w Mo v ie(" Star T r e k " , C a t e g o r y . S C I F I , 3 ) ;
co mp ara t orTo T est = new Mov i e R a t i n g C o m p a r a t o r ( ) ;
}

p u bl ic st a tic v oid m ain( Stri n g [ ] a r g s ) {


ju ni t.t e xtui . Test R unne r.ru n ( T e s t M o v i e C o m p a r i n g B y R a t i n g . c l a s s ) ;
}

And now, each of the tests. We'll just go through them one after another, since they're so similar to
what we just did.

Test 67: Comparing two movies with the same rating, based on rating,
should answer 0
p ub l ic v oid test C ompa r eSam e() {
M o vi e ano t herS t arTr e k = new M o v i e ( " S t a r T r e k " , C a t e g o r y . S C I F I , 3 ) ;
a s se rt Equ a ls(" C ompa r ing equa l l y r a t e d m o v i e s s h o u l d r e t u r n 0 . " ,
0,
c o mpar a tor To T e s t . c o m p a r e ( s t a r T r e k , a n o t h e r S t a r T r e k ) ) ;
}

Test 68: Comparing two movies, the first having a lower rating, based
on rating, should answer a negative integer
p ub l ic v oid test C ompa r eBef ore( ) {
M o vi e ali e n = n ew M o vie( "Ali e n " , C a t e g o r y . S C I F I , 5 ) ;
a s se rt Tru e ("Co m pari n g a high e r r a t e d m o v i e s h o u l d r e t u r n < 0 . " ,
com p arat o rToT est. c o m p a r e ( a l i e n , s t a r T r e k ) < 0 ) ;
}

Test 69: Comparing two movies, the first having a higher rating, based
on rating, should answer a positive integer
p ub l ic v oid test C ompa r eAft er() {
M o vi e sta r ship T roop e rs = new M o v i e ( " S t a r s h i p t r o o p e r s " , C a t e g o r y . S C I F I , 1 ) ;
a s se rt Tru e ("Co m pari n g a lowe r r a t e d m o v i e s h o u l d r e t u r n > 0 . " ,
com p arat o rToT est. c o m p a r e ( s t a r s h i p T r o o p e r s , s t a r T r e k ) > 0 ) ;
}

Test 70: For the purposes of comparison, an unrated movie always has
a lower rating
Here's the only difference: With ratings we have the concept of an unrated movie; we didn't have a
corresponding unnamed movie to deal with earlier.
p ub l ic v oid test C ompa r ingW ithU n r a t e d ( ) {
M o vi e jur a ssic P ark = new Mov i e ( " J u r a s s i c P a r k " , C a t e g o r y . S C I F I , - 1 ) ;
a s se rt Tru e ("Co m pari n g wi th a n u n r a t e d m o v i e s h o u l d r e t u r n > 0 . " ,
com p arat o rToT est. c o m p a r e ( j u r a s s i c P a r k , s t a r T r e k ) > 0 ) ;
}

And, here's the comparator:


p u bl ic cl a ss M o vieR a ting Comp a r a t o r i m p l e m e n t s C o m p a r a t o r {
pu bl ic M ovie R atin g Comp arat o r ( ) {
su per ( );
}

p ub l ic i nt c ompa r e(Ob j ect o1, O b j e c t o 2 ) {


Mo vi e f i rstM o vie = (Mo vie) o 1 ;
Mo vi e s e cond M ovie = (M ovie ) o 2 ;
in t rat i ngOf F irst M ovie = f i r s t M o v i e . g e t R a w R a t i n g ( ) ;
in t rat i ngOf S econ d Movi e = s e c o n d M o v i e . g e t R a w R a t i n g ( ) ;
re tu rn r atin g OfSe c ondM ovie - r a t i n g O f F i r s t M o v i e ;
}
}

Notice that we reference a new accessor, M o v i e . g e t R a w R a t i n g ( ) . This method doesn't throw an


U nr a te dM ovi e Exce p tion if the movie has no rating. Instead, it returns - 1 , the value indicating an
unrated Mov i e. This works perfectly with our comparison method: unrated M o v i e s compare as the
lowest rating value.
Now we have away of determining two orderings on a list of M o v i e s. The next step is to use that to
provide Mo vie L ist with the capability of sorting.

Amazon

Prev

don't be afraid of buying books

Next

SORT A MOVIELIST
Add the capability to ask a MovieList to provide its list sorted on a specific attribute, for the
moment this is one of name or rating.
Sorting is different than the category filtering we did earlier. It doesn't change the number of items, it only
changes their order. With that in mind, let's approach this by making sorting an in-place operation. By that
I mean that we will ask a Movie List to sort itself, and it will (potentially) modify the order of its items.
Test 71. Sorting an empty list has no effect on it.

Test 72. Sorting a list, by name, that is already sorted has no effect on it.

Test 73. Sorting a list, by name, that is in reverse order puts it in order.

Test 74. Sorting a list, by rating, that is already sorted has no effect on it.

Test 75. Sorting a list, by rating, that is in reverse order puts it in order.

Test 76. Sorting a list, by rating, that has duplicates puts it in order; the duplicates being in
their original order relative to one another.

Test 77. Sorting a randomly ordered list by name puts it in order, by name.

Test 78. Sorting a randomly ordered list by rating puts it in order, by name.

We'll tackle name sorting first. We start simply with what it means to sort an empty list.

Test 71: Sorting an empty list has no effect on it


p u bl i c vo id tes t Sor t ingEm ptyL is t ( ) {
em p t yLi st .sor t Usi n g(nam eCom pa r a t o r ) ;

as s e rtE qu als( " Sor t ing a emp ty l i s t s h o u l d n ' t c h a n g e i t s s i z e . " ,


0,
e mpt y List. size () ) ;

To make this pass we need to stub s ort U s i n g ( ) :


p u bl i c vo id sor t Usi n g(Com para to r a C o m p a r a t o r ) {
}

Not much, but it is enough to make the test compile, and pass.
The next test isn't much different. It tests that sorting an already sorted list doesn't change the order.

Test 72: Sorting a list, by name, that is already sorted has no effect on it
p u bl i c vo id tes t Sor t ingSo rted Li s t B y N a m e ( ) t h r o w s D u p l i c a t e M o v i e E x c e p t i o n {
Ve c t or so rted M ovi e s = n ew V ec t o r ( ) ;
so r t edM ov ies. a dd( n ew
so r t edM ov ies. a dd( n ew
so r t edM ov ies. a dd( n ew
so r t edM ov ies. a dd( n ew
so r t edM ov ies. a dd( n ew
so r t edM ov ies. a dd( n ew
so r t edM ov ies. a dd( n ew

Mo vie( "A " , C a t e g o r y . S C I F I ,


Mo vie( "B " , C a t e g o r y . S C I F I ,
Mo vie( "C " , C a t e g o r y . S C I F I ,
Mo vie( "D " , C a t e g o r y . S C I F I ,
Mo vie( "E " , C a t e g o r y . S C I F I ,
Mo vie( "F " , C a t e g o r y . S C I F I ,
Mo vie( "G " , C a t e g o r y . S C I F I ,

5));
4));
3));
2));
1));
0));
-1));

Mo v i eLi st sor t edL i st = new Mo v i e L i s t ( ) ;


It e r ato r i = s ort e dMovi es.i te r a t o r ( ) ;
wh i l e ( i. hasN e xt( ) ) {
s o rte dL ist. a dd( ( Movie ) i. ne x t ( ) ) ;
}
so r t edL is t.so r tUs i ng(na meCo mp a r a t o r ) ;
as s e rtE qu als( " Sor t ing s houl d n o t c h a n g e s i z e . " ,
s ort e dMovi es.s iz e ( ) ,
s ort e dList .siz e( ) ) ;
It e r ato r orig i nal = sor tedM ov i e s . i t e r a t o r ( ) ;
It e r ato r sort e d = sorte dLis t. i t e r a t o r ( ) ;

wh i l e ( or igin a l.h a sNext ()) {


a s s ert Eq uals ( "So r ted l ist sh o u l d s t a y s o r t e d w h e n s o r t e d . " ,
ori g inal. next () ,
sor t ed.

To compile we need to add it e rato r() to M o v i e L i s t . We'll skip the stub stage and go right to the real
implementation since it's so obvious: it should return an iterator over the underlying collection:
p u bl i c It er ator ite r ator( ) {
re t u rn mo vies . ite r ator( );
}

Now it compiles and passes. When we wrote the test we added a lot of fixture code. Let's take a moment to
move that to se t Up() to make the test cleaner and clearer. Now we have:
p u bl i c vo id tes t Sor t ingSo rted Li s t B y N a m e ( ) t h r o w s D u p l i c a t e M o v i e E x c e p t i o n {
so r t edL is t.so r tUs i ng(na meCo mp a r a t o r ) ;
as s e rtE qu als( " Sor t ing s houl d n o t c h a n g e s i z e . " ,
s ort e dMovi es.s iz e ( ) ,
s ort e dList .siz e( ) ) ;

It e r ato r orig i nal = sor tedM ov i e s . i t e r a t o r ( ) ;


It e r ato r sort e d = sorte dLis t. i t e r a t o r ( ) ;
wh i l e ( or igin a l.h a sNext ()) {
a s ser tE qual s ("S o rted list s h o u l d s t a y s o r t e d w h e n s o r t e d . " ,
ori g inal. next () ,
sor t ed.ne xt() );
}

Exercises
19.

What does the fixture look like?

The next test will take a reverse ordered list, sort it, and verify that it has been sorted.

Test 73: Sorting a list, by name, that is in reverse order puts it in order
p u bl i c vo id tes t Sor t ingRe vers eS o r t e d L i s t B y N a m e ( ) {
re v e rse dL ist. s ort U sing( name Co m p a r a t o r ) ;
It e r ato r orig i nal = sor tedM ov i e s . i t e r a t o r ( ) ;
It e r ato r sort e d = rever sedL is t . i t e r a t o r ( ) ;
wh i l e ( or igin a l.h a sNext ()) {
a s s ert Eq uals ( "Re v ersed lis t s h o u l d b e s o r t e d w h e n s o r t e d . " ,
ori g inal. next () ,
sor t ed.ne xt() );
}
}

Next, we need a reversed list. The straightforward way to do this is something like:
r e ve r s edL is t = n ew M ovieL ist( );
i n t n u mbe rO fMov i es = sort edMo vi e s . s i z e ( ) ;
f o r ( i nt i = 0; i < numbe rOfM ov i e s ; i + + ) {
re v e rse dL ist. a dd( ( Movie )sor te d M o v i e s . g e t ( n u m b e r O f M o v i e s - ( i + 1 ) ) ) ;
}

If we're doing that we might as well incorporate the construction of the sorted list we used in the last test.
So now we have:
s o rt e d Lis t = ne w Mo v ieLis t();
r e ve r s edL is t = n ew M ovieL ist( );
i n t n u mbe rO fMov i es = sort edMo vi e s . s i z e ( ) ;
f o r ( i nt i = 0; i < numbe rOfM ov i e s ; i + + ) {
re v e rse dL ist. a dd( ( Movie )sor te d M o v i e s . g e t ( n u m b e r O f M o v i e s - ( i + 1 ) ) ) ;
so r t edL is t.ad d ((M o vie)s orte dM o v i e s . g e t ( i ) ) ;
}

OK. Now we compile and get a red bar because the result of sorting the list isn't sorted. Now it's time to
actually do something about sorting a Mo v i e L i s t . The good thing is that the Java Collection framework
provides what we need to do this. That's why we started off with a task to build C o m p a r a t o r s :
p u bl i c vo id sor t Usi n g(Com para to r a C o m p a r a t o r ) {
Co l l ect io ns.s o rt( m ovies , aC om p a r a t o r ) ;
}

Green!
We need to clean up the tests a bit, though. The most recent test introduced some duplication, specifically,
the code that tests that a Mov i eLis t is in sorted order. We can use Extract Method to take care of that:
p u bl i c vo id tes t Sor t ingSo rted Li s t B y N a m e ( ) t h r o w s E x c e p t i o n {
so r t edL is t.so r tUs i ng(na meCo mp a r a t o r ) ;
ch e c kTh at List I sSo r ted(" Sort ed l i s t s h o u l d s t a y s o r t e d w h e n s o r t e d . " ,
so rted Li s t ) ;
}
p u bl i c vo id tes t Sor t ingRe vers eS o r t e d L i s t B y N a m e ( ) {
re v e rse dL ist. s ort U sing( name Co m p a r a t o r ) ;
ch e c kTh at List I sSo r ted(" Reve rs e d l i s t s h o u l d b e s o r t e d w h e n s o r t e d . " ,
r ever se d L i s t ) ;
}
p r iv a t e v oi d ch e ckT h atLis tIsS or t e d ( S t r i n g m e s s a g e , M o v i e L i s t l i s t T o C h e c k ) {
as s e rtE qu als( " Sor t ing s houl dn ' t c h a n g e l i s t s i z e . " ,
s o rte d Movie s.si ze ( ) ,
l i stT o Check .siz e( ) ) ;
It e r ato r orig i nal = sor tedM ov i e s . i t e r a t o r ( ) ;
It e r ato r sort e d = listT oChe ck . i t e r a t o r ( ) ;
wh i l e ( or igin a l.h a sNext ()) {
a s ser tE qual s (me s sage, ori gi n a l . n e x t ( ) , s o r t e d . n e x t ( ) ) ;
}
}

There, that cleans things up nicely. Next, we need to take care of sorting a M o v i e L i s t based on rating.
This will be very similar to what we just did, so as we did in the first task, we'll show it all in one step
here, even though we did it one test at a time.

Test 74: Sorting a list, by rating, that is already sorted has no effect on it
p u bl i c vo id tes t Sor t ingSo rted Li s t B y R a t i n g ( ) t h r o w s E x c e p t i o n {
so r t edL is t.so r tUs i ng(ra ting Co m p a r a t o r ) ;
ch e c kTh at List I sSo r ted(" Sort ed l i s t s h o u l d s t a y s o r t e d w h e n s o r t e d . " ,
s orte dL i s t ) ;
}

Test 75: Sorting a list, by rating, that is in reverse order puts it in order
p u bl i c vo id tes t Sor t ingRe vers eS o r t e d L i s t B y R a t i n g ( ) {
re v e rse dL ist. s ort U sing( rati ng C o m p a r a t o r ) ;
ch e c kTh at List I sSo r ted(" Reve rs e d l i s t s h o u l d b e s o r t e d w h e n s o r t e d . " ,
re vers ed L i s t ) ;
}

As expected, everything works. We already have the C o m p a r a t o r s working as well as the basic sorting
mechanism. Using a different (but tested) C o m p a r a t o r should work without any problems.

mechanism. Using a different (but tested) C o m p a r a t o r should work without any problems.
Hmmm... that seemed too easy. Let's try another test. Movie names are unique, but ratings aren't. Let's
write a test that takes a list with duplicate ratings and sorts it.

Test 76: Sorting a list, by rating, that has duplicates puts it in order; the
duplicates being in their original order relative to one another
p u bl i c vo id tes t Sor t ingLi stWi th D u p l i c a t e s B y R a t i n g ( ) t h r o w s E x c e p t i o n {
Mo v i e u nr ated M ovi e = ne w Mo vi e ( " U n r a t e d " , C a t e g o r y . S C I F I , - 1 ) ;
so r t edL is t.ad d (un r atedM ovie );
so r t edM ov ies. a dd( u nrate dMov ie ) ;
so r t edL is t.so r tUs i ng(ra ting Co m p a r a t o r ) ;
ch e c kTh at List I sSo r ted(" List w i t h d u p l i c a t e s h a s b a d o r d e r i n g . " ,
s orte dL i s t ) ;
}

Green bar. I'm still not confident enough. I want to see it sorting a jumbled list, using both C o m p a r a t o r s .
First, a tweak to the fixture:
p r ot e c ted v oid s etU p () th rows E x c e p t i o n {
// . . .
ju m b led Li st = new Movie List () ;
Ve c t or mo vies = n e w Vec tor( so r t e d M o v i e s ) ;
fo r (in t i = s ort e dMovi es.s iz e ( ) ; i > 0 ; i - - ) {
i n t i nd ex = ( int ) Math .flo or ( ( M a t h . r a n d o m ( ) * m o v i e s . s i z e ( ) ) ) ;
j u mbl ed List . add ( (Movi e)mo vi e s . g e t ( i n d e x ) ) ;
m o vie s. remo v e(i n dex);
}
// . . .
}

Test 77: Sorting a randomly ordered list by name puts it in order, by name
p u bl i c vo id tes t Sor t ingJu mble dL i s t B y N a m e ( ) t h r o w s E x c e p t i o n {
ju m b led Li st.s o rtU s ing(n ameC om p a r a t o r ) ;
ch e c kTh at List I sSo r ted(" Jumb le d l i s t s h o u l d n o w b e s o r t e d b y n a m e . " ,
j umbl ed L i s t ) ;
}

Test 78: Sorting a randomly ordered list by rating puts it in order, by name
p u bl i c vo id tes t Sor t ingJu mble dL i s t B y R a t i n g ( ) t h r o w s E x c e p t i o n {
ju m b led Li st.s o rtU s ing(r atin gC o m p a r a t o r ) ;
ch e c kTh at List I sSo r ted(" Jumb le d l i s t s h o u l d n o w b e s o r t e d b y r a t i n g . " ,
j umbl ed L i s t ) ;
}

We've shown the addition to the fixture already factored to s e t U p ( ) , but remember that we would have
started off with it in t est So rtin gJumb l e d L i s t B y N a m e ( ) , then extracted it into s e t U p ( ) when we
needed the same thing in te stSo rting J u m b l e d L i s t B y R a t i n g .
That passes all the tests. I'm finally confident enough to move on. I do mean all the tests. The A l l T e s t s
suite is getting bigger:
p u bl i c cl as s Al l Tes t s ext ends T e s t S u i t e {
pu b l ic st atic voi d main (Str in g [ ] a r g s ) {
j u nit .t extu i .Te s tRunn er.r un ( A l l T e s t s . c l a s s ) ;
}
pu b l ic st atic Tes t suit e() {
T e stS ui te s u ite = new Tes tS u i t e ( " T e s t f o r c o m . s a o r s a . n o w p l a y i n g . t e s t s " ) ;
s u ite .a ddTe s t(n e w Tes tSui te ( T e s t C u s t o m L i s t R e n d e r e r . c l a s s ) ) ;
s u ite .a ddTe s t(n e w Tes tSui te ( T e s t M o v i e . c l a s s ) ) ;

s u ite .a ddTe s t(n e w


s u ite .a ddTe s t(n e w
s u ite .a ddTe s t(n e w
s u ite .a ddTe s t(n e w

Tes tSui te ( T e s t M o v i e C o m p a r i n g B y N a m e . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t E d i t o r . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t E d i t o r L o a d i n g . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t E d i t o r S a v i n g . c l a s s ) ) ;

s u ite .a ddTe s t(n e w


s u ite .a ddTe s t(n e w
s u ite .a ddTe s t(n e w
s u ite .a ddTe s t(n e w
s u ite .a ddTe s t(n e w
s u ite .a ddTe s t(n e w

Tes tSui te ( T e s t M o v i e L i s t E d i t o r W i t h C a t e g o r y F i l t e r i n g . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t R e a d i n g . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t S o r t i n g . c l a s s ) ) ;
Tes tSui te ( T e s t E m p t y M o v i e L i s t . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t W i t h O n e M o v i e . c l a s s ) ) ;
Tes tSui te ( T e s t M o v i e L i s t W i t h T w o M o v i e s . c l a s s ) ) ;

s u ite .a ddTe s t(n e w Tes tSui te ( T e s t M o v i e L i s t W i t h M i x e d L i s t . c l a s s ) ) ;


s u ite .a ddTe s t(n e w Tes tSui te ( T e s t M o v i e L i s t W i t h P o p u l a t e d L i s t . c l a s s ) ) ;
s u ite .a ddTe s t(n e w Tes tSui te ( T e s t M o v i e L i s t W r i t i n g . c l a s s ) ) ;
r e tur n suit e ;

Amazon

Prev

don't be afraid of buying books

Next

ASK A MOVIELISTEDITOR FOR SORTED LISTS


Add sorting capabilities to MovieListEditor.
In the last task we did all the work required to show that the sorting capabilities work in the various
situations. For this task, we just need to test that M o v ieL i st E di to r responds properly to a request to
sort the list. Actually, one test might suffice:
Test 79. Asking the logical layer to sort its list causes the associated view's list to be
updated to one that is in order (order depends on what is being sorted on).

First, the extended fixture:


pub l ic cl ass T est Mo vieL istE dito r S ort i n g e x t en ds C o mm o nT es t Mo vi e Li st E di to r {
p r ivate Ve ct or so rted Movi es;
p r otect ed vo id se tUp( ) th rows E xce p t ion {
super .se tU p() ;
sorte dMo vi es = new Vect or() ;
sorte dMo vi es. ad d(st arTr ek);
sorte dMo vi es. ad d(st arWa rs);
sorte dMo vi es. ad d(st arga te);
sorte dMo vi es. ad d(th eShi ning ) ;
}
p u blic sta ti c v oi d ma in(S trin g [ ] a r gs) {
junit .te xt ui. Te stRu nner .run ( T est M o vie L i st Ed it o rS o rt in g .c la s s) ;
}
}

Now for the test.

Test 79: Asking the logical layer to sort its list causes the associated
view's list to be updated to one that is in order (order depends on what
is being sorted on)
pub l ic vo id te stS or ting () {
m o ckVie w.s et Mov ie s(mo vies );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Mov ie s(so rted Movi e s );
c o ntrol .se tV oid Ca llab le(1 );
c o ntrol .ac ti vat e( );
M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, mo ck V ie w) ;
e d itor. sor tU sin g( new Movi eNam e C omp a r ato r ( )) ;
c o ntrol .ve ri fy( );
}

Add an empty stub for M ovie List Edito r .so r t Usi n g () and run it. This, of course, fails because

sor t Using () does nothing. We need it to sort the list and update the display:
pub l ic vo id so rtU si ng(C ompa rato r aCo m p ara t o r) {
m o vies. sor tU sin g( aCom para tor) ;
u p dateA ndD is pla yF ilte redM ovie s ( );
}

Green bar! That was simple. There's a lesson here that reinforces and validates the way we've been
working: Do the real work in the model without worrying about any sort of user interface. You will spend
most of your time there, and it's the easiest part of a system to write good tests for. It also keeps the
interface layers as thin as possible. This, in turn, makes it easier to replace the interface with something
else. Splitting the interface into operation and presentation layers helps too.

Amazon

Prev

don't be afraid of buying books

Next

ADD A WAY TO SORT TO THE GUI


Add a View menu to the GUI with an option for each attribute that can be sorted on. Selecting
one of them results in the visible movie list being sorted by that attribute.
The last task for the sorting story is to add access to the new sorting capabilities to the Swing GUI.
Test 80. Selecting "Sort by name" from the "View" menu causes the displayed list to be sorted
by name.

Test 81. Selecting "Sort by rating" from the "View" menu causes the displayed list to be sorted
by rating.

Since this is conceptually different than our previous GUI tests, and requires a slightly different fixture, we'll
create a new T e s t C a s e :
p ublic c l a s s T e s t S w i n g M o v i e L i s t E di to rV iewSorting extends TestCase {
priva t e M o v i e L i s t m o v i e L i s t ;
priva t e M o v i e s t a r W a r s ;
priva t e M o v i e s t a r T r e k ;
priva t e M o v i e s t a r g a t e ;
priva t e M o v i e t h e S h i n i n g ;
priva t e V e c t o r m o v i e s ;
priva t e J F r a m e O p e r a t o r m a i n W i n do w;
priva t e M o v i e L i s t E d i t o r e d i t o r ;
priva t e V e c t o r s o r t e d M o v i e s ;
priva t e J M e n u B a r O p e r a t o r m e n u b a r;
priva t e J M e n u O p e r a t o r v i e w M e n u ;
prote c t e d v o i d s e t U p ( ) t h r o w s Ex ce pt ion {
sup e r . s e t U p ( ) ;
Swi n g M o v i e L i s t E d i t o r V i e w . s t a rt () ;
sta r g a t e = n e w M o v i e ( " S t a r g a te ", C ategory.SCIFI, -1);
the S h i n i n g = n e w M o v i e ( " T h e Sh in in g", Category.HORROR, 2);
sta r W a r s = n e w M o v i e ( " S t a r W ar s" , Category.SCIFI, 5);
sta r T r e k = n e w M o v i e ( " S t a r T re k" , Category.SCIFI, 3);

mov i e s = n e w V e c t o r ( ) ;
mov i e s . a d d ( s t a r g a t e ) ;
mov i e s . a d d ( t h e S h i n i n g ) ;
mov i e s . a d d ( s t a r W a r s ) ;
mov i e s . a d d ( s t a r T r e k ) ;
sor t e d M o v i e s = n e w V e c t o r ( ) ;
sor t e d M o v i e s . a d d ( s t a r T r e k ) ;
sor t e d M o v i e s . a d d ( s t a r W a r s ) ;
sor t e d M o v i e s . a d d ( s t a r g a t e ) ;
sor t e d M o v i e s . a d d ( t h e S h i n i n g ) ;
mov i e L i s t = n e w M o v i e L i s t ( ) ;
mov i e L i s t . a d d ( s t a r g a t e ) ;
mov i e L i s t . a d d ( t h e S h i n i n g ) ;
mov i e L i s t . a d d ( s t a r W a r s ) ;
mov i e L i s t . a d d ( s t a r T r e k ) ;

mai n W i n d o w = n e w J F r a m e O p e r a to r( "M ovie List");


edi t o r = n e w M o v i e L i s t E d i t o r (m ov ie List,
( Sw in gMovieListEditorView)mainWindow. getWindow());

men u b a r = n e w J M e n u B a r O p e r a t or (m ai nWindow);
vie w M e n u = n e w J M e n u O p e r a t o r (m en ub ar, "View");
vie w M e n u . p u s h ( ) ;

prote c t e d v o i d t e a r D o w n ( ) t h r o ws E xc eption {
sup e r . t e a r D o w n ( ) ;
mai n W i n d o w . d i s p o s e ( ) ;
}

Test 80: Selecting "Sort by name" from the "View" menu causes the
displayed list to be sorted by name
p ublic v o i d t e s t S o r t i n g B y N a m e ( ) {
JMenu I t e m O p e r a t o r s o r t B y N a m e I t em =
n e w J M e n u I t e m O p e r a t o r ( m ai nW in dow,
n e w N am eB as edChooser("sortByName"));
sortB y N a m e I t e m . p u s h ( ) ;
JList O p e r a t o r m o v i e L i s t =
n e w J L i s t O p e r a t o r ( m a i n W i n do w,
n e w N a m e Ba se dC hooser("movieList"));
ListM o d e l l i s t M o d e l = m o v i e L i s t. ge tM odel();
asser t E q u a l s ( " M o v i e l i s t i s t h e wr on g size",sortedMovies.size(),
l i s t M o d e l . g e t S i z e ( )) ;

for ( i n t i = 0 ; i < s o r t e d M o v i es .s iz e(); i++) {


asser t E q u a l s ( " M o v i e l i s t c o n t a in s ba d movie at index " +i,
s o r t e d M o v i e s . g e t ( i) ,
l i s t M o d e l . g e t E l e m en tA t( i));
}

This fails with an error when looking for the View menu. We need to add it to the GUI:
p rivate J M e n u B a r i n i t M e n u B a r ( ) {
//...
JMenu v i e w M e n u = n e w J M e n u ( " V i ew ") ;
menuB a r . a d d ( v i e w M e n u ) ;
viewM e n u . a d d ( i n i t S o r t B y N a m e I t e m( )) ;
retur n m e n u B a r ;
}
p rivate J M e n u I t e m i n i t S o r t B y N a m e It em () {
JMen u I t e m s o r t B y N a m e I t e m = n e w JM en uItem("Sort by name");
sort B y N a m e I t e m . s e t N a m e ( " s o r t B yN am e" );
retu r n s o r t B y N a m e I t e m ;
}

Now we see a failure due to the list not being sorted after the Sort by name menu item is selected. To deal
with this, we need to add an action listener which will forward the request to the associated editor:
s ortByN a m e I t e m . a d d A c t i o n L i s t e n e r (n ew A ctionListener() {
publi c v o i d a c t i o n P e r f o r m e d ( A c ti on Ev ent e) {
myEd i t o r . s o r t U s i n g ( n e w M o v i e N am eC om parator());
}});

Green.

Test 81: Selecting "Sort by rating" from the "View" menu causes the
displayed list to be sorted by rating
Now we do the same for Sort by rating. Here's the test:
p rotect e d v o i d s e t U p ( ) t h r o w s E x ce pt io n {
//...
ratin g S o r t e d M o v i e s = n e w V e c t o r( );
ratin g S o r t e d M o v i e s . a d d ( s t a r W a r s) ;
ratin g S o r t e d M o v i e s . a d d ( s t a r T r e k) ;
ratin g S o r t e d M o v i e s . a d d ( t h e S h i n in g) ;
ratin g S o r t e d M o v i e s . a d d ( s t a r g a t e) ;
//...
}
p ublic v o i d t e s t S o r t i n g B y R a t i n g ( ) {
JMenu I t e m O p e r a t o r s o r t B y R a t i n g It em =
n e w J M e n u I t e m O p e r a t o r ( ma in Wi ndow,
ne w Na meBasedChooser("sortByRating"));
sortB y R a t i n g I t e m . p u s h ( ) ;
JList O p e r a t o r m o v i e L i s t =
n e w J L i s t O p e r a t o r ( m a i n W i n do w,
n e w N a m eB as ed Chooser("movieList"));

ListM o d e l l i s t M o d e l = m o v i e L i s t. ge tM odel();
asser t E q u a l s ( " M o v i e l i s t i s t h e wr on g size",
r a t i n g S o r t e d M o v i e s. si ze (),
l i s t M o d e l . g e t S i z e () );

for ( i n t i = 0 ; i < r a t i n g S o r t ed Mo vi es.size(); i++) {


ass e r t E q u a l s ( " M o v i e l i s t c o n ta in s bad movie at index " +i,
r a t i n g S o r t e d M o v ie s. ge t(i),
l i s t M o d e l . g e t E l em en tA t(i));
}

And here's the new menu item code to make it pass (done in one bigger step):
p rivate J M e n u B a r i n i t M e n u B a r ( ) {
//. . .
viewM e n u . a d d ( i n i t S o r t B y R a t i n g I te m( )) ;
//. . .
}
p rivate J M e n u I t e m i n i t S o r t B y R a t i ng It em () {
JMenu I t e m s o r t B y R a t i n g I t e m = n ew J Me nuItem("Sort by rating");
sortB y R a t i n g I t e m . s e t N a m e ( " s o r t By Ra ti ng");
sortB y R a t i n g I t e m . a d d A c t i o n L i s t en er (n ew ActionListener() {
pub l i c v o i d a c t i o n P e r f o r m e d ( Ac ti on Event e) {
m y E d i t o r . s o r t U s i n g ( n e w M ov ie Ra tingComparator());
}}) ;
retur n s o r t B y R a t i n g I t e m ;
}

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
Working on this story illustrated how most of the work can (and should) be done in the guts of the
application. Sometimes this is called the model or problem domain code. Do all you can there, and put
good, well-crafted interfaces in place. This makes it easy, and sometimes trivial, to expose new
functionality in the interface layer(s).
The new GUI features are shown in Figures 17.1 and 17.2.

Figure 17.1. The GUI showing sorting by name.

Figure 17.2. The GUI showing sorting by rating.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 18. MULTIPLE RATINGS


Movies can have more than one rating, each with an identified source. Selecting a movie
should show all of its ratings. There should also be a way to add a rating. A movie's
displayed rating should now be the average of all of its individual ratings.
Here's an interesting story that extends an earlier one. We've previously added a rating to Mo v ie ; now
we have to extend that to handle multiple ratings and report the average. Furthermore, we need to be
able to identify the source of each rating. We'll take it one step at a time.

Amazon

Prev

don't be afraid of buying books

Next

MULTIPLE RATINGS
Add support for multiple ratings rather than just one. Provide access to the average of all the
ratings.
The first task is simply to support more than one rating value, and provide access to an average. Also, we have
to maintain the current behavior (i.e., keep all the existing tests passing) unless it is overridden by the new
behavior. For example, set R a tin g () doesn't make sense anymore.
Test 82. Constructing a Movie with no arguments creates an unrated Movie.

Test 83. Add a single rating to an unrated movie. Asking for the average rating answers the
rating just added.

Test 84. Add several ratings: 3, 5, and 4. Asking for the average rating answers 4.

Test 85. Add several ratings: 3, 5, 5, and 3. Asking for the average rating answers 4.

Test 86. Writing movies uses for format: <name> | <category> | <total rating> | <number of
ratings>.

Test 87. Reading movies uses for format: <name> | <category> | <total rating> | <number of
ratings>.

For simplicity, we'll add our new tests to T est Movie .

Test 82: Constructing a Movie with no arguments creates an unrated Movie


p ubli c v oi d t e stU n r ate d C on st ru c tor() {
Mov ie st ar T r ek = new M ov ie (" S tar Trek",Catego ry.SCIFI);
try {
s ta r Tr ek . g etR a t ing ( ) ;
f ai l (" Ge t t ing r ati n g o f an unrated Movie sh ould throw UnratedMovieException");
} c at c h (U n r ate d E xce p t io n ex ) {
}
}

Simply chaining to the more general constructor will get this working:

p ubli c M ov ie ( S tri n g na m e , Ca te g ory category) {


thi s( n am e, c ate g o ry, - 1) ;
}

Test 83: Add a single rating to an unrated movie. Asking for the average
rating answers the rating just added
p ubli c v oi d t e stA d d ing O n eR at in g () throws Except ion {
Mov ie st ar T r ek = new M ov ie (" S tar Trek",Catego ry.SCIFI);
sta rT r ek .a d d Rat i n g(3 ) ;
ass er t Eq ua l s ("B a d av e r ag e ra t ing of 1.",3,sta rTrek.getRating());
}

We need a stub a d d Rat i n g() :


p ubli c v oi d a d dRa t i ng( i n t aR at i ng) {
}

This causes the test to fail, throwing an Un rated Excep tion. Let's have addRating() do something:
p ubli c v oi d a d dRa t i ng( i n t aR at i ng) {
rat in g = a R a tin g ;
}

Green. Hmm...we have some common fixture in the two tests that should be refactored out. But it's not used
by the previous tests in T e stM o vi e , and what's more, the new tests don't use the existing fixture. We need to
create a new T es t C ase to correspond with the new fixture:
p ubli c c la ss T est M o vie R a ti ng s e xtends TestCase {
pub li c v oi d tes t U nra t e dC on st r uctor() {
M ov i e st a r Tre k = n e w M ov ie ( "Star Trek",Cate gory.SCIFI);
t ry {
s t ar Tr e k .ge t R ati n g () ;
f a il (" G e tti n g ra t i ng o f a n unrated Movie should throw UnratedMovieException");
} c a tc h ( U nra t e dEx c e pt io n e x) {
}
}

pub li c v oi d tes t A ddi n g On eR at i ng() throws Exce ption {


M ov i e st a r Tre k = n e w M ov ie ( "Star Trek",Cate gory.SCIFI);
s ta r Tr ek . a ddR a t ing ( 3 );
a ss e rt Eq u a ls( " B ad a v er ag e r ating of 1.",3,s tarTrek.getRating());
}

pub li c s ta t i c v o i d m a i n( St ri n g[ ] args) {
j un i t. te x t ui. T e stR u n ne r. ru n (TestMovieRating s.class);
}

Run it to make sure it works as it should (i.e., both tests should pass). Good. Now we can refactor the fixture
into a se t Up () method.

Exercises
20.

Do the extraction of the fixture.

We can see the inductive testing pattern emerging:


1. test the basis case (no rating)
2. test the next simplest case (one rating)
3. test the general case (more than 1 rating)

Test 84: Add several ratings: 3, 5, and 4. Asking for the average rating
answers 4
To test the general case, let's try adding three ratings:
p ubli c v oi d t e stA d d ing T h re eR at i ngs() throws Exc eption {
sta rT r ek .a d d Rat i n g(3 ) ;
sta rT r ek .a d d Rat i n g(5 ) ;
sta rT r ek .a d d Rat i n g(4 ) ;
ass er t Eq ua l s ("B a d av e r ag e ra t ing after adding 3, 5, and 4.",
4,
st a r Tre k . ge tR at i ng());
}

Red bar! Good, now we have some work to do. The failure is:
j unit .f r am ew o r k.A s s ert i o nF ai le d Error:
Bad a v er ag e rat i n g a f t er a dd i ng 3, 5, and 4.
exp ec t ed :< 5 >
but w a s: <4 >

Let's try the simplest thing that could possibly work:


p ubli c v oi d a d dRa t i ng( i n t aR at i ng) {
if (r a ti ng = = - 1 ) {
r at i ng = a Rat i n g;
} e ls e {
r at i ng = ( rat i n g + a Ra ti ng ) /2;
}
}

Green. Nice. Not an overly elegant solution, but it works...for now. Let's try a few other tests. Remember that
averaging integers involves rounding. That can cause trouble if we don't do it right. Let's try another test with
some other numbers, some that might cause some problems.

Test 85: Add several ratings: 3, 5, 5, and 3. Asking for the average rating

answers 4
p ubli c v oi d t e stA d d ing T h re eO th e rRatings() throw s Exception {
sta rT r ek .a d d Rat i n g(3 ) ;
sta rT r ek .a d d Rat i n g(5 ) ;
sta rT r ek .a d d Rat i n g(5 ) ;
sta rT r ek .a d d Rat i n g(3 ) ;
ass er t Eq ua l s ("B a d av e r ag e ra t ing after adding 3, 5, 5, and 3.",
4,
st a r Tre k . ge tR at i ng());
}

This fails:
j unit .f r am ew o r k.A s s ert i o nF ai le d Error:
Bad a v er ag e rat i n g a f t er a dd i ng 3, 5, 5, and 3.
exp ec t ed :< 4 >
but w a s: <3 >

What's going on? Let's do some math:


(3 + 5 +5+3)/4

(((((3 + 5)/2) + 5)/2) + 3)/2

There, this test brings to light a rounding error. Our simple implementation has the potential to make a
rounding error each time a rating is added. This is compounded with each rating. We can do one of three
things:
1. Keep the running average as a floating point, rounding only when the average rating is requested (and
even then, only rounding the value that is returned, not the stored average).
2. Store all ratings, taking all into account when the average is requested.
3. Keep track of a running total and a count of the number of ratings the total represents. Calculate the
average when asked to, from those two items.
What's the simplest? I'll go with the third option:
p ubli c v oi d a d dRa t i ng( i n t aR at i ng) {
if (r a ti ng = = - 1 ) {
r at i ng = a Rat i n g;
} e ls e {
r at i ng + = aRa t i ng;
}
num be r Of Ra t i ngs + + ;
}

p ubli c i nt g e t Rat i n g() t hr ow s U nratedException {


if (h a sR at i n g() ) {
r et u rn r a t ing / nu m b er Of Ra t ings;
} e ls e {
th ro w n ew U nra t e dEx c e pt io n( n ame);
}
}

Green. Now let's look at what we have when a Mo vie is unrated: numberOfRatings == 0. Before, we were
using a special value of r ati n g (specifically -1) as a flag for that state. That's never a pretty solution, and
now we have an opportunity to improve it. We have to start running all the tests for this part because we want
our changes to preserve existing behavior. Here's the new version of the effected methods:
p ubli c c la ss M o vie {
//. ..
pri va t e in t rat i n g = 0 ;
//. ..
pri va t e in t num b e rOf R a ti ng s = 0;
pub li c M ov i e (Mo v i e o r i gi na l) {
n am e = o r i gin a l .na m e ;
r at i ng = o rig i n al. r a ti ng ;
n um b er Of R a tin g s = o r ig in al . numberOfRatings;
c at e go ry = or i g ina l . ca te go r y;
}
pub li c M ov i e (St r i ng a N am e, C a tegory aCategory , int aRating) {
c he c kN ul l ( aNa m e );
c he c kE mp t y (aN a m e);
n am e = a N a me;
c at e go ry = (a C a teg o r y != n u ll) ? aCategory : Category.UNCATEGORIZED;
i f ( aR at i n g > - 1) {
r a ti ng = aR a t ing ;
n u mb er O f Rat i n gs = 1;
}
}
//. ..
pub li c b oo l e an h a sRa t i ng () {
r et u rn n u m ber O f Rat i n gs > 0 ;
}
pub li c i nt g etR a t ing ( ) t hr ow s UnratedExceptio n {
i f ( ha sR a t ing ( ) ) {
r e tu rn r ati n g / n u mb er Of R atings;
} e l se {
t h ro w n e w U n r ate d E xc ep ti o n(name);
}
}

pub li c v oi d set R a tin g ( in t ne w Rating) {


r at i ng = n ewR a t ing ;
n um b er Of R a tin g s = 1 ;
}
//. ..
pub li c i nt g etR a w Rat i n g( ) {
r et u rn h a s Rat i n g() ? ( ra ti n g / numberOfRati ngs) : -1;
}

pub li c v oi d add R a tin g ( in t aR a ting) {


i f ( ra ti n g == - 1) {
r a ti ng = aR a t ing ;
} e l se {
r a ti ng + = a R a tin g ;
}
n um b er Of R a tin g s ++;
}

The next item to work on is reading and writing. Here we are changing the behavior so we need to go back to
the tests, and add support for the rating count.

Test 86: Writing movies uses for format: <name> | <category> | <total rating>
| <number of ratings >
Here's the changed code of the two affected tests in TestM ovieL istWriting:
p ubli c v oi d t e stW r i tin g O ne Mo vi e () throws Except ion {
Str in g s ta r W ars O u tpu t = "S ta r Wars|Science Fi ction|4|1\n";
/.. .
}
p ubli c v oi d t e stW r i tin g M ul ti pl e Movies() throws Exception {
Str in g e xp e c ted R e sul t = "S ta r Wars|Science Fi ction|4|1\n" +
"T he P r incess Bride|Fan tasy|5|1\n";
//. ..
}

These two tests now fail since M ov ie doesn't write out its rating counter. Here's the new Movie.writeTo()
method that will get these tests to pass:
p ubli c v oi d w r ite T o (Wr i t er d es t ination) throws IOException {
des ti n at io n . wri t e (na m e );
wri te S ep ar a t or( d e sti n a ti on );
des ti n at io n . wri t e (ge t C at eg or y ().toString());
wri te S ep ar a t or( d e sti n a ti on );
des ti n at io n . wri t e (In t e ge r. to S tring(rating));
wri te S ep ar a t or( d e sti n a ti on );
des ti n at io n . wri t e (In t e ge r. to S tring(numberOfRa tings));
des ti n at io n . wri t e ('\ n ' );
}

Test 87: Reading movies uses for format: <name> | <category> | <total
rating> | <number of ratings>
We do the same thing with reading. First the tests, really just the fixture, to force the calculation of an average.
. . thus requiring the reading of the total and count:
p ubli c c la ss T est M o vie L i st Re ad i ngextends TestCa se {
//. ..

pro te c te d v o id s e tUp ( ) t hr ow s Exception {


e mp t yS tr i n g = " ";
o ne M ov ie S t rin g = " S t ar W ar s |Science Fiction |8|2\n";
/ /. . .
}
//. ..

This causes a failure as M ovi e . re ad Fr o m() only reads the total, which it takes as the rating itself. We must
update that method as so:
p ubli c s ta ti c Mov i e re a d Fr om (B u fferedReader rea der) throws IOException {
Str in g o ne L i ne= r e ade r . re ad Li n e();

Str in g o ne L i ne= r e ade r . re ad Li n e();


if (o n eL in e == n u ll) {
r et u rn n u l l;
}
Str in g To ke n i zer t oke n i ze dL in e = new StringTok enizer(oneLine, DELIMITER);
try {
S tr i ng n a m e = t oke n i ze dL in e .nextToken();
C at e go ry c ate g o ry = Ca te go r y.getCategoryNam ed(tokenizedLine.nextToken());
i nt ra ti n g = I n teg e r .p ar se I nt(tokenizedLine .nextToken());
i nt co un t = I n t ege r . pa rs eI n t(tokenizedLine. nextToken());
r et u rn n e w Mo v i e(n a m e, c at e gory, rating, co unt);

} c at c h (N o S uch E l eme n t Ex ce pt i on ex) {


t hr o w ne w IOE x c ept i o n( "B ad l y formatted movi e collection");
}

Notice how we need a new constructor to create the new Movi e. We shouldn't need it elsewhere, so we make
it private:
p riva te Mo vi e ( Str i n g n a m e, C at e gory category, i nt rating, int count) {
thi s( n am e, c ate g o ry, r at in g) ;
num be r Of Ra t i ngs = co u n t;
}

Green again! It feels a little hacky to me, though. We'll have to keep an eye on this. A quick run of all the
(non-Swing) tests shows one failure in T es tMovi eListEditorSaving(). Running Swing tests yields a
similar failure.

Exercises
21.

What is the problem, and how do we fix it?

Now all the tests pass and we can move on.

Amazon

Prev

don't be afraid of buying books

Next

RATING SOURCE
Create a class to represent the source for a rating. Keep in mind that a single source
can have many ratings, but not the reverse.
We have implemented multiple ratings, and the averaging of them. Now we will turn to the task of
attaching a source (e.g., The New York Times) to each rating. What should this source look like? Should
it be a new class? Or can we get away with a simple string? For now, all we know is that we need to
attach to each rating an indication of where it came from. For now, at least, we can get by with a
string.
So, here are our tests:
Test 88. A rating without a specified source should be anonymous.

Test 89. If we add a rating with a source to a movie, we should be able to later retrieve
the source associated with the rating.

And now, down to work.

Test 88: A rating without a specified source should be anonymous


From discussions with the customer, we learn that we need to be able to support anonymous ratings.
That fits nicely with our work so far, and gives us a good place to start:
p ub l ic v oid test A nony m ousR atin g ( ) t h r o w s E x c e p t i o n {
M o vi e sta r Wars = ne w Mov ie(" S t a r W a r s " , C a t e g o r y . S C I F I ) ;
s t ar Wa rs. a ddRa t ing( 5 );
I t er at or r atin g Iter a tor = st a r W a r s . r a t i n g s ( ) ;
a s se rt Tru e ("Th e re s h ould be a r a t i n g . " , r a t i n g I t e r a t o r . h a s N e x t ( ) ) ;
R a ti ng th e Rati n g = ( Rati ng)r a t i n g I t e r a t o r . n e x t ( ) ;
a s se rt Equ a ls(" B ad R a ting val u e . " , 5 , t h e R a t i n g . g e t V a l u e ( ) ) ;
a s se rt Equ a ls(" T he r a ting sho u l d b e a n o n y m o u s . " ,
" A nony m ous" ,
t h eRat i ng.g etSo u r c e ( ) ) ;

a s se rt Fal s e("T h ere s houl d on l y b e o n e r a t i n g . " , r a t i n g I t e r a t o r . h a s N e x t ( ) ) ;


a s se rt Equ a ls(" b ad a v erag e ra t i n g . " , 5 , s t a r W a r s . g e t R a t i n g ( ) ) ;

Two stubs are needed to make this compile. First, a r a t i n g s ( ) method for M o v i e :
p ub l ic I ter a tor r atin g s() {
r e tu rn nu l l;
}

Second, a Rat i ng class:


p ub l ic c las s Rat i ng {
pu b li c int getV a lue( ) {
r e tu rn 0;
}

pu b li c Str i ng g e tSou r ce() {


r e tu rn "" ;
}

The test now fails with a Nul l Poin ter E x c e p t i o n due to the null returned by r a t i n g s ( ) . Now we
need to do something more here. What should it be? Do we feel we know enough to be confident taking
a larger step? Why not? We can always roll back to where we are now if it doesn't work out.
We know that we want to accumulate a list of ratings, so we can add that to a d d R a t i n g ( ) .We'll start
by adding an instance variable to hold the list:
p ri v at e Lis t rat i ngs = nul l;

Next, we need to initialize r a ting s in the constructors:


p ub l ic M ovi e (Mov i e or i gina l) {
// . ..
ra t in gs = n ew A r rayL i st(o rigi n a l . r a t i n g s ) ;
}
p ub l ic M ovi e (Str i ng a N ame, Cat e g o r y a C a t e g o r y , i n t a R a t i n g ) {
c he ck Nul l (aNa m e);
c he ck Emp t y(aN a me);
n am e = a N ame;
c at eg ory = (a C ateg o ry ! = nu l l ) ? a C a t e g o r y : C a t e g o r y . U N C A T E G O R I Z E D ;
r at in gs = new Arra y List ();

i f (a Rat i ng > -1) {


r at ing = aR a ting ;
n um ber O fRat i ngs = 1;
r at ing s .add ( new R atin g(aR a t i n g ) ) ;
}

To support this, we need to flesh out R a t i n g a bit:


p ub l ic c las s Rat i ng {
pr i va te in t val u e;
pr i va te St r ing s ourc e ;

pu b li c Rat i ng(i n t aR a ting ) {


v a lu e = a R atin g ;
s o ur ce = " Anon y mous " ;
}
pu b li c int getV a lue( ) {
r e tu rn va l ue;
}
pu b li c Str i ng g e tSou r ce() {
r e tu rn so u rce;
}

Next, we need to extend ad d Rati ng() :


p ub l ic v oid addR a ting ( int aRat i n g ) {
if (r at ing == - 1 ) {
r at in g = aRat i ng;
} e ls e {
r at in g + = aRa t ing;
}
nu m be rO fRa t ings + +;
ra t in gs .ad d (new Rati n g(aR atin g ) ) ;
}

Now we can implement Mo v ie.r ating s ( ) :


p ub l ic I ter a tor r atin g s() {
re t ur n rat i ngs. i tera t or() ;
}

Green. Have we introduced any duplication? Yeshave a look at these two lines in a d d R a t i n g ( ) :
n um b er Of Rat i ngs+ + ;
r at i ng s. add ( new R atin g (aRa ting ) ) ;

Furthermore, notice in the constructors for M o v i e that either n u m b e r O f R a t i n g s = = 0 and r a t i n g s


is empty, or nu m berO f Rati n gs = = 1 and r a t i n g s contains one R a t i n g . So the value of
n um b er Of Rat i ngs is always the same as the size of r a t i n g s . We can replace all queries as to the
value of n umb e rOfR a ting s with ra ti n g s . s i z e ( ) .
In doing this, we find ourselves looking at s e t R a t i n g ( ) in M o v i e :
p ub l ic v oid setR a ting ( int newR a t i n g ) {
ra t in g = n e wRat i ng;
nu m be rO fRa t ings = 1;
}

A quick senders check tells us that this is only used in u p d a t e M o v i e ( ) , which in turn is only called
from the interface code. We have two choices here:
1. we can update s etRa t ing( ) to handle the r a t i n g s list, or
2. we can ignore it for now, and deal with it when we get to the interface tasks.
We'll opt for the latter. The crux of the matter is that s e t R a t i n g ( ) is a leftover from the single-rating
system. We are in the process of changing that to a multiple-rating system so, in all likelihood,
s et R at in g() will become obsolete anyway. Since it has no impact on what we are currently focused
on, we can ignore it for now. It would be a good idea, however, to add a short comment to the method
to note what we decided about it. Something similar to this will suffice:
/ / n ee ds co n side r atio n to move t o m u l t i - r a t i n g w h e n w e w o r k o n t h e i n t e r f a c e

Another potential problem is the constructor:


p ri v at e Mov i e(St r ing n ame, Cat e g o r y c a t e g o r y , i n t r a t i n g , i n t c o u n t ) {
t h is (n ame , cat e gory , rat ing) ;
n u mb er OfR a ting s = c o unt;
}

This constructor is only used by rea dFr o m ( ) , so it would appear that a similar argument applies: it
doesn't affect what we are doing at the moment, so we'll defer worrying about it until we renovate the
persistence code. Let's see what happens when we run A l l T e s t s . The reading test fails. That means
we need to fix that constructor now. We'll have to add multiple copies of the given rating. Note that this
isn't strictly correct, since the total of the list of ratings does equal the stored total. Oh well. It passes all
the current tests, and it's a side issue at the moment. It'll do. YAGNI.
p ri v at e Mov i e(St r ing n ame, Cat e g o r y c a t e g o r y , i n t r a t i n g , i n t c o u n t ) {
t h is (n ame , cat e gory , rat ing) ;
n u mb er OfR a ting s = c o unt;
r a ti ng s = new A rray L ist( );
f o r (i nt i = 0 ; i < coun t; i + + ) {
ra ti ngs . add( n ew R a ting (rat i n g ) ) ;
}
}

OK, now let's finish the job and remove n u m b e r O f R a t i n g s (after confirming that the tests all pass).
Green. One more thing we notice during this exercise: the method h a s R a t i n g ( ) is now:
p ub l ic b ool e an h a sRat i ng() {
re t ur n rat i ngs. s ize( ) > 0 ;
}

We can rewrite that in a way that communicates the intent much better:
p ub l ic b ool e an h a sRat i ng() {
re t ur n !ra t ings . isEm p ty() ;
}

Also, we can simplify a d dRat i ng() to:


p ub l ic v oid addR a ting ( int aRat i n g ) {
ra t in g += a Rati n g;
ra t in gs .ad d (new Rati n g(aR atin g ) ) ;
}

Everything still passes. Now we need a test for a rating with an identified source.

Test 89: If we add a rating with a source to a movie, we should be able


to later retrieve the source associated with the rating
p ub l ic v oid test R atin g With Sour c e ( ) {
st a rW ar s.a d dRat i ng(5 , "NY Tim e s " ) ;
It e ra to r r a ting I tera t or = star W a r s . r a t i n g s ( ) ;
Ra t in g the R atin g = ( R atin g)ra t i n g I t e r a t o r . n e x t ( ) ;
as s er tE qua l s("B a d Ra t ing valu e . " , 5 , t h e R a t i n g . g e t V a l u e ( ) ) ;
as s er tE qua l s("T h e ra t ing shou l d b e t h e N Y T i m e s . " ,
"N Y Tim e s",
th e Rati n g.ge tSou r c e ( ) ) ;

th e Rati n g.ge tSou r c e ( ) ) ;

We need a new ad dRa t ing( ) method in M o v i e to add a rating with a known source. We start with a
simple stub that does nothing except give us the desired red bar. Now we can add code to it:
p ub l ic v oid addR a ting ( int aRat i n g , S t r i n g a R a t i n g S o u r c e ) {
ra t in g += a Rati n g;
ra t in gs .ad d (new Rati n g(aR atin g , a R a t i n g S o u r c e ) ) ;
}

This, in turn, requires a new Rat ing constructor:


p ub l ic R ati n g(in t aRa t ing, Str i n g a R a t i n g S o u r c e ) {
va l ue = aR a ting ;
so u rc e = a R atin g Sour c e;
}

OK, green!
Now we can eliminate some duplication we've added. First, look back at the two a d d R a t i n g ( )
methods:
p ub l ic v oid addR a ting ( int aRat i n g ) {
ra t in g += a Rati n g;
ra t in gs .ad d (new Rati n g(aR atin g ) ) ;
}
p ub l ic v oid addR a ting ( int aRat i n g , S t r i n g a R a t i n g S o u r c e ) {
ra t in g += a Rati n g;
ra t in gs .ad d (new Rati n g(aR atin g , a R a t i n g S o u r c e ) ) ;
}

These are much the same, the only difference being the construction of the R a t i n g . If we extract the
common parts to a new method, we end up with:
p ri v at e voi d pri m AddR a ting (Rat i n g r a t i n g T o A d d ) {
r a ti ng += rati n gToA d d.ge tVal u e ( ) ;
r a ti ng s.a d d(ra t ingT o Add) ;
}
p ub l ic v oid addR a ting ( int aRat i n g ) {
p r im Ad dRa t ing( n ew R a ting (aRa t i n g ) ) ;
}
p ub l ic v oid addR a ting ( int aRat i n g , S t r i n g a R a t i n g S o u r c e ) {
p r im Ad dRa t ing( n ew R a ting (aRa t i n g , a R a t i n g S o u r c e ) ) ;
}

OK, let's look at the two constructors for R a t i n g :


p ub l ic R ati n g(in t aRa t ing) {
va l ue = aR a ting ;
so u rc e = " A nony m ous" ;

}
p ub l ic R ati n g(in t aRa t ing, Str i n g a R a t i n g S o u r c e ) {
va l ue = aR a ting ;
so u rc e = a R atin g Sour c e;
}

We can rewrite the first one to chain to the more general one, like this:
p ub l ic R ati n g(in t aRa t ing) {
th i s( aR ati n g, " A nony m ous" );
}

We still have duplication lurking in the total rating that is being maintained. We need to remove it. In
most cases, this is simply a matter of removing the reference to r a t i n g , since it is used in parallel with
the r at in gs collection. In the cases where it is used in a calculation, we need to do a bit more work.
The obvious case is g e tRat i ng() . This becomes:
p ub l ic i nt g etRa t ing( ) thr ows U n r a t e d E x c e p t i o n {
if (h as Rat i ng() ) {
r et ur n c a lcul a teAv e rage Rati n g ( ) ;
} e ls e {
t hr ow ne w Unr a tedE x cept ion( n a m e ) ;
}
}

p ub l ic i nt g etRa w Rati n g() {


if (h as Rat i ng() ) {
re tu rn c alcu l ateA v erag eRat i n g ( ) ;
} e ls e {
r et ur n - 1 ;
}
}
p ri v at e int calc u late A vera geRa t i n g ( ) {
in t a cc = 0 ;
It e ra to r r a ting I tera t or = rat i n g s . i t e r a t o r ( ) ;
wh i le ( rat i ngIt e rato r .has Next ( ) ) {
R at in g a R atin g = ( R atin g) r a t i n g I t e r a t o r . n e x t ( ) ;
a cc + = a R atin g .get V alue ();
}
re t ur n acc / ra t ings . size ();
}

The s et Rat i ng() method needs some attention as well, but only a bit. The significant thing is that to
maintain the current behavior we need a single rating after calling it. We can do this by throwing out
the current set of ratings (recall that this method is a leftover from the single-rating system and will be
gone once the GUI is updated to handle multiple ratings):
p ub l ic v oid setR a ting ( int newR a t i n g ) {
ra t in gs = n ew A r rayL i st() ;
ad d Ra ti ng( n ewRa t ing) ;
}

The final issue is persistence. We had hoped this could be ignored until we upgraded it to handle
multiple ratings, but we want to clean up the code now. We could just go ahead and leave the
persistence-related tests failing until we revamp the whole area. That could work, but it's not a good
idea to have failing tests lying around. Recall that one of the guidelines is to only ever have one failing
test. . . the one you are actively working with. So w r i t e T o ( ) becomes:

p ub l ic v oid writ e To(W r iter des t i n a t i o n ) t h r o w s I O E x c e p t i o n {


de s ti na tio n .wri t e(na m e);
wr i te Se par a tor( d esti n atio n);
de s ti na tio n .wri t e(ge t Cate gory ( ) . t o S t r i n g ( ) ) ;
wr i te Se par a tor( d esti n atio n);
in t r at ing T oWri t e = 0 ;
if (h as Rat i ng() ) {
r at in gTo W rite = ca l cula teTo t a l R a t i n g ( ) ;
} e ls e {
r at in gTo W rite = 0;
}
de s ti na tio n .wri t e(In t eger .toS t r i n g ( r a t i n g T o W r i t e ) ) ;

wr i te Se par a tor( d esti n atio n);


de s ti na tio n .wri t e(In t eger .toS t r i n g ( r a t i n g s . s i z e ( ) ) ) ;
de s ti na tio n .wri t e('\ n ');

To get the required c a lcul a teTo talR a t i n g ( ) method we can extract it from
c al c ul at eAv e rage R atin g (), as so:
p ri v at e int calc u late A vera geRa t i n g ( ) {
i n t to tal R atin g = c a lcul ateT o t a l R a t i n g ( ) ;
r e tu rn to t alRa t ing / rat ings . s i z e ( ) ;
}
p ri v at e int calc u late T otal Rati n g ( ) {
i n t ac c = 0 ;
I t er at or r atin g Iter a tor =rat i n g s . i t e r a t o r ( ) ;

w h il e (ra t ingI t erat o r.ha sNex t ( ) ) {


R at in g a R atin g = ( R atin g) r a t i n g I t e r a t o r . n e x t ( ) ;
a cc + = a R atin g .get V alue ();
}
r e tu rn ac c ;

Now we have only one reading-related test failing: t e s t R e a d i n g O n e M o v i e . The problem is related to
the constructor used:
p ri v at e Mov i e(St r ing n ame, Cat e g o r y c a t e g o r y , i n t r a t i n g , i n t c o u n t ) {
t h is (n ame , cat e gory , rat ing) ;
r a ti ng s = new A rray L ist( );

f o r (i nt i = 0 ; i < coun t; i + + ) {
ra ti ngs . add( n ew R a ting (rat i n g ) ) ;
}

We faked it before, now we need to do a better job of faking it. Let's think. We are given the total
desired rating value, and the number of ratings that have to add up to that total. What if we did:
p ri v at e Mov i e(St r ing n ame, Cat e g o r y c a t e g o r y , i n t r a t i n g , i n t c o u n t ) {
t h is (n ame , cat e gory ) ;
r a ti ng s = new A rray L ist( );
i f ( co unt > 0) {
in t sin g leRa t ing = rat ing / c o u n t ;
in t las t Rati n g = r atin g - ( s i n g l e R a t i n g * ( c o u n t - 1 ) ) ;

fo r (in t i = 0; i < co unt - 1 ; i + + ) {


ad dRa t ing( s ingl e Rati ng);
}
ad dR ati n g(la s tRat i ng);

Try it, compile, run the tests. Green. All right!


We just need to finish cleaning out the r a t i n g instance variable, and we're done. The core code is
completely converted to using multiple ratings.

Amazon

Prev

don't be afraid of buying books

Next

REVISED PERSISTENCE
Revamp the persistence capabilities to handle the changes to the rating structure.
The core of the system is using multiple ratings throughout. Now it's time to upgrade the persistence
capabilities. Until now, a flat text file has sufficed to store our data. Now that each M o v i e has an
arbitrarily sized collection of Rati ngs, we need something more structured. We could develop our own
file format to handle this, but it's easier and more standard to use XML. We could have used XML from
the beginning almost as easily, but it would have been overkill if we didn't have a real reason for it. . .
which we didn't.
We'll adopt the following XML format:
< mo v ie li st>
< m ov ie na m e = " name " cat egor y = " c a t e g o r y " >
<r at ing s >
< rat i ng v a lue= " valu e" s o u r c e = " s o u r c e " / >
. ..
</ ra tin g s>
< / mo vi e>
...
< /m o vi el ist >

Test 90. Writing an empty list outputs an empty <movielist> pair.

Test 91. Writing a list containing one movie outputs in the adopted XML format.

Test 92. Writing a list containing one movie with multiple ratings outputs in the adopted
XML format.

Test 93. Writing a list containing multiple movies outputs in the adopted XML format.

Test 94. Reading an empty <movielist> pair results in an empty list.

Test 95. Reading an appropriate XML stream containing a single movie definition results
in a list containing the single movie defined in the stream.

Test 96. Reading an appropriate XML stream containing more than one movie definition
results in a list containing the movies defined in the stream.

Since we are moving to a more structured storage solution, and it requires more overhead, we'll move
the reading and writing (i.e., the persistence) to a class specializing in that functionality.

Writing the new format


We'll start by tossing out the existing version of T e s t M o v i e L i s t W r i t i n g and writing a new test for
the new storage format. Since we're using XML, let's use the xmlUnit that Tim Bacon told us about in
Chapter 5. Here's the new Te s tCas e. Notice that we've started out with a small fixture. Based on
previous experience, we know we'll need it, and XML Unit does need some setup:
p ub l ic c las s Tes t Movi e List Writ i n g e x t e n d s X M L T e s t C a s e {
pr i va te St r ingW r iter dest inat i o n = n u l l ;
pr i va te Mo v ieLi s tWri t er w rite r = n u l l ;
pr i va te Mo v ieLi s t mo v ieLi st = n u l l ;
pu b li c Tes t Movi e List W riti ng(S t r i n g n a m e ) {
su pe r(n a me);
}

pr o te ct ed v oid s etUp ( ) {
S t ri ng pa r ser = "org . apac he.x e r c e s . j a x p . D o c u m e n t B u i l d e r F a c t o r y I m p l " ;
X M LU ni t.s e tCon t rolP a rser (par s e r ) ;
X M LU ni t.s e tTes t Pars e r(pa rser ) ;
d e st in ati o n = n ew S t ring Writ e r ( ) ;
w r it er = n ew X M LMov i eLis tWri t e r ( d e s t i n a t i o n ) ;
m o vi eL ist = ne w Mov i eLis t();
}

Test 90: Writing an empty list outputs an empty <movielist> pair


And here's the new version of the test for writing an empty M o v i e L i s t :
p ub l ic v oid test W riti n gEmp tyLi s t ( ) t h r o w s E x c e p t i o n {
wr i te r. wri t e(mo v ieLi s t);
as s er tX MLE q ual( " Writ i ng a n em p t y l i s t s h o u l d p r o d u c e e m p t y < m o v i e l i s t > . " ,
"< m ovie l ist> </mo v i e l i s t > " ,
de s tina t ion. toSt r i n g ( ) ) ;
}

This one simple test, with its fixture, lays out the design of the saving system, as shown in Figure 18.1.
This corresponds to the following stubs:
p ub l ic i nte r face Movi e List Writ e r {
vo i d wr ite ( Movi e List movi eLis t ) ;
}
p ub l ic c las s XML M ovie L istW rite r i m p l e m e n t s M o v i e L i s t W r i t e r {
pu b li c XML M ovie L istW r iter (Wri t e r d e s t i n a t i o n ) {
}

pu b li c voi d wri t e(Mo v ieLi st m o v i e L i s t ) {


}

Figure 18.1. The design of the Saving system.

With these stubs in place, we can compile and run the test, which fails with an XML validation error:
o rg . xm l. sax . SAXP a rseE x cept ion:
T h e ro ot e leme n t is requ ired i n a w e l l - f o r m e d d o c u m e n t .

There is no root element, since we are not outputting XML... in fact, we're not outputting anything yet.
Let's start by faking it (note that we need to add the throws clause to M o v i e L i s t W r i t e r . w r i t e ( ) as
well):
p ub l ic v oid writ e (Mov i eLis t mo v i e L i s t ) t h r o w s I O E x c e p t i o n {
de s ti na tio n .wri t e("< m ovie list > < / m o v i e l i s t > " ) ;
de s ti na tio n .flu s h();
}

That works fine. Now to drive moving from faking to making we need another test.

Test 91: Writing a list containing one movie outputs in the adopted XML
format
p ub l ic v oid test W riti n gOne Movi e ( ) t h r o w s E x c e p t i o n {
St r in g sta r Wars O utpu t = " <mov i e l i s t > " +
"< movi e n a m e = \ " S t a r W a r s \ " " +
"category = \"Science Fiction\">" +
"<ra t i n g s > " +
"<rating value=\"4\" " +
"source=\"NY Times\" />" +
"</r a t i n g s > " +
"< /mov i e > " +
" </mo viel i s t > " ;
Mo v ie s tar W ars = new Movi e("S t a r W a r s " , C a t e g o r y . S C I F I ) ;
st a rW ar s.a d dRat i ng(4 , "NY Tim e s " ) ;
mo v ie Li st. a dd(s t arWa r s);
wr i te r. wri t e(mo v ieLi s t);

as s er tX MLE q ual( " Wron g out put f r o m w r i t i n g a s i n g l e m o v i e l i s t . " ,


st a rWar s Outp ut,
de s tina t ion. toSt r i n g ( ) ) ;

When we run this, the call to as sertX M L E q u a l fails. We need to move from faking it to making it... at
least a bit. To pass this test we need:
p ub l ic v oid writ e (Mov i eLis t mo v i e L i s t ) t h r o w s I O E x c e p t i o n {
de s ti na tio n .wri t e("< m ovie list > " ) ;
if (m ov ieL i st.s i ze() > 0) {
M ov ie mo v ieTo W rite = (M ovie ) m o v i e L i s t . g e t M o v i e ( 0 ) ;
d es ti nat i on.w r ite( " <mov ie n a m e = \ " " ) ;
d es ti nat i on.w r ite( m ovie ToWr i t e . g e t N a m e ( ) ) ;
d es ti nat i on.w r ite( " \" c ateg o r y = \ " " ) ;
d es ti nat i on.w r ite( m ovie ToWr i t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
d es ti nat i on.w r ite( " \">" );
d es ti nat i on.w r ite( " <rat ings > " ) ;
I te ra tor rati n gIte r ator = m o v i e T o W r i t e . r a t i n g s ( ) ;
i f (r ati n gIte r ator . hasN ext( ) ) {
R at ing rati n gToW r ite = (R a t i n g ) r a t i n g I t e r a t o r . n e x t ( ) ;
d es tin a tion . writ e ("<r atin g v a l u e = \ " " ) ;
d es tin a tion . writ e (Int eger . t o S t r i n g ( r a t i n g T o W r i t e . g e t V a l u e ( ) ) ) ;
d es tin a tion . writ e ("\" sou r c e = \ " " ) ;
d es tin a tion . writ e (rat ingT o W r i t e . g e t S o u r c e ( ) ) ;
d es tin a tion . writ e ("\" />" ) ;
}

d es ti nat i on.w r ite( " </ra ting s > " ) ;


d es ti nat i on.w r ite( " </mo vie> " ) ;

de s ti na tio n .wri t e("< / movi elis t > " ) ;


de s ti na tio n .flu s h();

Well... it passes the test. It is pretty smelly, though. Let's start by extracting some methods. First, we'll
pull out the body of the inner conditional that outputs a rating:
p ub l ic v oid writ e (Mov i eLis t mo v i e L i s t ) t h r o w s I O E x c e p t i o n {
de s ti na tio n .wri t e("< m ovie list > " ) ;
if (m ov ieL i st.s i ze() > 0) {
M ov ie mo v ieTo W rite = (M ovie ) m o v i e L i s t . g e t M o v i e ( 0 ) ;
d es ti nat i on.w r ite( " <mov ie n a m e = \ " " ) ;
d es ti nat i on.w r ite( m ovie ToWr i t e . g e t N a m e ( ) ) ;
d es ti nat i on.w r ite( " \" c ateg o r y = \ " " ) ;
d es ti nat i on.w r ite( m ovie ToWr i t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
d es ti nat i on.w r ite( " \">" );
d es ti nat i on.w r ite( " <rat ings > " ) ;

I te ra tor rati n gIte r ator = m o v i e T o W r i t e . r a t i n g s ( ) ;


i f (r ati n gIte r ator . hasN ext( ) ) {

w ri teR a ting ( (Rat i ng)r atin g I t e r a t o r . n e x t ( ) ) ;

d es ti nat i on.w r ite( " </ra ting s > " ) ;


d es ti nat i on.w r ite( " </mo vie> " ) ;

de s ti na tio n .wri t e("< / movi elis t > " ) ;


de s ti na tio n .flu s h();

p ri v at e voi d wri t eRat i ng(R atin g r a t i n g T o W r i t e ) t h r o w s I O E x c e p t i o n {


de s ti na tio n .wri t e("< r atin g va l u e = \ " " ) ;
de s ti na tio n .wri t e(In t eger .toS t r i n g ( r a t i n g T o W r i t e . g e t V a l u e ( ) ) ) ;
de s ti na tio n .wri t e("\ " sou rce= \ " " ) ;
de s ti na tio n .wri t e(ra t ingT oWri t e . g e t S o u r c e ( ) ) ;
de s ti na tio n .wri t e("\ " />" );
}

Next, we can extract the body of the outer conditional:


p ub l ic v oid writ e (Mov i eLis t mo v i e L i s t ) t h r o w s I O E x c e p t i o n {
de s ti na tio n .wri t e("< m ovie list > " ) ;
if (m ov ieL i st.s i ze() > 0) {
M ov ie mo v ieTo W rite = (M ovie ) m o v i e L i s t . g e t M o v i e ( 0 ) ;
w ri te Mov i e(mo v ieTo W rite );
}

de s ti na tio n .wri t e("< / movi elis t > " ) ;


de s ti na tio n .flu s h();

p ri v at e voi d wri t eMov i e(Mo vie m o v i e T o W r i t e ) t h r o w s I O E x c e p t i o n {


de s ti na tio n .wri t e("< m ovie nam e = \ " " ) ;
de s ti na tio n .wri t e(mo v ieTo Writ e . g e t N a m e ( ) ) ;
de s ti na tio n .wri t e("\ " cat egor y = \ " " ) ;
de s ti na tio n .wri t e(mo v ieTo Writ e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) ) ;
de s ti na tio n .wri t e("\ " >");
de s ti na tio n .wri t e("< r atin gs>" ) ;
It e ra to r r a ting I tera t or = mov i e T o W r i t e . r a t i n g s ( ) ;
if (r at ing I tera t or.h a sNex t()) {
w ri te Rat i ng(( R atin g )rat ingI t e r a t o r . n e x t ( ) ) ;
}

de s ti na tio n .wri t e("< / rati ngs> " ) ;


de s ti na tio n .wri t e("< / movi e>") ;

This is much cleaner and will be easier to work with as we add more tests to drive the development.
Here's the next test, one for multiple ratings.

Test 92: Writing a list containing one movie with multiple ratings
outputs in the adopted XML format
pu b li c voi d tes t Writ i ngOn eMov i e W i t h M u l t i p l e r a t i n g s ( ) t h r o w s E x c e p t i o n {
St r in g sta r Wars O utpu t = " <mov i e l i s t > " +
" <mov i e n a m e = \ " S t a r W a r s \ " " +
"category = \"Science Fiction\">" +
"< r a t i n g s > " +
"<rating value=\"4\" " +
"source=\"NY Times\" />" +
"<rating value=\"5\" " +
"source=\"Jason\" />" +
"< / r a t i n g s > " +

" </mo v i e > " +


"</m ovie l i s t > " ;
Mo v ie s tar W ars = new Movi e("S t a r W a r s " , C a t e g o r y . S C I F I ) ;
st a rW ar s.a d dRat i ng(4 , "NY Tim e s " ) ;
st a rW ar s.a d dRat i ng(5 , "Ja son" ) ;
mo v ie Li st. a dd(s t arWa r s);
wr i te r. wri t e(mo v ieLi s t);

as s er tX MLE q ual( " Wron g out put f r o m w r i t i n g a s i n g l e m o v i e l i s t . " ,


st a rWar s Outp ut,
de s tina t ion. toSt r i n g ( ) ) ;

This fails because we're only outputting the first rating. We need to generalize this. It's simply a matter
of changing the if to whi l e:
p ri v at e voi d wri t eMov i e(Mo vie m o v i e T o W r i t e ) t h r o w s I O E x c e p t i o n {
/ / ..
I t er at or r atin g Iter a tor = mo v i e T o W r i t e . r a t i n g s ( ) ;
w h il e (ra t ingI t erat o r.ha sNex t ( ) ) {
w ri te Rat i ng(( R atin g )rat ingI t e r a t o r . n e x t ( ) ) ;
}
/ / ..
}

The next test will use multiple movies.

Test 93: Writing a list containing multiple movies outputs in the


adopted XML format
p ub l ic v oid test W riti n gMul tipl e M o v i e s ( ) t h r o w s E x c e p t i o n {
St r in g exp e cted R esul t = " <mov i e l i s t > " +
"<mo v i e n a m e = \ " S t a r W a r s \ " " +
"category = \"Science Fiction\">" +
"<ratings>" +
"<rating value=\"4\" " +
"source=\"NY Times\" />" +

"<rating value=\"5\" " +


"source=\"Jason\" />" +
"</ratings>" +
"</m o v i e > " +
"<mo v i e n a m e = \ " T h e P r i n c e s s B r i d e \ " " +
"category = \"Fantasy\">" +
"<ratings>" +
"<rating value=\"5\" " +
"source=\"Kent\" />" +
"<rating value=\"5\" " +
"source=\"Ron\" />" +
"</ratings>" +
"</m o v i e > " +
"</ movi e l i s t > " ;

Mo v ie s tar W ars = new Movi e("S t a r W a r s " , C a t e g o r y . S C I F I ) ;


st a rW ar s.a d dRat i ng(4 , "NY Tim e s " ) ;
st a rW ar s.a d dRat i ng(5 , "Ja son" ) ;
Mo v ie p rin c essB r ide = new Mov i e ( " T h e P r i n c e s s B r i d e " , C a t e g o r y . F A N T A S Y ) ;
pr i nc es sBr i de.a d dRat i ng(5 , "K e n t " ) ;
pr i nc es sBr i de.a d dRat i ng(5 , "R o n " ) ;
mo v ie Li st. a dd(s t arWa r s);
mo v ie Li st. a dd(p r ince s sBri de);
wr i te r. wri t e(mo v ieLi s t);

as s er tX MLE q ual( " Wron g out put f r o m w r i t i n g a m u l t i p l e m o v i e l i s t . " ,


ex p ecte d Resu lt,
de s tina t ion. toSt r i n g ( ) ) ;

Similar to the previous test, this fails because only the first movie is getting output. Again, we must
generalize, this time changing the other conditional to a loop:
p ub l ic v oid writ e (Mov i eLis t mo v i e L i s t ) t h r o w s I O E x c e p t i o n {
// . ..
It e ra to r m o vieI t erat o r = movi e L i s t . i t e r a t o r ( ) ;
wh i le ( mov i eIte r ator . hasN ext( ) ) {
w r it eM ovi e ((Mo v ie)m o vieI tera t o r . n e x t ( ) ) ;
}
// . ..
}

This code is pretty ugly and the intent is lost in the multitude of d e s t i n a t i o n . w r i t e ( ) calls. We can
use j a va .t ext .M ess a geFo r mat to clean it up some, and clarify the intent:
p ri v at e Mes s ageF o rmat rati ngFo r m a t =
new Mess a geFo r mat( "<ra t i n g v a l u e = \ " { 0 , n u m b e r , i n t e g e r } \ " " +
"s ourc e = \ " { 1 } \ " / > " ) ;
p ri v at e Mes s ageF o rmat movi eFor m a t =
new Mess a geFo r mat( "<mo v i e n a m e = \ " { 0 } \ " c a t e g o r y = \ " { 1 } \ " > " ) ;

pr i va te vo i d wr i teMo v ie(M ovie m o v i e T o W r i t e ) t h r o w s I O E x c e p t i o n {


O bj ec t[ ] arg s = { movi eToW r i t e . g e t N a m e ( ) ,
movi eToW r i t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) } ;
d es ti nat i on.w r ite( m ovie Form a t . f o r m a t ( a r g s ) ) ;
I te ra tor rati n gIte r ator = m o v i e T o W r i t e . r a t i n g s ( ) ;
d es ti nat i on.w r ite( " <rat ings > " ) ;
w hi le (r a ting I tera t or.h asNe x t ( ) ) {
wr it eRa t ing( ( Rati n g)ra ting I t e r a t o r . n e x t ( ) ) ;
}

d es ti nat i on.w r ite( " </ra ting s > " ) ;


d es ti nat i on.w r ite( " </mo vie> " ) ;

pr i va te vo i d wr i teRa t ing( Rati n g r a t i n g T o W r i t e ) t h r o w s I O E x c e p t i o n {


O bj ec t[ ] arg s = { new Inte g e r ( r a t i n g T o W r i t e . g e t V a l u e ( ) ) ,
rati ngTo W r i t e . g e t S o u r c e ( ) } ;
d es ti nat i on.w r ite( r atin gFor m a t . f o r m a t ( a r g s ) ) ;
}

The w ri teM o vie( ) method is still a bit long, don't you think? Let's extract the code that writes the
ratings:
p ri v at e voi d wri t eMov i e(Mo vie m o v i e T o W r i t e ) t h r o w s I O E x c e p t i o n {
O b je ct [ ] args = { m ovie ToWr i t e . g e t N a m e ( ) ,
m ovie ToWr i t e . g e t C a t e g o r y ( ) . t o S t r i n g ( ) } ;
d e st in ati o n.wr i te(m o vieF orma t . f o r m a t ( a r g s ) ) ;
w r it eR ati n gs(m o vieT o Writ e);
d e st in ati o n.wr i te(" < /mov ie>" ) ;
}
p ri v at e voi d wri t eRat i ngs( Movi e m o v i e T o W r i t e ) t h r o w s I O E x c e p t i o n {
I t er at or r atin g Iter a tor = mo v i e T o W r i t e . r a t i n g s ( ) ;
d e st in ati o n.wr i te(" < rati ngs> " ) ;
w h il e (ra t ingI t erat o r.ha sNex t ( ) ) {
w ri te Rat i ng(( R atin g )rat ingI t e r a t o r . n e x t ( ) ) ;
}
d e st in ati o n.wr i te(" < /rat ings > " ) ;
}

There, much nicer. The next step is to remove all of the previous writing-related code from M o v i e L i s t
and M ov ie.
Finally, we need to fix the writing-related code in M o v i e L i s t E d i t o r to use the new writing code.
We'll start by updating the tests. We need to convert them to expect XML output.

Exercises
22.

Convert Tes t Movi eLis t E d i t o r S a v i n g as required.

23.

Update Mov i eLis tEdit o r . s a v e ( ) to make it work.

Reading the new format


Now we must turn to reading. As before, we'll replace the old version of T e s t M o v i e L i s t - R e a d i n g
with a new, XML-based version. We know we'll need a similar fixture, so we'll convert the existing one
as a starting point:
p ub l ic c las s Tes t Movi e List Read i n g e x t e n d s X M L T e s t C a s e {
pr i va te St r ing e mpty S trin g = n u l l ;
pr i va te St r ing o neMo v ieSt ring = n u l l ;
pr i va te St r ing m ulti M ovie Stri n g = n u l l ;
pr i va te St r ing o neMo v iePr efix = n u l l ;
pu b li c Tes t Movi e List R eadi ng(S t r i n g n a m e ) {
s u pe r( nam e );
}
pr o te ct ed v oid s etUp ( ) th rows E x c e p t i o n {
e m pt yS tri n g = " <mov i elis t></ m o v i e l i s t > " ;
o n eM ov ieP r efix = "< m ovie list > " +
"< m ovie nam e = \ " S t a r W a r s \ " " +
" cate g o r y = \ " S c i e n c e F i c t i o n \ " > " +
"<ra ting s > " +
"< rati n g v a l u e = \ " 3 \ " " +
"source=\"Dave\" />" +
"< rati n g v a l u e = \ " 5 \ " " +
"source=\"Jason\" />" +
"</r atin g s > " +
"< / movi e>";

o n eM ov ieS t ring = one M ovie Pref i x + " < / m o v i e l i s t > " ;


m u lt iM ovi e Stri n g = o neMo vieP r e f i x +
"< m ovie nam e = \ " S t a r T r e k \ " " +
" cate g o r y = \ " S c i e n c e F i c t i o n \ " > " +
" <rat ings > " +
"<r atin g v a l u e = \ " 3 \ " " +
" sour c e = \ " A n o n y m o u s \ " / > " +
" </ra ting s > " +
"</ m ovie >" +
"<m o vie name = \ " S t a r g a t e \ " " +
"c ateg o r y = \ " S c i e n c e F i c t i o n \ " > " +
" < rati ngs> < / r a t i n g s > " +
"</ m ovie >" +
" < /mov i elis t>";

Next, we need a starting point. It makes sense to follow the path we did the first time, so we'll start
with a test for reading an empty list.

Test 94: Reading an empty <movielist> pair results in an empty list


p ub l ic v oid test R eadi n gEmp tyLi s t ( ) t h r o w s E x c e p t i o n {
Re a de r rea d er = new S trin gRea d e r ( e m p t y S t r i n g ) ;
Mo v ie Li st m ovie s = n e w XM LMov i e L i s t R e a d e r ( r e a d e r ) . r e a d ( ) ;
as s er tE qua l s("E m pty l ist shou l d n ' t h a v e e n t r i e s . " , 0 , m o v i e s . s i z e ( ) ) ;
}

We need some stubs. We'll follow the same structure as we did when we designed the writer: a general
interface, and a concrete implemention to handle reading XML:
p ub l ic i nte r face Movi e List Read e r {
Mo v ie Li str e ad() ;
}
p ub l ic c las s XML M ovie L istR eade r i m p l e m e n t s M o v i e L i s t R e a d e r {
pr i va te Re a der s ourc e ;
pu b li c XML M ovie L istR e ader (Rea d e r r e a d e r ) {
s o ur ce = r eade r ;
}

pu b li c Mov i eLis t rea d () {


r e tu rn nu l l;
}

Now we can compile and run the test. It fails with a N u l l P o i n t e r E x c e p t i o n because stubbed r e a d
to return nu l l. Now we can fake it:
p ub l ic M ovi e List read ( ) {
re t ur n new Movi e List ( );
}

The next test requires reading a single M o v i e , driving the code toward something real.

Test 95: Reading an appropriate XML stream containing a single movie


definition results in a list containing the single movie defined in the
stream

p ub l ic v oid test R eadi n gOne Movi e ( ) t h r o w s E x c e p t i o n {


R e ad er re a der = new Stri ngRe a d e r ( o n e M o v i e S t r i n g ) ;
M o vi eL ist movi e s = n ew X MLMo v i e L i s t R e a d e r ( r e a d e r ) . r e a d ( ) ;
a s se rt Equ a ls(" R eadi n g a one m o v i e l i s t s h o u l d r e s u l t i n a o n e i t e m l i s t . " ,
1,
m ovie s .siz e()) ;
M o vi e the M ovie R ead = mov ies. g e t M o v i e ( 0 ) ;
a s se rt Equ a ls(" R ead m ovie has w r o n g n a m e . " ,
" Star Wars ",
t heMo v ieRe ad.g e t N a m e ( ) ) ;

a s se rt Equ a ls(" R ead m ovie has w r o n g c a t e g o r y . " ,


C ateg o ry.S CIFI ,
t heMo v ieRe ad.g e t C a t e g o r y ( ) ) ;
a s se rt Equ a ls(" R ead m ovie has w r o n g r a t i n g . " ,
4,
t heMo v ieRe ad.g e t R a t i n g ( ) ) ;
I t er at or r atin g Iter a tor = th e M o v i e R e a d . r a t i n g s ( ) ;
a s se rt Tru e ("Re a d mo v ie s houl d h a v e r a t i n g s . " ,
ra t ingI t erat or.h a s N e x t ( ) ) ;
R a ti ng fi r stRa t ing = (Ra ting ) r a t i n g I t e r a t o r . n e x t ( ) ;
a s se rt Equ a ls(" F irst rati ng h a s w r o n g v a l u e " ,
3,
f irst R atin g.ge t V a l u e ( ) ) ;
a s se rt Equ a ls(" F irst rati ng h a s b a d s o u r c e " ,
" Dave " ,
f irst R atin g.ge t S o u r c e ( ) ) ;
a s se rt Tru e ("Re a d mo v ie s houl d h a v e t w o r a t i n g s . " ,
ra t ingI t erat or.h a s N e x t ( ) ) ;
R a ti ng se c ondR a ting = (R atin g ) r a t i n g I t e r a t o r . n e x t ( ) ;
a s se rt Equ a ls(" S econ d rat ing h a s w r o n g v a l u e " ,
5,
s econ d Rati ng.g e t V a l u e ( ) ) ;

a s se rt Equ a ls(" S econ d rat ing h a s b a d s o u r c e " ,


" Jaso n ",
s econ d Rati ng.g e t S o u r c e ( ) ) ;
a s se rt Fal s e("R e ad m o vie shou l d h a v e o n l y t w o r a t i n g s . " ,
r a ting I tera tor. h a s N e x t ( ) ) ;

That's a rather long test, but it really just crawls through the structure that was read and verifies that all
is as expected. This, of course, fails since we faked r e a d ( ) , and it simply returns an empty
M ov i eL is t. Let's start by adding the code required to set up and use JDOM to process the XML data:
p ub l ic M ovi e List read ( ) th rows I O E x c e p t i o n {
S A XB ui lde r bui l der = new SAX B u i l d e r ( ) ;
D o cu me nt t heMo v ieli s tDoc umen t ;

try {
th eM ovi e list D ocum e nt = bui l d e r . b u i l d ( s o u r c e ) ;
} ca tc h ( J DOME x cept i on e ) {
th ro w n e w IO E xcep t ion( e.ge t M e s s a g e ( ) ) ;
}
M o vi eL ist theN e wLis t = p roce s s D o c u m e n t ( t h e M o v i e l i s t D o c u m e n t ) ;
r e tu rn th e NewL i st;

p ri v at e Mov i eLis t pro c essD ocum e n t ( D o c u m e n t t h e M o v i e l i s t D o c u m e n t ) {


r e tu rn ne w Mov i eLis t ();
}

This still fails in the same place, because even though we are now processing the input, we still just
return an empty M o vie L ist. This is a good sign, though, because it does mean that our input XML is
valid, and is being processed. Next, we need to process it and build our objects from the resulting
d oc u me nt . Let's do that a small bit at a time, working our way through the tests as we do. First, we
need to get the right number of M ovies:
p ri v at e Mov i eLis t pro c essD ocum e n t ( D o c u m e n t t h e M o v i e l i s t D o c u m e n t )
thr o ws D u plic a teMo vieE x c e p t i o n {
M o vi eL ist newM o vieL i st = new M o v i e L i s t ( ) ;
E l em en t m o viel i stEl e ment = t h e M o v i e l i s t D o c u m e n t . g e t R o o t E l e m e n t ( ) ;
L i st m ovi e s = m ovie l istE leme n t . g e t C h i l d r e n ( ) ;
I t er at or m ovie E leme n tIte rato r = m o v i e s . i t e r a t o r ( ) ;
w h il e (mo v ieEl e ment I tera tor. h a s N e x t ( ) ) {
E le me nt m ovie E leme n t = (Ele m e n t ) m o v i e E l e m e n t I t e r a t o r . n e x t ( ) ;
S tr in g n a me = movi e Elem ent. g e t A t t r i b u t e V a l u e ( " n a m e " ) ;
S tr in g c a tego r yNam e = m ovie E l e m e n t . g e t A t t r i b u t e V a l u e ( " c a t e g o r y " ) ;
C at eg ory cate g ory = Cat egor y . g e t C a t e g o r y N a m e d ( c a t e g o r y N a m e ) ;
M ov ie ne w Movi e = n e w Mo vie( n a m e , c a t e g o r y ) ;
n ew Mo vie L ist. a dd(n e wMov ie);

}
r e tu rn ne w Movi e List ;

Now the test gets to the check of the average rating. This fails due to an U n r a t e d E x c e p t i o n being
thrown. This tells us that one movie is being read and the name and category are correct. Next, we'll
add code to process the ratings:
p ri v at e Mov i eLis t pro c essD ocum e n t ( D o c u m e n t t h e M o v i e l i s t D o c u m e n t )
thr o ws D u plic a teMo vieE x c e p t i o n {
/ / .. .
Mo vi e n e wMov i e = n ew M ovie ( n a m e , c a t e g o r y ) ;
El em ent rati n gsEl e ment = m o v i e E l e m e n t . g e t C h i l d ( " r a t i n g s " ) ;
Li st ra t ings = ra t ings Elem e n t . g e t C h i l d r e n ( ) ;
It er ato r rat i ngIt e rato r =r a t i n g s . i t e r a t o r ( ) ;
wh il e ( r atin g Iter a tor. hasN e x t ( ) ) {
E le men t rat i ngEl e ment = ( E l e m e n t ) r a t i n g I t e r a t o r . n e x t ( ) ;
i nt va l ue;
t ry {
v alu e = r a ting E leme nt.g e t A t t r i b u t e ( " v a l u e " ) . g e t I n t V a l u e ( ) ;
} c atc h (Da t aCon v ersi onEx c e p t i o n e ) {
t hro w new IOEx c epti on(e . g e t M e s s a g e ( ) ) ;
}

S tr ing sour c e = r atin gEle m e n t . g e t A t t r i b u t e V a l u e ( " s o u r c e " ) ;


n ew Mov i e.ad d Rati n g(va lue, s o u r c e ) ;

}
n ew Mo vie L ist. a dd(n e wMov ie);

}
r e tu rn ne w Movi e List ;

Green! Now we need to refactor to clean up the mess. We'll extract the processing of movies and
ratings, resulting in:
p ri v at e Mov i eLis t pro c essD ocum e n t ( D o c u m e n t t h e M o v i e l i s t D o c u m e n t )
thr o ws D u plic a teMo vieE x c e p t i o n {
M o vi eL ist newM o vieL i st = new M o v i e L i s t ( ) ;
E l em en t m o viel i stEl e ment = t h e M o v i e l i s t D o c u m e n t . g e t R o o t E l e m e n t ( ) ;
L i st m ovi e s = m ovie l istE leme n t . g e t C h i l d r e n ( ) ;
I t er at or m ovie E leme n tIte rato r = m o v i e s . i t e r a t o r ( ) ;

w h il e (mo v ieEl e ment I tera tor. h a s N e x t ( ) ) {


E le me nt m ovie E leme n t = (Ele m e n t ) m o v i e E l e m e n t I t e r a t o r . n e x t ( ) ;
n ew Mo vie L ist. a dd(p r oces sMov i e ( m o v i e E l e m e n t ) ) ;
}
r e tu rn ne w Movi e List ;

p ri v at e Mov i e pr o cess M ovie (Ele m e n t m o v i e E l e m e n t ) t h r o w s I O E x c e p t i o n {


S t ri ng na m e = m ovie E leme nt.g e t A t t r i b u t e V a l u e ( " n a m e " ) ;
S t ri ng ca t egor y Name = mo vieE l e m e n t . g e t A t t r i b u t e V a l u e ( " c a t e g o r y " ) ;
C a te go ry c ateg o ry = Cate gory . g e t C a t e g o r y N a m e d ( c a t e g o r y N a m e ) ;
M o vi e new M ovie = ne w Mov ie(n a m e , c a t e g o r y ) ;
E l em en t r a ting s Elem e nt = mov i e E l e m e n t . g e t C h i l d ( " r a t i n g s " ) ;
L i st r ati n gs = rati n gsEl emen t . g e t C h i l d r e n ( ) ;
I t er at or r atin g Iter a tor =rat i n g s . i t e r a t o r ( ) ;

w h il e (ra t ingI t erat o r.ha sNex t ( ) ) {


E le me nt r atin g Elem e nt = (El e m e n t ) r a t i n g I t e r a t o r . n e x t ( ) ;
n ew Mo vie . addR a ting ( proc essR a t i n g ( r a t i n g E l e m e n t ) ) ;
}
r e tu rn ne w Movi e ;

p ri v at e Rat i ng p r oces s Rati ng(E l e m e n t r a t i n g E l e m e n t ) t h r o w s I O E x c e p t i o n {


i n t va lue ;
try {
v al ue = r atin g Elem e nt.g etAt t r i b u t e ( " v a l u e " ) . g e t I n t V a l u e ( ) ;
} ca tc h ( D ataC o nver s ionE xcep t i o n e ) {
t hr ow ne w IOE x cept i on(e .get M e s s a g e ( ) ) ;
}

S t ri ng so u rce = rat i ngEl emen t . g e t A t t r i b u t e V a l u e ( " s o u r c e " ) ;


r e tu rn ne w Rat i ng(v a lue, sou r c e ) ;

Note that we had to rename Mov ie.pr i m A d d R a t i n g ( ) to a d d R a t i n g ( ) and change its visibility to
p ub l ic . You may sometimes (I won't go as far as to say often) find that methods that are the result of

refactoring eventually become useful as part of the public interface of the class.
That takes care of reading a single movie. Next, we need to test the reading of multiple movies.

Test 96: Reading an appropriate XML stream containing more than one
movie definition results in a list containing the movies defined in the
stream
p ub l ic v oid test R eadi n gMul tipl e M o v i e ( ) t h r o w s E x c e p t i o n {
Re a de r rea d er = new S trin gRea d e r ( m u l t i M o v i e S t r i n g ) ;
Mo v ie Li st m ovie s = n e w XM LMov i e L i s t R e a d e r ( r e a d e r ) . r e a d ( ) ;
as s er tE qua l s("R e adin g a m ulti p l e m o v i e l i s t s h o u l d r e s u l t i n 3 i t e m s . " ,
3,
mo v ies. s ize( ));
}

This compiles and passes without a hitch. Notice that since we already have a test that verifies that a
movie can be read properly, all we need to do here is verify that a collection can be read.
Now we can move up a level and upgrade the reading-related M o v i e L i s t E d i t o r tests. Very little
changes in the test, just the creation of the test file in s e t U p ( ) :
n ew XM LM ovi e List W rite r (new Fil e W r i t e r ( i n p u t F i l e ) ) . w r i t e ( m o v i e L i s t ) ;

The test fails with a file format exception, since M o v i e L i s t E d i t o r is using the previous reading code.
We need to change its l oa d() method to use the new code:
p ub l ic b ool e an l o ad()
t hro w s Fi l eNot F ound Exce p t i o n , I O E x c e p t i o n , D u p l i c a t e M o v i e E x c e p t i o n {
Fi l e in put F ile = vie w .get File T o L o a d ( ) ;
if (i np utF i le = = nul l ) {
r et ur n f a lse;
}

Fi l eR ea der read e r = n ew F ileR e a d e r ( i n p u t F i l e ) ;


mo v ie s = n e w XM L Movi e List Read e r ( r e a d e r ) . r e a d ( ) ;
fi l te rO nCa t egor y (Cat e gory .ALL ) ;
re t ur n tru e ;

Theregreen bar. Now run A l lTes ts to be suregreen bar. The last step is to remove the
r ea d Fr om () methods from Movi e and M o v i e L i s t .Run A l l T e s t s again for safety and we're done.
Now a couple of tweaks to the Swing tests. First, T e s t S w i n g M o v i e L i s t E d i t o r F i l e O p e r a t i o n s
needs to extend XMLT e stCa s e. Next, the test data:
p ro t ec te d v o id s e tUp( ) thr ows E x c e p t i o n {
/ / .. .
S t ri ng te x tPre f ix = "<mo viel i s t > " +
"< movi e n a m e = \ " S t a r W a r s \ " " +
"category=\"Science Fiction\">" +
"<r a t i n g s > " +
"<rating value=\"5\" " +
"source=\"Anonymous\" />" +
"</ r a t i n g s > " +
"< /mov i e > " +
"< movi e n a m e = \ " S t a r T r e k \ " " +
"category=\"Science Fiction\">" +
"<r a t i n g s > " +
"<rating value=\"3\" " +
"source=\"Anonymous\" />" +
"</ r a t i n g s > " +
"< /mov i e > " +

"< movi e n a m e = \ " S t a r g a t e \ " " +


"category=\"Science Fiction\">" +
"<ra t i n g s > < / r a t i n g s > " +
"< /mov i e > " ;
s a ve dT ext = text P refi x + "< /mov i e l i s t > " ;

e x te nd edS a vedT e xt = text Pref i x + " < m o v i e n a m e = \ " T h e S h i n i n g \ " " +


"category=\"Horror\">" +
"<ratings>" +
"<rating value=\"2\" " +
"source=\"Anonymous\" />" +
"</ratings>" +
"</movie>" +
"</movielist>";
/ / .. .

The other change is to the comparison code in t e s t S a v i n g ( ) :


p ub l ic v oid test S avin g () t hrow s E x c e p t i o n {
/ / .. .
s a ve Bu tto n .pus h ();
a s se rt XML E qual ( "Sav e -Ase d fi l e h a s w r o n g c o n t e n t s . " ,
s a vedT e xt,
c o nten t sOf( outp u t F i l e ) ) ;

/ / .. .
s a ve It em. p ush( ) ;
a s se rt XML E qual ( "Res a ved file h a s w r o n g c o n t e n t s . " ,
e x tend e dSav edTe x t ,
c o nten t sOf( outp u t F i l e ) ) ;

p ri v at e Str i ng c o nten t sOf( File a F i l e ) t h r o w s I O E x c e p t i o n {


F i le In put S trea m fst r eam = ne w F i l e I n p u t S t r e a m ( a F i l e ) ;
i n t si ze = fst r eam. a vail able ( ) ;
b y te [] bu f fer = new byte [siz e ] ;
f s tr ea m.r e ad(b u ffer ) ;
re t ur n new Stri n g(bu f fer) ;
}

And that's it. Green all the way.

Amazon

Prev

don't be afraid of buying books

Next

SHOW MULTIPLE RATINGS IN THE GUI


In the GUI, show a list of all ratings for the selected movie.
To show all of a movie's ratings we need to add a list to the GUI in which to show them. For now, we'll
leave the existing rating combo in place so that we don't break the add and update tests. We'll take
care of that in a later task.
This task doesn't require any new tests; we need to update to use the new storage format, though.

In the Logical Layer


First, we'll work on the Mo vieL istE dito r . We need to extend the test te s tS el e ct in g () in
Tes t Movie Lis tE dit or :
pub l ic vo id te stS el ecti ng() {
// . ..
mo c kView .se tN ame Fi eld( star Wars . g etN a m e() ) ;
// . ..
mo c kView .se tR ati ng s(st arWa rs.g e t Rat i n gs( ) ) ;
co n trol. set Vo idC al labl e(1) ;
mo c kView .se tN ame Fi eld( star Trek . g etN a m e() ) ;
// . ..
mo c kView .se tR ati ng s(st arTr ek.g e t Rat i n gs( ) ) ;
co n trol. set Vo idC al labl e(1) ;
mo c kView .se tN ame Fi eld( star gate . g etN a m e() ) ;
// . ..
mo c kView .se tR ati ng s(st arga te.g e t Rat i n gs( ) ) ;
co n trol. set Vo idC al labl e(1) ;

mo c kView .se tN ame Fi eld( theS hini n g .ge t N ame ( ) );


// . ..
mo c kView .se tR ati ng s(th eShi ning . g etR a t ing s ( )) ;
co n trol. set Vo idC al labl e(1) ;
// . ..

All we had to add was the expectations for a call to s e t Ra ti ng s () corresponding to each selected
movie. This fails because those calls are not being made. A simple addition to
Mov i eList Edi to r.s el ect( ) does the job (the call to s et R at in g s( ) ):
pub l ic vo id se lec t( int i) {
if (i == -1 ) {
s elect edM ov ie = null ;
} e lse {
s elect edM ov ie = filt ered Movi e s .ge t M ovi e ( i) ;
v iew.s etN am eFi el d(se lect edMo v i e.g e t Nam e ( )) ;
v iew.s etC at ego ry Fiel d(se lect e d Mov i e .ge t C at eg or y () ) ;
v iew.s etR at ing s( sele cted Movi e . get R a tin g s () );

t ry {
view .se tR ati ng Fiel d(se lect e d Mov i e .ge t R at in g( ) + 1) ;
} catc h ( Un rat ed Exce ptio n e) {
view .se tR ati ng Fiel d(0) ;
}

Now t estSe lec ti ng( ) passes, but three other tests fail because they were not expecting the
set R ating s() call. Instead of editing them to expect it, we can have their mock view ignore those
calls. This is reasonable as they aren't performing tests related to the ratings. There are a handful of
tests that need the following lines added at the beginning:
moc k View. set Ra tin gs (nul l);
con t rol.s etD ef aul tV oidC alla ble( ) ;

That's all we need to do here.

In the Swing Layer


The first test we will update is te stSe le ct U p dat e s Rat i ng ( ) in
Tes t Swing Mov ie Lis tE dito rVie w. The new version is as follows (notice the addition of a second
rating to st arW ar s):
pub l ic vo id te stS el ectU pdat esRa t i ng( ) {
{
st a rWars .ad dR ati ng (4, "Dav e");
JL i stOpe rat or mo vi eLis t =
new JL is tOp er ator (mai nWin d o w, n e w N a m eB as ed C ho o se r( " mo vi e Li st " )) ;
JL i stOpe rat or ra ti ngsL ist =
new JL is tOp er ator (mai nWin d o w, n e w N a m eB as ed C ho o se r( " ra ti n gs ") ) ;

mo v ieLis t.c li ckO nI tem( 0, 1 );


ve r ifyRa tin gs (ra ti ngsL ist, sta r W ars ) ;
mo v ieLis t.c li ckO nI tem( 1, 1 );
ve r ifyRa tin gs (ra ti ngsL ist, sta r T rek ) ;
mo v ieLis t.c li ckO nI tem( 2, 1 );
ve r ifyRa tin gs (ra ti ngsL ist, sta r g ate ) ;

pri v ate v oid v eri fy Rati ngs( JLis t O per a t or r a ti ng sL i st , M ov i e aM o vi e) {


L i stMod el li stM od el = rat ings L i st. g e tMo d e l( );
I t erato r r at ing It erat or = aMo v i e.r a t ing s ( );
i n t i = 0;

w h ile ( rat in gIt er ator .has Next ( ) ) {


R ating aR at ing = (Ra ting ) ra t i ngI t e rat o r .n ex t( ) ;
a ssert Equ al s(" Ex pect ed r atin g not d isp l a ye d" ,
a Ra ting ,
l is tMod el.g etEl e m ent A t (i) ) ;
i ++;
}
a s sertE qua ls ("W ro ng n umbe r of r ati n g s d i s pl ay ed " ,i , l is t Mo de l .g et S iz e( ) );

This, of course, fails because we don't have a list of ratings in the GUI yet. That's easy to fix in
Swi n gMovi eLi st Edi to rVie w:
pri v ate J Pan el in it Deta ilPa ne() {
J P anel det ai lPa ne = n ew J Pane l ( );
/ / ...

d e tailP ane .a dd( in itRa ting List ( ) );


d e tailP ane .a dd( Bo x.cr eate Rigi d A rea ( n ew D i me ns io n (0 , 5 )) ) ;
d e tailP ane .a dd( in itCa tego ryFi e l d() ) ;
r e turn det ai lPa ne ;

//. . .
pri v ate J Scr ol lPa ne ini tRat ingL i s t() {
r a tingL ist = ne w JLis t(ne w Ve c t or( ) ) ;
r a tingL ist .s etN am e("r atin gs") ;
r a tingL ist .s etS el ecti onMo de(L i s tSe l e cti o n Mo de l. S IN G LE S E LE CT I ON );

J S croll Pan e scr ol ler =


new J Scr ol lPan e(ra ting L i st,
Sc roll P a neC o n sta n t s. VE RT I CA L S CR O LL BA R A LW A YS ,
Sc roll P a neC o n sta n t s. HO RI Z ON T AL S C RO LL B AR N E VE R) ;
r e turn scr ol ler ;

Now the test fails when checking the ratings since nothing is being done with the new list box. We need
to add some code to s et Rati ngs( ) to take care of that:
pub l ic vo id se tRa ti ngs( Vect or n e w Rat i n gs) {
ra t ingLi st. se tLi st Data (new Rati n g s);
}

That makes it work. For aesthetic reasons, we'd like to use the same format as we did in the movie list,
namely, the rating star icons followed by the text. We'll do something similar to what we did in Chapter
13. We've done this before, so we'll skip the details here, showing only the results. Here's the test case:
pub l ic cl ass T est Ra ting Rend erer e xte n d s T e s tC as e {
p r ivate JL is t l is t;
p r ivate Ra ti ng fi veSt ars;
p r ivate Ra ti ngR en dere r re nder e r ;
p r ivate Ra ti ng th reeS tars ;
p r otect ed vo id se tUp( ) {
fiveS tar s = n ew Rat ing( 5, " D a ve" ) ;
three Sta rs = ne w Ra ting (3, " J aso n " );
rende rer = ne w Rati ngRe nder e r ();
list = n ew JL is t();
list. set Ba ckg ro und( Colo r.BL U E );
list. set Fo reg ro und( Colo r.RE D ) ;
list. set Se lec ti onBa ckgr ound ( C olo r . RED ) ;
list. set Se lec ti onFo regr ound ( C olo r . BLU E ) ;
}
p u blic voi d tes tR etur nsSe lf() {
Compo nen t res ul t re nder er.g e t Lis t C ell R e nd er er C om p on en t (l is t ,

f i ve St a rs ,
1,
f a ls e,
f a ls e) ;
asser tSa me ("g et List Cell Rend e r erC o m pon e n t sh ou l d r et ur n s el f ." ,r e nd er e r,
res ul t);

p u blic voi d tes tC onte nts( ) {


rende rer .g etL is tCel lRen dere r C omp o n ent ( l is t, f i ve S ta rs , 1 , f al se , f al s e) ;
asser tEq ua ls( "B ad t ext. ",fi v e Sta r s .ge t S ou rc e( ) , r en de r er .g e tT ex t () );
asser tSa me ("I co n sh ould be 5 sta r s .",
Ra ti ngRe nder er.i c o nFo r R ati n g (5 ),
re nd erer .get Icon ( ) );
rende rer .g etL is tCel lRen dere r C omp o n ent ( l is t, t h re e St ar s , 1, tr ue , f al s e) ;
asser tEq ua ls( "B ad t ext. ",th r e eSt a r s.g e t So ur ce ( ), re nd e re r. g et Te x t( )) ;
asser tSa me ("I co n sh ould be 3 sta r s .",
Ra ti ngRe nder er.i c o nFo r R ati n g (3 ),
re nd erer .get Icon ( ) );
}
p u blic voi d tes tU nSel ecte dCol o r s() {
rende rer .g etL is tCel lRen dere r C omp o n ent ( l is t, f i ve S ta rs , 1 , f al se , f al s e) ;
asser tEq ua ls( "U nsel ecte d ba c k gro u n d s h o ul d be bl u e. ", C ol or . BL UE ,
re nder er.g etBa c k gro u n d() ) ;
asser tEq ua ls( "U nsel ecte d fo r e gro u n d s h o ul d be re d ." ,C o lo r. R ED ,
re nder er.g etFo r e gro u n d() ) ;
}
p u blic voi d tes tS elec tedC olor s ( ) {
rende rer .g etL is tCel lRen dere r C omp o n ent ( l is t, f i ve S ta rs , 1 , t ru e, fa ls e );
asser tEq ua ls( "S elec ted back g r oun d sho u l d be r e d. " ,C ol o r. RE D ,
r ende rer. getB a c kgr o u nd( ) ) ;
asser tEq ua ls( "S elec etd fore g r oun d sho u l d be b l ue . ", Co l or .B L UE ,
r ende rer. getF o r egr o u nd( ) ) ;
}

p u blic sta ti c v oi d ma in(S trin g [ ] a r gs) {


junit .te xt ui. Te stRu nner .run ( T est R a tin g R en de re r .c l as s) ;
}

And here's the code for the renderer:


pub l ic cl ass R ati ng Rend erer ext e n ds J L abe l im pl em e nt s L is t Ce ll R en de r er {
pr i vate sta ti c I ma geIc on[ ] ra t i ngI c o ns = {
n e w Ima geI co n(" im ages /zer o-st a r s.g i f "),
n e w Ima geI co n(" im ages /one -sta r . gif " ) ,
n e w Ima geI co n(" im ages /two -sta r s .gi f " ),
n e w Ima geI co n(" im ages /thr ee-s t a rs. g i f") ,
n e w Ima geI co n(" im ages /fou r-st a r s.g i f "),
n e w Ima geI co n(" im ages /fiv e-st a r s.g i f ")
};
pu b lic R ati ng Ren de rer( Stri ng t e x t, I c on i c on ,
i n t ho r i zon t a lA li gn m en t ) {
s u per(t ext , ico n, hor izon talA l i gnm e n t);
}
pu b lic R ati ng Ren de rer( Stri ng t e x t, i n t h o r iz on ta l Al i gn me n t) {
s u per(t ext , hor iz onta lAli gnme n t );
}
pu b lic R ati ng Ren de rer( Stri ng t e x t) {
s u per(t ext );
}

pu b lic R ati ng Ren de rer( Icon ima g e , i n t ho r i zo nt al A li g nm en t ) {


s u per(i mag e, ho ri zont alAl ignm e n t);
}
pu b lic R ati ng Ren de rer( Icon ima g e ) {
s u per(i mag e) ;
}
pu b lic R ati ng Ren de rer( ) {
s u per() ;
}
pu b lic s tat ic Im ag eIco n[ ] ico n s () {
r e turn rat in gIc on s;
}
pu b lic s tat ic Im ag eIco n ic onFo r R ati n g (in t ra ti ng ) {
r e turn rat in gIc on s[ra ting ];
}
pu b lic C omp on ent g etLi stCe llRe n d ere r C omp o n en t( JL i st li st ,
Ob j ec t v al u e,
in t i n de x,
bo o le a n is S el ec t ed ,
bo o le a n ce l lH as F oc us ) {
R a ting rat in gTo Re nder = ( Rati n g )va l u e;

i f (isS ele ct ed) {


setBa ckg ro und (l ist. getS elec t i onB a c kgr o u nd () );
setFo reg ro und (l ist. getS elec t i onF o r egr o u nd () );
} else {
setBa ckg ro und (l ist. getB ackg r o und ( ) );
setFo reg ro und (l ist. getF oreg r o und ( ) );
}
s e tText (ra ti ngT oR ende r.ge tSou r c e() ) ;
s e tIcon (ra ti ngI co ns[r atin gToR e n der . g etV a l ue () ]) ;

r e turn thi s;

Next, we can just change the GUI code to use the new custom renderer for the rating list:
pri v ate J Scr ol lPa ne ini tRat ingL i s t() {
/ / ...
r a tingL ist .s etC el lRen dere r(ne w Rat i n gRe n d er er () ) ;
/ / ...
}

Amazon

Prev

don't be afraid of buying books

Next

ADD A RATING IN THE GUI


In the GUI, provide a way to add a rating to the selected movie. Be sure to handle an
empty source and map it to Anonymous.
Now we need to be able to add a rating to the selected movie.
Test 97. Telling the logical layer to select a movie and add a rating to it, and providing
specific rating value and source, causes that rating to be added to the movie that was
selected.

Test 98. Selecting a movie, setting the rating value, and source to specific values and
pressing the "Add Rating" button causes that rating to be added to the selected movie.

Here's the test in Te st Movi eLis tEdit o r for that function.

Test 97: Telling the logical layer to select a movie and add a rating to it,
and providing specific rating value and source, causes that rating to be
added to the movie that was selected
pub l ic vo id te stA dd ingR atin g() t h row s Dup l i ca te Mo v ie E xc ep t io n {
V e ctor ext en ded Ra ting s = star W a rs. g e tRa t i ng s( );
e x tende dRa ti ngs .a dd(n ew R atin g ( 2, " D ave " ) );

m o ckVie w.s et Mov ie s(mo vies );


c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Nam eF ield (sta rWar s . get N a me( ) ) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Rat in gFie ld(6 );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Cat eg oryF ield (Cat e g ory . S CIF I ) ;
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.s et Rat in gs(s tarW ars. g e tRa t i ngs ( ) );
c o ntrol .se tV oid Ca llab le(1 );
m o ckVie w.g et Rat in gVal ueFi eld( ) ;
c o ntrol .se tR etu rn Valu e(2, 1);
m o ckVie w.g et Rat in gSou rceF ield ( ) ;
c o ntrol .se tR etu rn Valu e("D ave" , 1 );
m o ckVie w.s et Rat in gs(e xten dedR a t ing s ) ;
c o ntrol .se tV oid Ca llab le(1 );

c o ntrol .ac ti vat e( );


M o vieLi stE di tor e dito r = new M o vie L i stE d i to r( mo v ie L is t, m oc kV i ew );
e d itor. sel ec t(0 );

e d itor. add Ra tin g( );


c o ntrol .ve ri fy( );

We need to add some methods to the M o vie L i stE d i tor V ie w interface to fetch the values for the new
rating:
int getRa tin gV alu eF ield ();
Str i ng ge tRa ti ngS ou rceF ield ();

We also need a stubbed add Rati ng() in M o v ieL i s tEd i to r :


pub l ic vo id ad dRa ti ng() {
}

OK, this compiles, but fails. The expected calls to the new methods are not being made. Also, the
second call to s etR at ing s() with the new rating is not happening. Here's the failure trace:
jun i t.fra mew or k.A ss erti onFa iled E r ror :
E a syMoc k f or in te rfac e co m.sa o r sa. n o wpl a y in g. Mo v ie L is tE d it or V ie w:
E x pecta tio n fai lu re o n ve rify :
metho d c al l s et Rati ngs( [5 ( A n ony m o us) , 2 (D av e )] ) :
cal ls ex pec te d: 1 ,
rec eiv ed : 0
metho d c al l g et Rati ngSo urce F i eld ( ) : c a l ls e xp e ct e d: 1 , r ec e iv ed : 0
metho d c al l g et Rati ngVa lueF i e ld( ) : ca l l s ex pe c te d : 1, re ce i ve d: 0

Here's the code for add Ra ting ():


pub l ic vo id ad dRa ti ng() {
i f (sel ect ed Mov ie != null ) {
int v alu e = v ie w.ge tRat ingV a l ueF i e ld( ) ;
Strin g s ou rce = vie w.ge tRat i n gSo u r ceF i e ld () ;
selec ted Mo vie .a ddRa ting (val u e , s o u rce ) ;
view. set Ra tin gs (sel ecte dMov i e .ge t R ati n g s( )) ;
}
}

The test still fails. The failure trace is:


jun i t.fra mew or k.A ss erti onFa iled E r ror :
E a syMoc k f or in te rfac e co m.sa o r sa. n o wpl a y in g. Mo v ie L is tE d it or V ie w:
U n expec ted m eth od cal l se tRat i n gs( [ 5 (A n o ny mo us ) , 2 ( Da v e) ])

But this is exactly the ratings that were expected earlier. What's up? R at i ng doesn't have an
equ a ls() method. That's what.
We need to digress and write a test for comparing R a tin g s. Hold on a sec ... won't that leave us with
more than one test failing (the new test for R a tin g . equ a ls ( ) as well as the currently failing test?
Yes and no. Yes, in that we'll have two failures reported. No, in that this test should be passing. It's the
comparion that is failing; all requirements of the test are being satisfied. So let's press on and write the
test for Rat ing .e qua ls ():
pub l ic cl ass T est Ra ting ext ends T est C a se {
p u blic voi d tes tE qual s() {
f inal Rat in g a Ra ting = n ew R a t ing ( 3 , " D a ve ") ;
f inal Rat in g a nE qual Rati ng = n ew R a tin g ( 3, " Da v e" ) ;
f inal Rat in g a Di ffer entR atin g = n e w Ra t i ng (2 , " Ja s on ") ;
f inal Rat in g a Su bRat ing = ne w Rat i n g(3 , "D av e" ) {
};
n ew Eq ual sT est er (aRa ting , an E q ual R a tin g , a Di ff e re n tR at i ng , a Su bR a ti ng ) ;
}
}

Now we need to implement eq uals () and h a s hCo d e () in R at in g :


pub l ic bo ole an eq ua ls(f inal Obj e c t a n O bje c t ) {
i f (anO bje ct == n ull) ret urn f a lse ;
i f (anO bje ct .ge tC lass () ! = th i s .ge t C las s ( )) r et u rn fa ls e ;
f i nal R ati ng ot he rRat ing = (R a t ing ) anO b j ec t;
r e turn (ge tV alu e( ) == oth erRa t i ng. g e tVa l u e( )) & &
(g et Sou rc e(). equa ls(o t h erR a t ing . g et So ur c e( ) )) ;
}

pub l ic in t h as hCo de () {
i n t res ult = 17 ;
r e sult = 3 7 * r es ult + va lue;
r e sult = 3 7 * r es ult+ sou rce. h a shC o d e() ;
r e turn res ul t;
}

This lets tes tEq ua ls( ) pass. Now go back to our previous test. It now passes with flying colors ...
well, OK, just green. The next step is to add rating addition to the Swing GUI. Here's our test.

Test 98: Selecting a movie, setting the rating value, and source to
specific values and pressing the "Add Rating" button causes that rating
to be added to the selected movie

pub l ic vo id te stA dd ingR atin gs() {


J L istOp era to r m ov ieLi st =
ne w J Li stO pe rato r(ma inWi n d ow, n ew N a me Ba se d Ch o os er ( "m ov i eL is t ") );
m o vieLi st. cl ick On Item (0, 1);
J C omboB oxO pe rat or rat ingC ombo =
new J Com bo BoxO pera tor( m a inW i n dow ,
n e w N a m eBa s e dC ho os e r( " ra ti n gv al u e" )) ;
r a tingC omb o. set Se lect edIn dex( 3 ) ;
J T extFi eld Op era to r ra ting Sour c e Fie l d =
ne w J Te xtFi eldO pera t o r(m a i nWi n d ow ,
n e w N a m eB as ed C ho o se r( " ra ti n gs ou r ce ") ) ;
r a tingS our ce Fie ld .ent erTe xt(" D a ve" ) ;
J B utton Ope ra tor a ddRa ting Butt o n =
n ew JB utt on Oper ator (mai n W ind o w , n e w N am eB a se d Ch oo s er (" a dd ra t in g" ) );
a d dRati ngB ut ton .d oCli ck() ;
V e ctor rat in gs = star Wars .get R a tin g s ();
a s sertE qua ls ("s ta rWar s sh ould h ave 2 ra t i ng s. ", 2 ,r a ti ng s .s iz e () );
R a ting new Ra tin g = (R atin g)ra t i ngs . g et( 1 ) ;
a s sertE qua ls ("N ew rat ing has w r ong v alu e . ", 3, ne w Ra t in g. g et Va l ue () ) ;
a s sertE qua ls ("N ew rat ing has w r ong s our c e ." , "D a ve " ,n ew R at in g .g et S ou rc e () );

J L istOp era to r r at ings List =


ne w J Li stO pe rato r(ma inWi n d ow, n ew N a me Ba se d Ch o os er ( "r at i ng s" ) );
v e rifyR ati ng s(r at ings List , st a r War s ) ;

This selects Star Wars from the movie list, selects a rating value, enters a rating source, pushes the Add
Rating button, verifies that Star Wars has the expected new rating, and verifies that it was added to the
ratings list.
The initial failure of this test is due to the lack of a rating source field. We easily add one:
pri v ate J Pan el in it Deta ilPa ne() {
/ / ...
d e tailP ane .a dd( in itRa ting List ( ) );
d e tailP ane .a dd( in itRa ting Valu e F iel d ( ));
d e tailP ane .a dd( in itRa ting Sour c e Fie l d ()) ;
d e tailP ane .a dd( in itAd dRat ingB u t ton ( ) );
/ / ...
}
pri v ate C omp on ent i nitR atin gVal u e Fie l d () {
L i st ra tin gI con sW ithN A = Arra y s .as L i st( C u st om Mo v ie L is tR e nd er e r. ic o ns () ) ;
L i st ra tin gI con sW itho utNA = r a t ing I c ons W i th NA .s u bL i st (1 , 7 );
r a tingV alu eF iel d = ne w JC ombo B o x(r a t ing I c on sW it h ou t NA .t o Ar ra y () );
r a tingV alu eF iel d. setN ame( "rat i n gva l u e") ;
r e turn rat in gVa lu eFie ld;
}
pri v ate C omp on ent i nitR atin gSou r c eFi e l d() {
r a tingS our ce Fie ld = n ew J Text F i eld ( 1 6);
r a tingS our ce Fie ld .set Name ("ra t i ngs o u rce " ) ;
r e turn rat in gSo ur ceFi eld;
}
pri v ate C omp on ent i nitA ddRa ting B u tto n ( ) {
J B utton ad dB utt on = n ew J Butt o n ("A d d Ra t i ng ") ;
a d dButt on. se tNa me ("ad drat ing" ) ;
a d dButt on. ad dAc ti onLi sten er(n e w Ac t i onL i s te ne r( ) {
p ublic vo id ac ti onPe rfor med( A c tio n E ven t e) {

myEdi tor .a ddR at ing( );


}
});
r e turn add Bu tto n;

Now the test fails due to an incorrect rating being added. We need to fill in the accessors that fetch the
relevant field values:
pub l ic in t g et Rat in gVal ueFi eld( ) {
r e turn rat in gVa lu eFie ld.g etSe l e cte d I nde x ( );
}
pub l ic St rin g get Ra ting Sour ceFi e l d() {
r e turn rat in gSo ur ceFi eld. getT e x t() ;
}

Now the test passes. Is there anything to clean up? Yes, when we added the component code, we did it
quick and dirty. We need to arrange the rating addition components in a nicer way and pull their setup
into a separate method. We did something very similar before, so here's the final code:
pri v ate J Pan el in it Deta ilPa ne() {
/ / ...
d e tailP ane .a dd( in itCa tego ryFi e l d() ) ;
d e tailP ane . add (B ox.c reat eRig i d Are a ( new D im en si o n( 0 , 5) ) );
d e tailP ane .a dd( in itRa ting sPan e ( ));
r e turn det ai lPa ne ;
}
pri v ate C omp on ent i nitR atin gsPa n e () {
J P anel rat in gsP an e = new JPan e l ();
r a tings Pan e. set La yout (new Box L a you t ( rat i n gs Pa ne , B o xL ay o ut .X AX IS ) );
r a tings Pan e. set Bo rder (Bor derF a c tor y . cre a t eE tc he d Bo r de r( ) );
r a tings Pan e. add (i nitR atin gLis t ( ));
J P anel add Ra tin gP ane = ne w JP a n el( ) ;
a d dRati ngP an e.s et Layo ut(n ew B o x Lay o u t(a d d Ra ti ng P an e , Bo x La yo u t. Y A XI S) ) ;
a d dRati ngP an e.s et Bord er(B orde r F act o r y.c r e at eE mp t yB o rd er ( 1, 1 0 , 10 , 1 0) ) ;
a d dRati ngP an e.a dd (ini tRat ingV a l ueF i e ld( ) ) ;
a d dRati ngP an e.a dd (Box .cre ateR i g idA r e a(n e w D im en s io n (0 , 5 )) );
a d dRati ngP an e.a dd (ini tRat ingS o u rce F i eld ( ) );
a d dRati ngP an e.a dd (Box .cre ateR i g idA r e a(n e w D im en s io n (0 , 5 )) );
a d dRati ngP an e.a dd (ini tAdd Rati n g But t o n() ) ;
a d dRati ngP an e.a dd (Box .cre ateR i g idA r e a(n e w D im en s io n (0 , 5 )) );
a d dRati ngP an e.a dd (Box .cre ateG l u e() ) ;

r a tings Pan e. add (B ox.c reat eRig i d Are a ( new D im en si o n( 5 , 0) ) );


r a tings Pan e. add (a ddRa ting Pane ) ;
r e turn rat in gsP an e;

Amazon

Prev

don't be afraid of buying books

Next

REMOVE THE SINGLE-RATING FIELD


The single-rating field no longer serves any purpose. Remove it and update the system
appropriately.
Now that everything has been updated to use multiple ratings, we can remove support for the early
single-rating system.
We will start by removing setR atin g() in Mo v i e. From there you compile and run the tests.When you
find compile errors or failing tests, you need to investigate and either remove references to already
removed code, or tweak tests to not use deleted functionality. I'll leave this as an exercise to the
reader. Starting with the removal of M ov ie . s etR a t ing ( ) it took about 30 minutes to clean the
system of the single-rating support. Compile and run the tests (all the tests) continuously during this
process to be sure you don't overdo it.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
This has been a rather involved story. Looking back, we should have split it into two or more smaller
stories. We did a lot of work in this chapter. We moved from a simple, single numeric rating per movie
to a multirating system, each with a source annotation.
As part of the process, we had to revamp our persistence mechanism, moving it to its own set of
classes as well as moving to a more structured format. This provides an example of letting the
immediate requirements drive the addition of complexity to the system.
More and more, XML is a commonplace standard. It is the basis of many pieces of internet infrastucture.
May be we should consider XML as the simplest thing when our data has any amount of structure. This
story certainly would have been easier to do if we had used XML from the beginning rather than a flat
text file.

Amazon

Prev

don't be afraid of buying books

Chapter 19. REVIEWS


Movies can have a review attached to each rating. If there is a review there must be a
source.

Next

Amazon

Prev

don't be afraid of buying books

Next

ADD A REVIEW TO RATINGS


Add support for a single review to Rating.
Here are the tests we want to pass when we've finished this task:
Test 99. A rating created without a review should answer an empty string when asked
for its review.

Test 100. A rating created with a review should answer that string when asked for its
review.

First, some cleanup


We need to extend the concept of a rating.Before doing this, we should make sure that Ra t in g is well
encapsulated. When we look at how ratings are added to movies, we find these methods in Mo v ie :
pub l ic vo id ad dRa ti ng(R atin g ra t i ng T o Add ) {
r a tings .ad d( rat in g To Add) ;
}
pub l ic vo id ad dRa ti ng(i nt a Rati n g ) {
a d dRati ng( ne w R at ing( aRat ing) ) ;
}
pub l ic vo id ad dRa ti ng(i nt a Rati n g , S t r ing a Ra ti ng S ou r ce ) {
a d dRati ng( ne w R at ing( aRat ing, a Rat i n gSo u r ce )) ;
}

If we look further, we find that the last two methods reflect the constructors for R a ti ng :
pub l ic Ra tin g( int a Rati ng) {
t h is(aR ati ng , " An onym ous" );
}

pub l ic Ra tin g( int a Rati ng, Stri n g aR a t ing S o ur ce ) {


v a lue = aR at ing ;
s o urce = a Ra tin gS ourc e;
}

This is starting to smell like Shotgun Surgery, in that if we add a constructor that take a S tr in g
review, we will likely want to add a corresponding add R a ti ng method. This is not a good trend. We

need to clean this up before moving forward.


To do this we must replace calls to the complex a d d Rat i ng methods with a call to
add R ating (Ra ti ng) with a nested Rat i ng construction. For example, the first line below needs to be
replaced with the second:
sta r Wars. add Ra tin g( 4, " Dave ");
sta r Wars. add Ra tin g( new Rati ng(4 , "Da v e ")) ;

After changing each line, we compile and rerun all the tests. This keeps us from introducing errors
accidentally. If we do, we know immediately, when the code is still in mind, and in the editor.
Once we have replaced all references to a method, we can delete it. That will leave only one
add R ating () method: the one that takes a R a t ing .

Test 99: A rating created without a review should answer an empty


string when asked for its review
Now we need to design our review handling. First, we think about what it means not to have are view.
We should maintain backward compatibility if it is possible, and in this case it should be. If we use the
current constructors that don't take are view, the resulting Ra ti n g shouldn't have a review. Now, we
write this up as a test:
pub l ic vo id te stN oR evie w() {
R a ting aRa ti ng = new Rati ng(3 , "Da v e ");
a s sertE qua ls ("R at ing with out r e vie w sho u l d ha ve em p ty r e vi ew . ",
"" ,
aR at ing. getR evie w ( ));
}

We need to stub Ra ti ng. getRe view( ) :


pub l ic St rin g get Re view () {
r e turn nul l;
}

The test fails until we fake it in g etRe vi ew ( ) :


pub l ic St rin g get Re view () {
r e turn "";
}

Now we should move aR atin g into the common fixture, and remove it from te s t- Eq u al s( ) :
pri v ate R ati ng aR at ing = nu ll;
pro t ected vo id se tU p() {

a R ating = ne w R at ing( 3, " Dave " ) ;

Test 100: A rating created with a review should answer that string when
asked for its review
Next, we need to ask, "What if a Rati ng like that has a review?" and "How does a R at in g get a
review?" The answer to the first question is that it should be able to answer it when asked. The answer
to the second is that we'll start with the simplest approach, which is via a constructor. If we need to set
the review after creation, we'll add the capability then. We express these answers as a test:
pub l ic vo id te stW it hCon stru cted R e vie w ( ) {
R a ting rat in gWi th Revi ew = new R ati n g (4, " Ja so n" , " T es t R ev ie w ") ;
a s sertE qua ls ("R ev iew is i ncor r e ct. " ,
"T es t Re view ",
ra ti ngWi thRe view . g etR e v iew ( ) );
}

Since we're happy with how things are going and we feel confident, we'll take a bigger step and rework
Rat i ng's constructors to accommodate the review argument, as well as update g e tR ev i ew () to use
the new field:
pri v ate S tri ng re vi ew = nul l;
pub l ic Ra tin g( int a Rati ng) {
t h is(aR ati ng , " An onym ous" , "" ) ;
}
pub l ic Ra tin g( int a Rati ng, Stri n g aR a t ing S o ur ce ) {
t h is(aR ati ng , a Ra ting Sour ce, " " );
}
pub l ic Ra tin g( int a Rati ng, Stri n g aR a t ing S o ur ce , S tr i ng a R ev ie w ) {
v a lue = aR at ing ;
s o urce = a Ra tin gS ourc e;
r e view = a Re vie w;
}
pub l ic St rin g get Re view () {
r e turn rev ie w;
}

Our new test passes, and all the previous ones still do. This might be a good time to rerun Al l Te st s .
Green.

Amazon

Prev

don't be afraid of buying books

Next

SAVE REVIEW
Add support to persistence for saving reviews.
We just have one test here:
Test 101. If a rating has an attached review, the review is saved as the text of the
<rating> element.

We already have tests in place for saving without any reviews. Nothing there will change. We also don't
need to do anything relating to the saving mechanism in the interface components. All we need to worry
about is that if there are reviews, they get saved properly. We can do this at the lowest level, in
Tes t Movie Lis tW rit in g.We just need a single test that verifies that saving with a review works.

Test 101: If a rating has an attached review, the review is saved as the
text of the <rating> element
pub l ic vo id te stW ri ting OneM ovie W i thR e v iew ( ) t hr ow s E x ce pt i on {
S t ring sta rW ars Ou tput = " <mov i e lis t > " +
"< movi e nam e = \ " S ta r Wa r s\ " " +
" c a teg o r y = \ "S ci en c e F ic ti o n\ "> " +
" <rat i n gs> " +
"<ra t i ng v a lue = \ "4 \" s o ur c e= \" N Y Ti m es \" >" +
"Ni c e , f l u ffy , go od v s e v il , S ci Fi pi ec e " +
"</r a t ing > " +
" </ra t i ngs > " +
"</ movi e > " +
" </mo viel i s t>" ;

M o vie s tar Wa rs = new Movi e("S t a r W a r s", C a te go ry . SC I FI );


s t arWar s.a dd Rat in g(ne w Ra ting ( 4 ,
" N Y Ti m e s",
" N i ce, f luf f y , go od vs ev il , S ci F i pi e ce ") ) ;
m o vieLi st. ad d(s ta rWar s);
w r iter. wri te (mo vi eLis t);
a s sertX MLE qu al( "W rong out put f r om w r iti n g a s in g le mo vi e l is t ." ,
st ar Wars Outp ut,
de st inat ion. toSt r i ng( ) ) ;

This fails since the XML comparison fails. To get it passing, we need to work on
XML M ovieL ist Wr ite r:
pri v ate M ess ag eFo rm at r atin gFor m a tWi t h out R e vi ew =
n ew Me ssa ge Form at(" <rat i n g v a l ue= \ " {0 ,n um b er , in te g er }\ " " +
" s our c e =\" { 1 }\ " /> " );
pri v ate M ess ag eFo rm at r atin gFor m a tWi t h Rev i e w =
n ew Me ssa ge Form at(" <rat i n g v a l ue= \ " {0 ,n um b er , in te g er }\ " " +
" s our c e =\" { 1 }\ "> " +
"{ 2}" +
"</r atin g > ");

//. . .
p r ivate vo id wr it eRat ing( Rati n g ra t i ngT o W ri te ) t hr o ws I O Ex ce p ti on {
Objec t[] a rgs = { n ew I nteg e r (ra t i ngT o W ri te .g e tV a lu e( ) ),
ra ting ToWr i t e.g e t Sou r c e( ),
ra ting ToWr i t e.g e t Rev i e w( ) };
Messa geF or mat f orma tToU se;
if (r ati ng ToW ri te.h asRe view ( ) ) {
fo rma tT oUs e = ra ting Form a t Wit h R evi e w ;
} els e {
forma tTo Us e=r at ingF orma tWit h o utR e v iew ;

}
d e stina tio n. wri te (for matT oUse . f orm a t (ar g s )) ;

Notice how we've added a second format for ratings and renamed the previous one to better reveal its
intended use. We also need to add a query (h a s Rev i e w( )) to R a ti n g:
pub l ic bo ole an ha sR evie w() {
r e turn rev ie w ! = null && revi e w .le n g th( )
> 0;
}

With all of this in place, the test passes. Now we need to clean up. When we added the second rating
format we introduced some duplication in the format strings. Ratings should have a common core format
whether there is a review or not. We need to refactor to eliminate the duplication and make the
intended commonality obvious:
pri v ate S tri ng co mm onRa ting Form a t =
"<r at ing valu e=\" { 0 ,nu m b er, i n te ge r} \ " " +
" sour ce=\ " { 1}\ " " ;
pri v ate M ess ag eFo rm at r atin gFor m a tWi t h out R e vi ew =
n ew Me ssa ge Form at(c ommo n R ati n g For m a t + " / >" ) ;
pri v ate M ess ag eFo rm at r atin gFor m a tWi t h Rev i e w =
n ew Me ssa ge Form at(c ommo n R ati n g For m a t + "> { 2} < /r at i ng >" ) ;

Make sure all the tests still pass, and that should do it.

Amazon

Prev

don't be afraid of buying books

Next

LOAD REVIEW
Add support to persistence for loading reviews.
Similarly to the previous task, we only need to concern ourselves with reading a M ov ie L is t that
contains a review.
Test 102. Reading an XML stream that contains text for a rating element results in that
rating having an attached review.

We need to add a test to Te stMo vieLi s tRe a d ing . Just for fun, we'll take a slightly different
approach this time. The existing test for reading a movie uses a fixture that uses a movie with two
ratings. Let's add a review to one of them.

Test 102: Reading an XML stream that contains text for a rating element
results in that rating having an attached review
First, a tweak to s etU p( ):
one M ovieP ref ix = "< movi elis t>" +
"< movi e na me=\ " S tar W ars \ " " +
"cat egor y = \"S c i enc e Fi ct io n \" > " +
"<r atin gs>" +
" <rat ing v a lue = \ "3\ " " +
" s o urc e = \"D a v e\ " /> " +
" <rat ing v a lue = \ "5\ " " +
" s o urc e = \"J a s on \" >" +
"Gr eat m o vie ! " +
" </ra ting > " +
"</ rati ngs> " +
"< /mov ie>" ;

This needs a corresponding tweak to the test:


pub l ic vo id te stR ea ding OneM ovie ( ) th r o ws E x ce pt io n {
/ / ...
R a ting fir st Rat in g =( Rati ng)r a t ing I t era t o r. ne xt ( );
/ / ...
a s sertF als e( "Fi rs t ra ting sho u l d n o t ha v e a r ev i ew . ",
fi rs tRat ing. hasR e v iew ( ) );
/ / ...
R a ting sec on dRa ti ng = (Rat ing) r a tin g I ter a t or .n ex t () ;
/ / ...
a s sertT rue (" Sec on d ra ting sho u l d h a v e a r ev ie w. " ,
sec on dRat ing. hasR e v iew ( ) );
a s sertE qua ls ("S ec ond rati ng's r evi e w is w ro ng ." ,
"G re at m ovie !",
se co ndRa ting .get R e vie w ( ));
/ / ...

As expected, this test fails on the assertion that the second rating has a review. To correct that, we
need to revisit pr oce ss Rati ng() in X M LMo v i eLi s t Rea d er :
pri v ate R ati ng pr oc essR atin g(El e m ent r ati n g El em en t ) t hr ow s I OE x ce pt i on {
/ / ...
S t ring rev ie w = r atin gEle ment . g etT e x tTr i m () ;
r e turn new R ati ng (val ue, sour c e , r e v iew ) ;
}

Green.

Amazon

Prev

don't be afraid of buying books

Next

DISPLAY REVIEW
Add support to the GUI to display reviews.
Now that we can handle reviews internally, it's time to make them available to the user.
Test 103. Telling the logical layer that a rating is selected that has no review results in the
view being told to put an empty string in the review field.

Test 104. Telling the logical layer that a rating is selected that has a review results in the
view being told to put the review string in the review field.

We start with a test in Te s tMovi eLis tE d i t o r .

Test 103: Telling the logical layer that a rating is selected that has no
review results in the view being told to put an empty string in the review
field
p u bl i c vo id te st Rat i ngSe l ecti onW i t h o u t R e v i e w ( ) {
mo c k V ie w. set Mo vie s (mov i es);
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Na meF i eld( s tarW ars . g e t N a m e ( ) ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ca teg o ryFi e ld(C ate g o r y . S C I F I ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g s(st a rWar s.g e t R a t i n g s ( ) ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Revi e wFie ld( " " ) ;
co n t r ol .s etV oi dCa l labl e (1);
co n t r ol .a cti va te( ) ;

Mo v i e Li st Edi to r e d itor = ne w M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
ed i t o r. se lec t( 0);
ed i t o r. se lec tR ati n g(0) ;
co n t r ol .v eri fy ();

Two stubs need to be added. The first is s e t R a t i n g R e v i e w F i e l d ( ) in M o v i e L i s t E d i t o r V i e w :


v o id s e tR at ing Re vie w Fiel d (Str ing s t r i n g ) ;

The other is in Mo v ieLi s tEdi t or:


p u bl i c vo id se le ctR a ting ( int i) {
}

Now we can run the new test. It fails because the expected call to s e t R a t i n g R e v i e w F i e l d ( ) was not
made. A simple change will get the test passing:

p u bl i c vo id se le ctR a ting ( int i) {


vi e w . se tR ati ng Rev i ewFi e ld(" ");
}

Now we've faked it. Our next test will drive us to make it work properly.

Test 104: Telling the logical layer that a rating is selected that has a review
results in the view being told to put the review string in the review field
This test is very similar to the previous one, except that a review is present:
p u bl i c vo id te st Rat i ngSe l ecti onW i t h R e v i e w ( ) {
St r i n g da ves Re vie w = " N ot b ad, b u t k i n d o f l i g h t . " ;
st a r W ar s. add Ra tin g (new Rati ng( 3 , " D a v e " , d a v e s R e v i e w ) ) ;
mo c k V ie w. set Mo vie s (mov i es);
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Na meF i eld( s tarW ars . g e t N a m e ( ) ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ca teg o ryFi e ld(C ate g o r y . S C I F I ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g s(st a rWar s.g e t R a t i n g s ( ) ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Revi e wFie ld( d a v e s R e v i e w ) ;
co n t r ol .s etV oi dCa l labl e (1);
co n t r ol .a cti va te( ) ;

Mo v i e Li st Edi to r e d itor = ne w M o v i e L i s t E d i t o r ( m o v i e L i s t , m o c k V i e w ) ;
ed i t o r. se lec t( 0);
ed i t o r. se lec tR ati n g(1) ;
co n t r ol .v eri fy ();

Now we need to make s elec t Rati ng() do more than return an empty string; it has to fetch the review
corresponding to the selected rating and send that to the view:
p u bl i c vo id se le ctR a ting ( int i) {
Ra t i n g se lec te dRa t ing = sel ect e d M o v i e . g e t R a t i n g ( i ) ;
vi e w . se tR ati ng Rev i ewFi e ld(s ele c t e d R a t i n g . g e t R e v i e w ( ) ) ;
}

The compiler (or the editor if we are using an advanced IDE) tells us that we need to write
g e tR a t i ng (i nt) . When we look at Mov i e we see that we have g e t R a t i n g ( ) , which returns the average
rating. Now we add g e tRa ti ng(i nt). This is confusingthe former returns an i n t that is the average of
all the ratings, while the latter returns the R a t i n g at the given index. We make a note to refactor to make
the intent clearer by renaming getR ating ( ) to g e t A v e r a g e R a t i n g ( ) . While we're at it,
g e tR a w R at in g() smells funny. We'll have a closer look at it as well.
But let's get back to the problem at hand. Here's our new g e t R a t i n g ( i n t ) :
p u bl i c Ra ti ng ge tRa t ing( i nt i ) {
re t u r n (R ati ng )ra t ings . get( i);
}

OK, green!

Cleaning up the average rating queries


Now let's do those refactorings. The rename is easy so that we won't go into detail here. Just be sure to run

A l lT e s t s after each small change if you are refactoring by hand, and afterward if you are using an
automated rename.
The next item on our list is a bit more involved. The method in question, g e t R a w R a t i n g ( ) , is used only in
M o vi e R a ti ng Com pa rat o r:
p u bl i c in t com pa re( O bjec t o1, Ob j e c t o 2 ) {
Mo v i e f ir stM ov ie = (Mo v ie)o 1;
Mo v i e s ec ond Mo vie = (M o vie) o2;
in t r at in gOf Fi rst M ovie = fi rst M o v i e . g e t R a w R a t i n g ( ) ;
in t r at in gOf Se con d Movi e = s eco n d M o v i e . g e t R a w R a t i n g ( ) ;
re t u r n ra tin gO fSe c ondM o vie
- ra t i n gO fF irs tM ovi e ;
}

Its only use is to return - 1 if the movie is unrated rather than throwing an exception. We have both a query
to check if there is a rating, as well as an exception that is thrown if the rating of an unrated movie is
requested. We should really only have one. I suggest that we refactor to get rid of the exception. We can do
this by renaming get R awR at ing( ) to ge t A v e r a g e R a t i n g ( ) ,so that we now have:
p u bl i c in t get Av era g eRat i ng() {
if ( h as Ra tin g( )) {
r e t ur n cal cu lat e Aver a geRa tin g ( ) ;
} else {
r e t ur n
-1;
}
}

The rename took care of the above c ompa r e ( ) method, so it's not an issue anymore. This rename broke the
compilation of a few methods. The first is in C u s t o m M o v i e L i s t R e n d e r e r :
p u bl i c Co mp one nt ge t List C ellR end e r e r C o m p o n e n t ( . . . ) {
// . . .
if ( m ov ie ToR en der . hasR a ting ()) {
try {
s et Ic on( ra tin g Icon s [mov ieT o R e n d e r . g e t A v e r a g e R a t i n g ( ) + 1 ] ) ;
} c at ch (U nr ate d Exce p tion ex ) {
}
} else {
s e t Ic on (ra ti ngI c ons[ 0 ]);
}
// . . .
}

In this case, we can just strip off the try c a t c h block:


p u bl i c Co mp one nt ge t List C ellR end e r e r C o m p o n e n t ( . . . ) {
// . . .
if ( m ov ie ToR en der . hasR a ting ()) {
s e t Ic on (ra ti ngI c ons[ m ovie ToR e n d e r . g e t A v e r a g e R a t i n g ( ) + 1 ] ) ;
} else {
s e t I co n( rat in gIc o ns[0 ] );
}
// . . .
}

We can simplify this further since now mov i e T o R e n d e r . g e t A v e r a g e R a t i n g ( ) returns - 1 if the movie is
unrated. The icon index calculation will now work in that case, so we have:
p u bl i c Co mp one nt ge t List C ellR end e r e r C o m p o n e n t ( . . . ) {
// . . .
se t I c on (r ati ng Ico n s[mo v ieTo Ren d e r . g e t A v e r a g e R a t i n g ( ) + 1 ] ) ;
// . . .
}

Running the related test verifies that this works fine.


The next problem is in T e stMo v ieRa tin g s ( ) :
p u bl i c vo id te st Unr a tedC o nstr uct o r ( ) {
tr y {
s t a rT re k.g et Ave r ageR a ting ();
f a i l( "G ett in g r a ting of a n u n r a t e d M o v i e s h o u l d t h r o w U n r a t e d M o v i e E x c e p t i o n " ) ;
} c a t ch ( Unr at edE x cept i on e x) {
}
}

Here, we can't just strip off the exception-related code. . . our test relies on it. But we can easily rewrite the
test to use the tweaked behavior:
p u bl i c vo id te st Unr a tedC o nstr uct o r ( ) {
as s e r tF al se( "U nra t ed m o vie sho u l d n o t r e p o r t t h a t i t h a s a r a t i n g . " ,
s tar T rek. h asRa tin g ( ) ) ;
as s e r tE qu als (" Ave r age r atin g o f a n u n r a t e d m o v i e i s w r o n g . " ,
- 1,
s tar T rek. g etAv era g e R a t i n g ( ) ) ;
}

The other problem is in T e stMo v ie:


p u bl i c vo id te st Unr a tedE x cept ion ( ) {
tr y {
s t a rW ar s.g et Ave r ageR a ting ();
f a i l( "g etR at ing on a n unr ate d M o v i e s h o u l d t h r o w U n r a t e d E x c e p t i o n . " ) ;
} c a t ch ( Unr at edE x cept i on e x) {
a s s er tE qua ls ("U n rate d Exce pti o n s h o u l d i d e n t i f y t h e m o v i e . " ,
sta r Wars . getN ame ( ) ,
ex. g etMe s sage ()) ;
}
}

This test is redundant. The test we just updated tested the very same behavior. We can simply delete this
test.
Now we run A llT e sts to verify that everything passes. OK, done. We've just cleaned up our code and made
it clearer. High five!

Expose reviews in the GUI


Now we need to add review display to the Swing GUI component.
Test 105. Selecting a rating that doesn't have a review puts an empty string in the review
field.

Test 106. Telling the logical layer that a rating is selected that has a review results in the
view being told to put the review string in the review field.

Our first test will verify correct behavior when there is no review as well as drive the evolution of the GUI
itself.

Test 105: Selecting a rating that doesn't have a review puts an empty string
in the review field
p u bl i c vo id te st Rat i ngSe l ecti onW i t h o u t R e v i e w ( ) {
JL i s t Op er ato r mov i eLis t =
ne w JLi st Ope r ator ( main Win d o w , n e w N a m e B a s e d C h o o s e r ( " m o v i e L i s t " ) ) ;
mo v i e Li st .cl ic kOn I tem( 0 , 1) ;
JL i s t Op er ato r rat i ngsL i st =
ne w JLi st Ope r ator ( main Win d o w , n e w N a m e B a s e d C h o o s e r ( " r a t i n g s " ) ) ;
ra t i n gs Li st. cl ick O nIte m (0, 1);
JT e x t Ar ea Ope ra tor revi e wFie ld =
new J Tex t Area O pera tor ( m a i n W i n d o w , n e w N a m e B a s e d C h o o s e r ( " r e v i e w " ) ) ;
as s e r tE qu als (" The r e sh o uld not b e a r e v i e w . " , " " , r e v i e w F i e l d . g e t T e x t ( ) ) ;
}

This fails because there isn't a Tex tArea named " r e v i e w " . Let's fix that in
S w in g M o vi eL ist Ed ito r View :
p r iv a t e J Te xtA re a r a ting R evie wFi e l d = n u l l ;
/ / .. .

p r iv a t e C om pon en t i n itRa t ings Pan e ( ) {


//...
a d d R at in gPa ne .ad d (ini t Rati ngS o u r c e F i e l d ( ) ) ;
a d d R at in gPa ne .ad d (Box . crea teR i g i d A r e a ( n e w D i m e n s i o n ( 0 , 5 ) ) ) ;
a d d R at in gPa ne .ad d (ini t Rati ngR e v i e w F i e l d ( ) ) ;
a d d R at in gPa ne .ad d (Box . crea teR i g i d A r e a ( n e w D i m e n s i o n ( 0 , 5 ) ) ) ;
a d d R at in gPa ne .ad d (ini t AddR ati n g B u t t o n ( ) ) ;
//...
}
p r iv a t e C om pon en t i n itRa t ingR evi e w F i e l d ( ) {
ra t i n gR ev iew Fi eld = ne w JTe xtA r e a ( ) ;
ra t i n gR ev iew Fi eld . setN a me(" rev i e w " ) ;

JS c r o ll Pa ne sc rol l er =
n ew JS cro l lPan e (rat ing R e v i e w F i e l d ,
Scro llP a n e C o n s t a n t s . V E R T I C A L S C R O L L B A R A L W A Y S ,
Scro llP a n e C o n s t a n t s . H O R I Z O N T A L S C R O L L B A R N E V E R ) ;
re t u r n sc rol le r;

The method i nit R atin g sPan e () is getting rather large; we should make a note to clean it up (by
extracting the construction of the rating detail pane) later.
This addition makes our test pass so we can do that refactoring. Here's the result:
p r iv a t e C om pon en t i n itRa t ings Pan e ( )

JP a n e l ra tin gs Pan e = n e w JP ane l ( ) ;


ra t i n gs Pa ne. se tLa y out( n ew B oxL a y o u t ( r a t i n g s P a n e , B o x L a y o u t . X A X I S ) ) ;
ra t i n gs Pa ne. se tBo r der( B orde rFa c t o r y . c r e a t e E t c h e d B o r d e r ( ) ) ;
ra t i n gs Pa ne. ad d(i n itRa t ingL ist ( ) ) ;
ra t i n gs Pa ne. ad d(B o x.cr e ateR igi d A r e a ( n e w D i m e n s i o n ( 5 , 0 ) ) ) ;
ra t i n gs Pa ne. ad d(i n itRa t ingD eta i l P a n e ( ) ) ;
re t u r n ra tin gs Pan e ;

p r iv a t e J Pa nel in itR a ting D etai lPa n e ( ) {


JP a n e l ad dRa ti ngP a ne = new JPa n e l ( ) ;
ad d R a ti ng Pan e. set L ayou t (new Bo x L a y o u t ( a d d R a t i n g P a n e , B o x L a y o u t . Y A X I S ) ) ;
ad d R a ti ng Pan e. set B orde r (Bor der F a c t o r y . c r e a t e E m p t y B o r d e r ( 1 , 1 0 , 1 0 , 1 0 ) ) ;
ad d R a ti ng Pan e. add ( init R atin gVa l u e F i e l d ( ) ) ;
ad d R a ti ng Pan e. add ( Box. c reat eRi g i d A r e a ( n e w D i m e n s i o n ( 0 , 5 ) ) ) ;
ad d R a ti ng Pan e. add ( init R atin gSo u r c e F i e l d ( ) ) ;
ad d R a ti ng Pan e. add ( Box. c reat eRi g i d A r e a ( n e w D i m e n s i o n ( 0 , 5 ) ) ) ;
ad d R a ti ng Pan e. add ( init R atin gRe v i e w F i e l d ( ) ) ;
ad d R a ti ng Pan e. add ( Box. c reat eRi g i d A r e a ( n e w D i m e n s i o n ( 0 , 5 ) ) ) ;
ad d R a ti ng Pan e. add ( init A ddRa tin g B u t t o n ( ) ) ;
ad d R a ti ng Pan e. add ( Box. c reat eRi g i d A r e a ( n e w D i m e n s i o n ( 0 , 5 ) ) ) ;
ad d R a ti ng Pan e. add ( Box. c reat eGl u e ( ) ) ;
re t u r n ad dRa ti ngP a ne;
}

Test 106: Telling the logical layer that a rating is selected that has are view
results in the view being told to put the review string in the review field
Now we need a test that verifies the behavior when there is a review to be displayed. First we need to tweak
the fixture to add a rating with a review:
p r ot e c t ed v oid s etU p () t h rows Ex c e p t i o n {
// . . .
th e S h in in g = n ew M ovie ( "The Sh i n i n g " , C a t e g o r y . H O R R O R , 2 ) ;
th e S h in in g.a dd Rat i ng(n e w Ra tin g ( 5 , " J a c k " , " A t i m e l e s s c l a s s i c " ) ) ;
// . . .
}
/ / .. .
p u bl i c vo id te st Rat i ngSe l ecti onW i t h R e v i e w ( ) {
JL i s t Op er ato r mov i eLis t =
ne w JLi st Ope r ator ( main Win d o w , n e w N a m e B a s e d C h o o s e r ( " m o v i e L i s t " ) ) ;
mo v i e Li st .cl ic kOn I tem( 3 , 1) ;
JL i s t Op er ato r rat i ngsL i st =
ne w JLi st Ope r ator ( main Win d o w , n e w N a m e B a s e d C h o o s e r ( " r a t i n g s " ) ) ;
ra t i n gs Li st. cl ick O nIte m (1, 1);
JT e x t Ar ea Ope ra tor revi e wFie ld =
new J Tex t Area O pera tor ( m a i n W i n d o w , n e w N a m e B a s e d C h o o s e r ( " r e v i e w " ) ) ;
as s e r tE qu als (" The r e sh o uld be a r e v i e w . " ,
"A t imel e ss C las s i c ! " ,
rev i ewFi e ld.g etT e x t ( ) ) ;
}

This fails since se t Rati n gRev i ewFi eld ( ) currently does nothing. It needs to fill in the new J T e s t A r e a :
p u bl i c vo id se tR ati n gRev i ewFi eld ( S t r i n g t h e R e v i e w ) {

ra t i n gR ev iew Fi eld . setT e xt(t heR e v i e w ) ;

The test still fails. Let's look at the code for the rating list:
p r iv a t e J Sc rol lP ane init R atin gLi s t ( ) {
ra t i n gL is t = n ew J List ( new Vec t o r ( ) ) ;
ra t i n gL is t.s et Nam e ("ra t ings ");
ra t i n gL is t.s et Sel e ctio n Mode (Li s t S e l e c t i o n M o d e l . S I N G L E S E L E C T I O N ) ;
ra t i n gL is t.s et Cel l Rend e rer( new R a t i n g R e n d e r e r ( ) ) ;

JS c r o ll Pa ne sc rol l er =
ne w J Sc rol l Pane ( rati ngL i s t ,
Scro llP a n e C o n s t a n t s . V E R T I C A L S C R O L L B A R A L W A Y S ,
Scro llP a n e C o n s t a n t s . H O R I Z O N T A L S C R O L L B A R N E V E R ) ;
re t u r n sc rol le r;

We need to add a selection handler:


r a ti n g L is t. add Li stS e lect i onLi ste n e r ( n e w L i s t S e l e c t i o n L i s t e n e r ( ) {
pu b l i c vo id va lue C hang e d(Li stS e l e c t i o n E v e n t e ) {
m y E d it or .se le ctR a ting ( rati ngL i s t . g e t S e l e c t e d I n d e x ( ) ) ;
}
});

That does it.

Some visual cleanup


In playing with the application, we see that there are a couple of visual, aesthetic bits of weirdness:
1. Widgets resize oddly with the window size changed; combo boxes and test fields get the extra height,
the review test area doesn't.
2. When a rating is selected, its review is displayed in the rating-detail area, but its value and source
aren't. This looks odd.
The first issue we can fix simply by overriding the m a x i m u m S i z e ( ) method of the combo boxes and text
fields, like this:
p r iv a t e J Co mbo Bo x i n itCa t egor yFi e l d ( ) {
ca t e g or yF iel d = n e w JC o mboB ox( C a t e g o r y . c a t e g o r i e s ( ) ) {
p u b li c Dim en sio n get M axim umS i z e ( ) {
D im en sio ns ize = getP r efer red S i z e ( ) ;
s iz e. wid th = S hort . MAX VAL U E ;
r et ur n s iz e;
}};
//...
}

The second issue is more involved, and is more functional. We need tests to drive it. We can do this quickly
and easily by extending the tests we just wrote. First, in T e s t M o v i e L i s t E d i t o r :

p u bl i c vo id te st Rat i ngSe l ecti onW i t h o u t R e v i e w ( ) {


// . . .
mo c k V ie w. set Ra tin g s(st a rWar s.g e t R a t i n g s ( ) ) ;
co n t r ol .s etV oi dCa l labl e (1) ;
mo c k V ie w. set Ra tin g Valu e Fiel d(5 ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Sour c eFie ld( " A n o n y m o u s " ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Revi e wFie ld( " " ) ;
co n t r ol .s etV oi dCa l labl e (1);
// . . .
}

That drives us to add two methods to the view interface:


v o id s e tR at ing Va lue F ield ( int i);
v o id s e tR at ing So urc e Fiel d (Str ing s t r i n g ) ;

Now the test fails since these methods are not being called. We now need to add code to
M o vi e L i st Ed ito r. sel e ctRa t ing( ):
p u bl i c vo id se le ctR a ting ( int i) {
Ra t i n g se lec te dRa t ing = sel ect e d M o v i e . g e t R a t i n g ( i ) ;
vi e w . se tR ati ng Val u eFie l d(se lec t e d R a t i n g . g e t V a l u e ( ) ) ;
vi e w . se tR ati ng Sou r ceFi e ld(s ele c t e d R a t i n g . g e t S o u r c e ( ) ) ;
vi e w . se tR ati ng Rev i ewFi e ld(s ele c t e d R a t i n g . g e t R e v i e w ( ) ) ;
}

This gets te stR a ting S elec t ionW ithou t R e v i e w ( ) passing, but t e s t R a t i n g S e l e c t i o n W i t h R e v i e w


fails. That's OKas we are about to extend it to cover this situation anyway:
p u bl i c vo id te st Rat i ngSe l ecti onW i t h R e v i e w ( ) {
// . . .
mo c k V ie w. set Ra tin g s(st a rWar s.g e t R a t i n g s ( ) ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Valu e Fiel d(3 ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Sour c eFie ld( " D a v e " ) ;
co n t r ol .s etV oi dCa l labl e (1);
mo c k V ie w. set Ra tin g Revi e wFie ld( d a v e s R e v i e w ) ;
co n t r ol .s etV oi dCa l labl e (1);
// . . .
}

Green again. Now to revisit the corresponding tests in T e s t S w i n g M o v i e L i s t E d i t o r . Actually, we only


have to extend one of them. There are two reasons for this:
1. we don't need to set method call expectations
2. if it works with a review, it will work without.
p u bl i c vo id te st Wit h Revi e w() {
JL i s t Op er ato r mov i eLis t =
ne w JLi st Ope r ator ( main Win d o w , n e w N a m e B a s e d C h o o s e r ( " m o v i e L i s t " ) ) ;
mo v i e Li st .cl ic kOn I tem( 3 , 1) ;

JL i s t Op er ato r rat i ngsL i st =


ne w JLi st Ope r ator ( main Win d o w , n e w N a m e B a s e d C h o o s e r ( " r a t i n g s " ) ) ;
ra t i n gs Li st. cl ick O nIte m (1, 1);
JC o m b oB ox Ope ra tor rati n gCom bo =
ne w J Co mbo B oxOp e rato r(m a i n W i n d o w ,
ne w N a m e B a s e d C h o o s e r ( " r a t i n g v a l u e " ) ) ;
as s e r tE qu als (" Bad rati n g va lue . " , 5 , r a t i n g C o m b o . g e t S e l e c t e d I n d e x ( ) ) ;

JT e x t Fi el dOp er ato r rat i ngSo urc e F i e l d =


ne w JTe x tFie l dOpe rat o r ( m a i n W i n d o w ,
new NameBasedChooser("ratingsource"));
as s e r tE qu als (" Bad rati n g so urc e . " , " J a c k " , r a t i n g S o u r c e F i e l d . g e t T e x t ( ) ) ;
JT e x t Ar ea Ope ra tor revi e wFie ld =
n ew JT ext A reaO p erat or( m a i n W i n d o w , n e w N a m e B a s e d C h o o s e r ( " r e v i e w " ) ) ;
as s e r tE qu als (" The r e sh o uld be a r e v i e w . " ,
"A t imel e ss c las s i c ! " ,
rev i ewFi e ld.g etT e x t ( ) ) ;

This drives us to implement the two methods we added to the interface earlier:
p u bl i c vo id se tR ati n gVal u eFie ld( i n t i ) {
ra t i n gV al ueF ie ld. s etSe l ecte dIn d e x ( i ) ;
}
p u bl i c vo id se tR ati n gSou r ceFi eld ( S t r i n g t h e S o u r c e ) {
ra t i n gS ou rce Fi eld . setT e xt(t heS o u r c e ) ;
}

That gets the test to pass. Things look much better now.

Amazon

Prev

don't be afraid of buying books

ADD A REVIEW
Add support to the GUI to enter a review as part of adding a rating.
There are no new tests for this task, just some enhancements of existing ones.
We start by extending te stAd ding Rati n g in T est M o vi eL is t Ed it o r since that behavior now
includes a review:
pub l ic vo id te stA dd ingR atin g() t h row s Dup l i ca te Mo v ie E xc ep t io n {
V e ctor ext en ded Ra ting s = star W a rs. g e tRa t i ng s( );
e x tende dRa ti ngs .a dd(n ew R atin g ( 2, " D ave " , " No t b ad . ") );
/ / ...
m o ckVie w.g et Rat in gVal ueFi eld( ) ;
c o ntrol .se tR etu rn Valu e(2, 1);
m o ckVie w.g et Rat in gSou rceF ield ( ) ;
c o ntrol .se tR etu rn Valu e("D ave" , 1);
m o ckVie w.g et Rat in gRev iewF ield ( ) ;
c o ntrol .se tR etu rn Valu e("N ot b a d .", 1 );

//...
}

This drives the addition of get Rati ngRe v i ewF i e ld( ) to M ov ie L is t Ed it o rV ie w :


Str i ng ge tRa ti ngR ev iewF ield ();

The test now fails because this new method is not being called. To get the test to pass we need to
extend a d dRa ti ng( ) in Mo vieL istE di to r :
pub l ic vo id ad dRa ti ng() {
i f (sel ect ed Mov ie != null ) {
int v alu e = v ie w.ge tRat ingV a l ueF i e ld( ) ;
Strin g s ou rce = vie w.ge tRat i n gSo u r ceF i e ld () ;
Strin g r ev iew = vie w.ge tRat i n gRe v i ewF i e ld () ;
selec ted Mo vie .a ddRa ting (new R ati n g (va l u e, s ou r ce , r ev i ew )) ;
view. set Ra tin gs (sel ecte dMov i e .ge t R ati n g s( )) ;
}
}

Green. Let's move on to the GUI test in Te s t Swi n g Mov i eL i st Ed i to r Vi ew :


pub l ic vo id te stA dd ingR atin gs()
{
J L istOp era to r m ov ieLi st =
ne w J Li stO pe rato r(ma inWi n d ow, n ew N a me Ba se d Ch o os er ( "m ov i eL is t ") );
m o vieLi st. cl ick On Item (0, 1);
J C omboB oxO pe rat or rat ingC ombo =
new JC om boB ox Oper ator (mai n W ind o w ,
new N a meB a s edC h o os er (" r at i ng va l ue ") ) ;
r a tingC omb o. set Se lect edIn dex( 3 ) ;
J T extFi eld Op era to r ra ting Sour c e Fie l d =
ne w J Te xtFi eldO pera t o r(m a i nWi n d ow ,

Next

n e w N a m eB as ed C ho o se r( " ra ti n gs ou r ce ") ) ;
r a tingS our ce Fie ld .ent erTe xt(" D a ve" ) ;
J T extAr eaO pe rat or rev iewF ield =
n ew JT ex tAre aOpe rato r ( mai n W ind o w , ne w N am e Ba se d Ch oo s er (" r ev ie w ") );
r e viewF iel d. set Te xt(" Not bad. " ) ;
J B utton Ope ra tor a ddRa ting Butt o n =
n ew JB utt on Oper ator (mai n W ind o w , n e w N am eB a se d Ch oo s er (" a dd ra t in g" ) );
a d dRati ngB ut ton .d oCli ck() ;

V e ctor rat in gs = star Wars .get R a tin g s ();


a s sertE qua ls ("s ta rWar s sh ould h ave 2 ra t i ng s. ", 2 ,r a ti ng s .s iz e () );
R a ting new Ra tin g = (R atin g)ra t i ngs . g et( 1 ) ;
a s sertE qua ls ("N ew rat ing has w r ong v alu e . ", 3, ne w Ra t in g. g et Va l ue () ) ;
a s sertE qua ls ("N ew rat ing has w r ong s our c e ." , "D a ve " ,n ew R at in g .g et S ou rc e () );
a s sertE qua ls ("N ew rat ing has w r ong r evi e w ." ,
" No t ba d.",
ne wRat ing. getR e v iew ( ) );
J L istOp era to r r at ings List =
ne w J Li stO pe rato r(ma inWi n d ow, n ew N a me Ba se d Ch o os er ( "r at i ng s" ) );
v e rifyR ati ng s(r at ings List , st a r War s ) ;

This requires the following method in S wi ng M o vie L i stE d it o rV ie w :


pub l ic St rin g get Ra ting Revi ewFi e l d() {
r e turn rat in gRe vi ewFi eld. getT e x t() ;
}

That does it. We're done. Figure 19.1 shows the final state of the application GUI.

Figure 19.1. Final form of the application GUI.

Amazon

Prev

don't be afraid of buying books

Next

RETROSPECTIVE
Adding reviews to the system would seem, at first glance, to be a large task. It touches all aspects of
the system from persistence to GUI. In fact, it was very simple and straightforward to add. This is one
of the benefits of Test-Driven Development, refactoring, and programming by intention: it is easier to
extend the code. Making a seemingly profound addition or change is no longer something to be feared.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 20. PROJECT RETROSPECTIVE


In this chapter we look back at the project, noting what worked well and what we might improve. This is
a valuable exercise if it is your first project or your one hundred and first. Another thing we do is have a
look at the design, both from an aesthetic point of view as well as from a more objective, qualitative
viewpoint. We'll collect some metrics, and see what the numbers look like.

Amazon

Prev

don't be afraid of buying books

Next

THE DESIGN
Figure 20.1 shows the class diagram. Our design is fairly simplewe don't see any classes that are
overly large or small, and none that are heavy on instance variables or operations. Classes are not
overly connected. Overall, the design looks well balanced. Even though this is a small project, we should
be able to maintain this level of design balance through slavishly practicing TDD and refactoring. In the
next sections we will look more objectively at the design by collecting metrics that give us a more
mathematical view of the design's quality. One thing to consider when looking at a design is how code is
distributed:
Are there any unusually large or small classes?
Are there any data or function classes? These are classes that contain mostly or completely
methods or instance variables, respectively.
Are classes overly connected to, or dependent on, each other?

Figure 20.1. The class diagram.


[View full size image]

To determine the answers to these questions, we can collect metrics on the code. We did this using

TogetherControlCenter. For each class, we collected five metrics: [1]


[1] Descriptions based on the TogetherControlCenter online documentation.

LOC: Lines of Code


The number of lines of code, excluding comments and blank lines.
This isn't a valid metric for measuring productivity or progress. Rather, it provides a high-level view of
where the code is. It gives a quick way to find a class that might be too large (and are possibly doing
too much and should be broken up) or too small (and are possibly not doing enough to justify their
autonomy and should be merged into another class). See Figure 20.2.

Figure 20.2. The plot of LOC per class.


[View full size image]

NOA: Number of Attributes


The number of attributes (instance variables) that a class has.

NOO: Number of Operations

The number of operations (methods), of all visibilities.


It is most interesting to compare the previous two metrics, as shown in Figure 20.3. An extreme
imbalance between these values is indication that there may be a problem. A class that is mostly or all
attributes could be a data class. Conversely, a class that is all or mostly operations could be a function
class. However, keep in mind that there should generally be more operations than attributes. Another
point to remember is that accessors and mutators (aka, getters and setters) will show up as operations.
This is misleading in that they are really just ways of getting to the attributes and do not often provide
any functionality. As such, they should be considered part of the attribute.

Figure 20.3. The plot of NOA and NOO per class.

LOCOM2: The Lack of Cohesion of Methods 2


The percentage of methods that do not access a specific attribute averaged over all attributes in the
class. A high value of cohesion (a low lack of cohesion) implies that the class is well designed. See Figure
20.4.

Figure 20.4. The plot of LOCOM2 and WMPC2 per class.


[View full size image]

All of these classes except M ovie List have a fairly high LOCOM2 value, indicating a highly cohesive
class. Furthermore, this indicates that Mov i e Lis t may not be a good class. This isn't a great surprise
since it is mostly a wrapper for the underlying collection of movies. Is this a problem? Maybe. I wouldn't
worry about it now. It serves its purpose for the moment, and may evolve into something better.
The thing to remember is that these are just metrics. They don't tell you anything conclusive about the
quality of your code. All they can do is give you indications of how things are going, and where to look
for potential problems.

WMPC2: Weighted Methods Per Class 2


This is intended to measure the complexity of a class, assuming that a class with more methods than
another is more complex, and that a method with more parameters than another is also likely to be
more complex. See Figure 20.4.
Our classes fare quite well according to this metric. A comparatively high value for WMPC2 could indicate
a class that is too complex and should be split up.
Based on these metrics, our project is in good shape. The M o vi e class is significantly more complex
than most, but is also very cohesive. Since M ovi e is arguably the core class in the system, this is not
unexpected.

Amazon

Prev

don't be afraid of buying books

Next

TEST VS. APPLICATION


An interesting thing to do is compare the amount of code we've written in the application to how much
we've written in the tests.
The graph in Figure 20.5 shows a comparison between the amount of application code and test code,
broken into GUI and non-GUI-related classes in each case. This is a very raw measurement, but it gives
an overall impression of how much we are testing. There should be at least as much test code as
application code, half as much is better.

Figure 20.5. Comparison of the amount of test and application code.

From the graph we can see that for the GUI there is a bit more test code than application code. GUIs
tend to be heavy in terms of the amount of code. Properly done (i.e., highly decoupled) GUI code is
primarily component creation, layout, event handling (deferring to the domain code), and presentation.
Since this is straightforward code it doesn't need to be tested as heavily as complex application logic.
We can also see that there is approximately twice as much test code as application code for the nonGUI portions. This is fine, but we should start to be concerned if the test:application ratio is much more
than 2:1.

Amazon

Prev

don't be afraid of buying books

Next

TEST QUALITY
Jester Results
Running Jester on our application code results in a variety of changes that do not cause any test to fail.
Once we trim off changes made to comments and the boilerplate (e.g., standard optimizations in
equ a ls(), to Str in g() , ha shCo de(), etc), we are left with a small number of potential
shortcomings, which are discussed in the following sections.

MovieListEditor
There are two areas where Jester found potential discrepancies:
1. The case when no movie is selected. This includes deselecting a selected movie (by calling
sele ct( -1 )) as well as performing operations on the selected movie when one isn't selected
(specifically upd at e() and addR a tin g ( )).
2. The case when the view returns n u l l as the file to load.
These cases are tested somewhat in the GUI tests which were not run as part of this analysis. These
results do point out problems with our test coverage which need to be remedied.

XMLMovieListWriter
This is an interesting case which can actually lead to the simplifying of the code. If you recall, before we
added support for reviews, the XML element ra t i ng had no text, only attributes. When we added
reviews, we placed the review in the text for the r ati n g tag. We chose to add a second output format
to handle the case where the Ra ting being written had a review:
pri v ate S tri ng co mm onRa ting Form a t =
" <ra ti ng va lue= \"{0 ,num b e r,i n t ege r } \" s ou r ce = \" {1 } \" ";
pri v ate M ess ag eFo rm at r atin gFor m a tWi t h out R e vi ew =
n ew Me ssa ge Form at(c ommo n R ati n g For m a t + " / >" ) ;
pri v ate M ess ag eFo rm at r atin gFor m a tWi t h Rev i e w =
n ew Me ssa ge Form at(c ommo n R ati n g For m a t + "> { 2} < /r at i ng >" ) ;

Here is the code that writes a Ra ting using these formats:


pri v ate v oid w rit eR atin g(Ra ting r ati n g ToW r i te ) th r ow s I OE x ce pt i on {
O b ject[ ] a rg s = { new Int eger ( r ati n g ToW r i te .g et V al u e( )) ,
rat ingT oWri t e .ge t S our c e () ,
rat ingT oWri t e .ge t R evi e w () } ;
M e ssage For ma t f or matT oUse ;
i f (rat ing To Wri te .has Revi ew() ) {
forma tTo Us e = r atin gFor matW i t hRe v i ew;
} else {
forma tTo Us e = r atin gFor matW i t hou t R evi e w ;
}
d e stina tio n. wri te (for matT oUse . f orm a t (ar g s )) ;
}

Jester found that it could change the condition of the if to be always tr u e and the tests still passed.
This implies that we can always simply use the first option of the if statement. Why is that? Remember
that the tests compare XML structure, not text. The structure is the same in each case when there is no
revieweither an element with no text, or an element with an empty text. These are the same once it is
parsed.
So, let's simplify wr ite Ra ting () by removing the redundancy:
pri v ate M ess ag eFo rm at r atin gFor m a t =
n ew Me ssa ge Form at(" <rat i n g v a l ue= \ " {0 ,n um b er , in te g er }\ " " +
" sour ce=\ " { 1}\ " > {2} < / ra ti ng > ") ;
pri v ate v oid w rit eR atin g(Ra ting r ati n g ToW r i te ) th r ow s I OE x ce pt i on {
O b ject[ ] ar gs = { ne w In tege r ( rat i n gTo W r it e. ge t Va l ue () ) ,
ra ting ToWr i t e.g e t Sou r c e( ),
ra ting ToWr i t e.g e t Rev i e w( ) };
d e stina tio n. wri te (rat ingF orma t . for m a t(a r g s) );
}

Much simpler and cleaner. This is an unexpected benefit of running Jester on our code: the uncovering of
some duplication.

RatingRenderer
The issue here is due to a shortcut we took when we skipped testing the color based on selected status.
If you recall, we did that because this code was identical to the code in the movie renderer which we
tested more thoroughly. Maybe we shouldn't have taken that shortcut?
There are a few places where we could use an additional test or two. We didn't write any tests related
to problems that can occur when opening files, for example.

NoUnit Results
It took a while to work through several bugs in NoUnit that prevented it from running on our project
code. These were all known bugs with workarounds available. After making the required changes, we had
NoUnit running and providing results. These results were interesting, but not entirely surprising. The
output of NoUnit's analysis is too large to reproduce here, but you can see it on our Web site with other
support material for this book[URL 65].
The anonymous subclasses (e.g., event handlers) and component construction methods in the GUI are
tested either not at all, or not very well. These functions are typically called from the GUI framework,
and not our test code. In the GUI-based test cases they are called indirectly from the test case, again

through the Swing framework.


Likewise, the custom renderers are not well tested. They are mostly boilerplate and their methods are
called from the GUI framework.
Other methods that tend not to be well tested are support methods such as t o St ri n g( ), eq u al s( ) ,
and hash C ode () . Remember that we didn't explicitly test the latter two. We used Eq ua l sT es t er to
do that, so NoUnit didn't pick up the calls to them.
As we scan through the NoUnit output, we can see that the primary functional methods of the core
classes are all marked green, indicating that they are tested directly.

Clover Results
We easily ran Clover on the project, a few simple additions to the Ant build script. Results, shown in
Figure 20.6, were good. Areas not tested were primarily methods such as t oS tr i ng () and
has h code( ). One nice feature of Clover that we didn't make use of is the ability to mark sections of
code that should be ignored when calculating coverage. This allows us to mark methods like
toS t ring( ) and not have them included in the coverage results. In some cases the untested code was
error handling code that needs to be addressed. For example, there was no testing of error handling on
the read and write code.

Figure 20.6. Results from Clover.


[View full size image]

Comments on the Test Quality Results


Our tests are in good shape. Jester found a couple of places where we could extend our test coverage
one where we were lazy and one where we missed testing for specific GUI behavior (the deselect case).
The latter would best be dealt with by specifying what should happen when list items (both movie and
rating) are deselected, and hence there is no selection. With that story in place, we would write tests
and evolve the behavior to match. Jester also found an opportunity for simplification. This in itself is
reason enough to use it occasionally.
NoUnit's report was reassuring: all of the important functionality was directly tested. Things found not to
be tested were largely because they were called indirectly through Jemmy and the Swing framework.
Thus, while NoUnit didn't detect the calls, they are being tested.
Likewise, the results from Clover weren't bad. They could be better, though. We can learn from this. The
biggest problem was that we checked test coverage and quality once at the end. This is something that
really should be done regularly, preferably as part of the build process. By keeping our eye on this
feedback we can make corrections while they are minor. The same applies to running audits and metrics
on the project. If we have an automated tool to do this (which is really a requirement), there's no
excuse not to be doing it frequently. This, again, lets us take corrective action before we have a
significant problem to deal with.

Amazon

Prev

don't be afraid of buying books

Next

OUR USE OF MOCKS


We've only used mocks for the interface view. They served us very well in that case, but could we have
used them in other places to make our work easier? Likely. One possible use would be in the file loading
and saving tests. There we could have used mock files, setting required return values for load tests and
expectations for save tests. As the application grows and the time that the entire test suite takes to run
increases, we may revisit that issue and add mocks to some of the tests, such as the file-related cases.
There are dangers in overusing mocks. The primary one is that it can make refactoring more difficult
since mocks of the refactored classes need to be modified to match the new behavior. Hand-built mocks
(whether using MockObjects with or without MockMaker, or not) need to be maintained like any other
class in the system. On the other hand, using EasyMock can lead to overspecification in tests if you
aren't careful.
Like all powerful tools, mocks are incredibly helpful and beneficial when used responsibly. Anything used
to excess has the potential to create problems. Mocks are no different.

Amazon

Prev

don't be afraid of buying books

Next

GENERAL COMMENTS
One purpose in writing this book was to aid you, the reader, in your quest to improve your skills and your
code. Another purpose was to let me think through the issues involved in TDD, mainly by having to express
them clearly and understandably. I've written this section very near the end of the process, and have taken
the opportunity to read through the book, looking for things that I've learned or become more convinced of.
I have, for the most part, resisted the temptation to go back and revise the code in this book. As such, it
serves as a fairly accurate picture of how the development went. And as such, there are a few things I
would have done differently.
One thing is that I would have used mocks more during the development of the GUI. This was a very
simple application so we incurred no real penalty from using the real application code behind the GUI in our
tests. Had it been a more involved application, the tests would have been noticably slow. Mocks would help
avoid that.
Another thingone more obvious and somewhat more embarrassing in hindsightjumped out and hit me as
I read through the book. Think back to the first task, specifically the test for containment in the list. I'll
replicate it here for convenience, hoping that my editors don't refactor away the duplication:
pu bli c void te st Con te nts( ) {
ass e rtTru e(" Li st sh ould con tain s tar W a rs. " , m ov ie L is t .c on t ai ns ( st ar W ar s) ) ;
ass e rtTru e(" Li st sh ould con tain s tar T r ek. " , m ov ie L is t .c on t ai ns ( st ar T re k) ) ;
ass e rtFal se( "L ist s houl d no t co n t ain s tar g at e ." , m ov i eL is t .c on t ai ns ( st ar g at e) ) ;
}

There are two things wrong with this test:


1. Checking for the containment of two movies is redundant and doesn't add much, if anything.
2. There are three assertions in this single test. I feel even more strongly now that you should always
strive for a single assertion per test. In a perfect world, s et U p( ) would build the fixture, and each
test method would make a single assertion about it. Test methods this simple cease to require the
message parameter for the assert.
If I were to yield to temptation, I would replace the above t es t Co nt a in s () with:
pu bli c void te st Con ta ins( ) {
ass e rtTru e(m ov ieL is t.co ntai ns(s t a rWa r s ));
}
pu bli c void te st Doe sN otCo ntai n() {
ass e rtFal se( mo vie Li st.c onta ins( s t arg a t e)) ;
}

Amazon

Prev

don't be afraid of buying books

Next

DEBUGGING
We didn't talk about the time we spent laboriously single-stepping through our code searching for bugs.
Or about painstakingly poking through data structures looking for that bad value. We didn't talk about it
because we didn't do it. The write-up of the project reflects exactly what happened, down to each line of
code we wrote. We did omit most of the times we ran the tests... because we did it constantly (and
painlessly). We ran the tests after every change, after every compile. And many times just to be sure.
So... what about the debugger? Well, honestly, we never used it. I was telling someone about Eclipse
and they asked what the debugger was like. After a moment of thought, I said something like, "Pretty
good...I guess...I haven't used it more than a couple times...and that was mainly out of curiosity." I'd
been using Eclipse almost daily for over a year at that point. He couldn't believe it.
So what's going on here? Well, once you get good at TDD you don't have much use for a debugger. This
is because you are working in very small increments, adding very little code at a time.
You write a test, then write just enough code to make it pass. Once it passes, you're done with that
chunk of work. When you're refactoring, you start with all the tests passing. After each small change
you rerun the tests (likely, a localized subset of the entire suite). If they don't all pass, the problem is
in that last little change you made. Because your tests are watching your code for bugs in such tiny
increments, you never have much trouble finding a bug. It's hard to believe how well this works until
you try it yourself.
Even if you were feeling brave (or foolhardy) and made a big change between tests, you simply back
that change out and go through it again in smaller steps. . . running the tests after each step. (This is a
common and humbling lesson when you are first learning TDD.)
So, yes, TDD sounds like a lot of extra work upfront (as my friend above commented), and certainly it is
more work upfront than many of us are accustomed to. But it actually saves much more work on the
backend (debugging) than it costs upfront. As nonintuitive as it may be, TDD saves you time. You'll find
that once you acquire the feel for it, you'll work much faster than when you were using the code and
debug approach. And you don't have to wait weeks to discover this higher speed: it will be obvious after
just a few hours, and certainly after a couple of days. You just won't be spending all that backend time
sleuthing for bugs. You'll be making rapid, steady, bug-free progress, task after task. As I keep saying,
you will find it exhilarating.

Amazon

Prev

don't be afraid of buying books

LIST OF TESTS
Test 1: defined on Pg. 207 and implemented on Pg. 207
An empty list should have a size of zero.

Test 2: defined on Pg. 207 and implemented on Pg. 209


Adding a movie to an empty list should result in a list with a size of one.

Test 3: defined on Pg. 207 and implemented on Pg. 211


Adding two movies to an empty list should result in a list with a size of
two.

Test 4: defined on Pg.207 and implemented on Pg. 214


If we add a movie to a list, we should be able to ask if it's there and
receive a positive response. Conversely, we should receive a negative
response when we ask about a movie that we haven't added.

Test 5: defined on Pg. 219 and implemented on Pg. 219


The logical layer should send the appropriate list of movies to the view for
display.

Next

Test 6: defined on Pg. 219 and implemented on Pg. 221


The GUI should have a listbox and should display a list of movies in it as
requested.

Test 7: defined on Pg. 226 and implemented on Pg. 226


When the logical layer is asked to add a movie, it should request the
required data from the view and update the movie list to include a new
movie based on the data provided.

Test 8: defined on Pg. 226 and implemented on Pg. 229


The GUI should have a field for the movie name and an add button. It
should answer the contents of the name field when asked, and request
that the logical layer add a movie when the add button is pushed.

Test 9: defined on Pg. 235 and implemented on Pg. 235


Changing the name of a movie results in it using the new name hereafter.

Test 10: defined on Pg. 235 and implemented on Pg. 236


A movie can't be constructed with a null name.

Test 11: defined on Pg. 235 and implemented on Pg. 236


A movie can't be constructed with an empty name.

Test 12: defined on Pg. 235 and implemented on Pg. 237


A movie can't be renamed to a null name.

Test 13: defined on Pg. 235 and implemented on Pg. 238


A movie can't be renamed to an empty name.

Test 14: defined on Pg. 239 and implemented on Pg. 239


Indicating, to the logical layer, that a selection is made from the list
causes the view to be given a value for the name field, that is, the
selected movie's name.

Test 15: defined on Pg. 239 and implemented on Pg. 241


Selecting from the list causes the name field to be filled in with the
selected movie's name.

Test 16: defined on Pg. 239 and implemented on Pg. 242

When an update is requested, the selected movie is renamed to whatever


is answered by the view as the new name.

Test 17: defined on Pg. 239 and implemented on Pg. 244


When the update button is pushed, the selected movie is renamed to
what-ever is in the name field.

Test 18: defined on Pg. 247 and implemented on Pg. 247


Attempting to add a duplicate movie throws an exception and leaves the
list unchanged.

Test 19: defined on Pg. 247 and implemented on Pg. 248


Asking the movielist to rename a movie results in its name being
changed.

Test 20: defined on Pg. 247 and implemented on Pg. 251


Asking the logical layer to add a duplicate movie causes it to inform the
presentation layer that the operation would result in a duplicate.

Test 21: defined on Pg. 247 and implemented on Pg. 253

Asking the logical layer to update a movie that would result in a duplicate
causes it to inform the presentation layer that the operation would result
in a duplicate.

Test 22: defined on Pg. 247 and implemented on Pg. 255


Trying to add a movie that is the same as one in the list results in the
display of a "Duplicate Movie" error dialog.

Test 23: defined on Pg. 247 and implemented on Pg. 256


Trying to rename a movie to the name of one in the list results in the
display of a "Duplicate Movie" error dialog.

Test 24: defined on Pg. 261 and implemented on Pg. 261


An unrated movie answers negative when asked if it is rated.

Test 25: defined on Pg. 261 and implemented on Pg. 261


A rated movie answers positive when asked if it is rated, and it can
answer its rating when asked.

Test 26: defined on Pg. 261 and implemented on Pg. 263


Asking an unrated movie for its rating throws an exception.

Test 27: defined on Pg. 264 and implemented on Pg. 264


When asked for the renderer component, the renderer returns itself.

Test 28: defined on Pg. 264 and implemented on Pg. 265


When given a movie to render, the resulting test and rating image
corresponds to the movie being rendered.

Test 29: defined on Pg. 264 and implemented on Pg. 266


When rendering an unselected item, the renderer uses its list's unselected
colors.

Test 30: defined on Pg. 264 and implemented on Pg. 267


When rendering a selected item, the renderer uses its list's selected
colors.

Test 31: defined on Pg. 270 and implemented on Pg. 270


Selecting a movie updates the rating in the GUI.

Test 32: defined on Pg. 270 and implemented on Pg. 272


Updating a movie changes its rating if a different rating was selected for
it.

Test 33: defined on Pg. 270 and implemented on Pg. 273


Selecting a movie from the list updates the displayed rating.

Test 34: defined on Pg. 270 and implemented on Pg. 275


Updating a movie in the GUI changes its rating if a different rating was
selected for it, and updates the display accordingly.

Test 35: defined on Pg. 277 and implemented on Pg. 277


A movie that hasn't explicitly been given a category should answer that it
is uncategorized when asked for its category.

Test 36: defined on Pg. 277 and implemented on Pg. 278


If a movie is given a category when it is created, it answers that when
asked for its category.

Test 37: defined on Pg. 277 and implemented on Pg. 279


Trying to create a movie with an invalid category (i.e., not from the
predefined set) throws an exception.

Test 38: defined on Pg. 281 and implemented on Pg. 282


Telling the logical layer that a movie is selected causes the presentation
layer to be told the category to display.

Test 39: defined on Pg. 281 and implemented on Pg. 283


Selecting a movie in the GUI causes the category field to be updated.

Test 40: defined on Pg. 285 and implemented on Pg. 285


Telling the logical layer to update and providing it with data that indicates
a category change results in the GUI layer being given a new set of
movies with that change reflected.

Test 41: defined on Pg. 285 and implemented on Pg. 286


Selecting a movie from the list, changing the value of the category, and
pressing Update updates the data for that movie. When that movie is
selected again, the new category is displayed.

Test 42: defined on Pg. 289 and implemented on Pg. 290


Requesting a sublist filtered on a specific category answers a list
containing all movies of that category, and only those movies.

Test 43: defined on Pg. 292 and implemented on Pg. 293


Asking for a subset for the ALL category answers the original list.

Test 44: defined on Pg. 293 and implemented on Pg. 295


When the logical layer is told to filter on a specific category, the
presentation layer is given a new list to display containing movies for the
specified category.

Test 45: defined on Pg. 293 and implemented on Pg. 296


Telling the logical layer to select a specific movie in a filtered list rather
than the complete list from actually selects the appropriate movie, in spite
of being selected from a sublist.

Test 46: defined on Pg. 293 and implemented on Pg. 297


Telling the logical layer to update a specific movie in a filtered list rather
than from the complete list actually updates the appropriate movie
properly.

Test 47: defined on Pg. 294 and implemented on Pg. 298


A rename performed in the context of the logical layer is done in the
underlying full list; that is, potential duplicates in the full list are detected.

Test 48: defined on Pg. 294 and implemented on Pg. 299


Selecting a category to filter on in the GUI causes the displayed list of
movies to update accordingly.

Test 49: defined on Pg. 300 and implemented on Pg. 301


Changing the category of a movie in a filtered list causes that movie to
be removed from the list. If that category is filtered on, that movie will
be in the list.

Test 50: defined on Pg. 309 and implemented on Pg. 309


Writing an empty list to a stream writes nothing.

Test 51: defined on Pg. 309 and implemented on Pg. 310


Writing a list of one movie should write that movie to the stream in the
format <name> | <category> | <rating>.

Test 52: defined on Pg. 309 and implemented on Pg. 312


Writing a list containing several movies writes each movie to the stream,
one per line, in the format of the previous test.

Test 53: defined on Pg. 313 and implemented on Pg. 313


Telling the logical layer to "saveas" causes it to ask the presentation
layer to specify a file into which it writes the movie list.

Test 54: defined on Pg. 313 and implemented on Pg. 316


Selecting "Save As" from the "File" menu prompts for a file using the
standard file chooser. The list is written into the selected file.

Test 55: defined on Pg. 313 and implemented on Pg. 321


If the file selection is cancelled, nothing is written to any file.

Test 56: defined on Pg. 322 and implemented on Pg. 322


Telling the logical layer to "Save" causes it to save the current list to the
same file as the previous "Save As" operation.

Test 57: defined on Pg. 322 and implemented on Pg. 325


Selecting "Save" from the "File" menu causes the list to be written into
the previously selected file.

Test 58: defined on Pg. 329 and implemented on Pg. 329


Reading from an empty stream results in an empty list of movies.

Test 59: defined on Pg. 329 and implemented on Pg. 330


Reading from a stream containing the data for a single movie results in a
list containing the single movie.

Test 60: defined on Pg. 329 and implemented on Pg. 332


Reading from a stream containing the data for several movies results in a
list containing those movies.

Test 61: defined on Pg. 333 and implemented on Pg. 333


With the list empty, telling the logical layer to load from a file that
contains data for a set of movies results in the list containing those
movies.

Test 62: defined on Pg. 333 and implemented on Pg. 335

Choosing "Load" from the "File" menu and selecting a file containing a
specific set of movie data causes the corresponding movies to be placed
in the list.

Test 63: defined on Pg. 333 and implemented on Pg. 336


With a set of movies loaded, cancelling the load of a different set leaves
the originally loaded set unchanged.

Test 64: defined on Pg. 339 and implemented on Pg. 340


Comparing two movies with the same name, based on name, should
answer 0.

Test 65: defined on Pg. 339 and implemented on Pg. 340


Comparing two movies, the first having a lexically smaller name, based on
name, should answer a negative integer.

Test 66: defined on Pg. 339 and implemented on Pg. 341


Comparing two movies, the first having a lexically greater name, based on
name, should answer a positive integer.

Test 67: defined on Pg. 339 and implemented on Pg. 342

Comparing two movies with the same rating, based on rating, should
answer 0.

Test 68: defined on Pg. 339 and implemented on Pg. 342


Comparing two movies, the first having a lower rating, based on rating,
should answer a negative integer.

Test 69: defined on Pg. 339 and implemented on Pg. 342


Comparing two movies, the first having a higher rating, based on rating,
should answer a positive integer.

Test 70: defined on Pg. 339 and implemented on Pg. 342


For the purposes of comparison, an unrated movie always has a lower
rating.

Test 71: defined on Pg. 343 and implemented on Pg. 343


Sorting an empty list has no effect on it.

Test 72: defined on Pg. 343 and implemented on Pg. 344


Sorting a list, by name, that is already sorted has no effect on it.

Test 73: defined on Pg. 343 and implemented on Pg. 345


Sorting a list, by name, that is in reverse order puts it in order.

Test 74: defined on Pg. 343 and implemented on Pg. 347


Sorting a list, by rating, that is already sorted has no effect on it.

Test 75: defined on Pg. 343 and implemented on Pg. 347


Sorting a list, by rating, that is in reverse order puts it in order.

Test 76: defined on Pg. 343 and implemented on Pg. 347


Sorting a list, by rating, that has duplicates puts it in order; the
duplicates being in their original order relative to one another.

Test 77: defined on Pg. 343 and implemented on Pg. 348


Sorting a randomly ordered list by name puts it in order, by name.

Test 78: defined on Pg. 343 and implemented on Pg. 348


Sorting a randomly ordered list by rating puts it in order, by name.

Test 79: defined on Pg. 349 and implemented on Pg. 349


Asking the logical layer to sort its list causes the associated view's list to
be updated to one that is in order (order depends on what is being sorted
on).

Test 80: defined on Pg. 350 and implemented on Pg. 351


Selecting "Sort by name" from the "View" menu causes the displayed list
to be sorted by name.

Test 81: defined on Pg. 350 and implemented on Pg. 352


Selecting "Sort by rating" from the "View" menu causes the displayed list
to be sorted by rating.

Test 82: defined on Pg. 355 and implemented on Pg. 356


Constructing a Movie with no arguments creates an unrated Movie.

Test 83: defined on Pg. 355 and implemented on Pg. 356


Add a single rating to an unrated movie. Asking for the average rating
answers the rating just added.

Test 84: defined on Pg. 355 and implemented on Pg. 357


Add several ratings: 3, 5, and 4. Asking for the average rating answers 4.

Test 85: defined on Pg. 355 and implemented on Pg. 358


Add several ratings: 3, 5, 5, and 3. Asking for the average rating answers
4.

Test 86: defined on Pg. 355 and implemented on Pg. 360


Writing movies uses for format: <name> | <category> | <total rating> |
<number of ratings>.

Test 87: defined on Pg. 355 and implemented on Pg. 361


Reading movies uses for format: <name> | <category> | <total rating>
| <number of ratings>.

Test 88: defined on Pg. 362 and implemented on Pg. 362


A rating without a specified source should be anonymous.

Test 89: defined on Pg. 362 and implemented on Pg. 366


If we add a rating with a source to a movie, we should be able to later
retrieve the source associated with the rating.

Test 90: defined on Pg. 370 and implemented on Pg. 371


Writing an empty list outputs an empty <movielist> pair.

Test 91: defined on Pg. 370 and implemented on Pg. 372


Writing a list containing one movie outputs in the adopted XML format.

Test 92: defined on Pg. 370 and implemented on Pg. 375


Writing a list containing one movie with multiple ratings outputs in the
adopted XML format.

Test 93: defined on Pg. 370 and implemented on Pg. 375


Writing a list containing multiple movies outputs in the adopted XML
format.

Test 94: defined on Pg. 370 and implemented on Pg. 379


Reading an empty <movielist> pair results in an empty list.

Test 95: defined on Pg. 370 and implemented on Pg. 379


Reading an appropriate XML stream containing a single movie definition
results in a list containing the single movie defined in the stream.

Test 96: defined on Pg. 370 and implemented on Pg. 383


Reading an appropriate XML stream containing more than one movie
definition results in a list containing the movies defined in the stream.

Test 97: defined on Pg. 390 and implemented on Pg. 390


Telling the logical layer to select a movie and add a rating to it, and
providing specific rating value and source, causes that rating to be added
to the movie that was selected.

Test 98: defined on Pg. 390 and implemented on Pg. 393


Selecting a movie, setting the rating value, and source to specific values
and pressing the "Add Rating" button causes that rating to be added to
the selected movie.

Test 99: defined on Pg. 397 and implemented on Pg. 398


A rating created without a review should answer an empty string when
asked for its review.

Test 100: defined on Pg. 397 and implemented on Pg. 399


A rating created with a review should answer that string when asked for
its review.

Test 101: defined on Pg. 400 and implemented on Pg. 400


If a rating has an attached review, the review is saved as the text of the
<rating> element.

Test 102: defined on Pg. 401 and implemented on Pg. 402


Reading an XML stream that contains text for a rating element results in
that rating having an attached review.

Test 103: defined on Pg. 403 and implemented on Pg. 403


Telling the logical layer that a rating is selected that has no review results
in the view being told to put an empty string in the review field.

Test 104: defined on Pg. 403 and implemented on Pg. 404


Telling the logical layer that a rating is selected that has a review results
in the view being told to put the review string in the review field.

Test 105: defined on Pg. 407 and implemented on Pg. 407


Selecting a rating that doesn't have a review puts an empty string in the
review field.

Test 106: defined on Pg. 407 and implemented on Pg. 409


Telling the logical layer that a rating is selected that has a review results
in the view being told to put the review string in the review field.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
In this chapter we've taken a step back and had a look at the project we just did. We generated a class
diagram, collected some metrics, and analyzed the quality and coverage of our tests. Our code fared
well. What does this indicate? Are we especially good programmers? Maybe. It's nice to think so.
However, most of the credit goes to how we worked. Test-Driven Development makes us work in small
steps. This focuses us on one small problem at a time. This in turn allows us to design and code the
simplest solution that we can find. This keeps our design simple. Constant refactoring keeps the
combination of each simplest design decision as simple as possible. These techniques let us evolve a
simple, clean, yet wonderfully expressive design. Furthermore, the design evolves very quickly and has
an incredibly high level of quality.

Amazon

Prev

don't be afraid of buying books

Part IV: XUNIT FAMILY MEMBERS


Up to this point we have focused exclusively on Java. In this part of the book we take a
moment to look briefly at some of the other members of the xUnit family. There are too
many now to all be considered, so we will discuss only those most relevant to today's
programmer.
The ordering of these chapters is arbitrary, as they are all mutually independent. The
structure of each chapter is the same. We start by looking at architectural and API issues
specific to the family member. The bulk of each chapter works through the same task
using the language of the chapter.
Any special tricks or techniques available or applicable to each language are discussed as
we go.
The task we'll use is the first task of the first story in the example project:
Make a container for movies with a way to add to it. Doesn't have to be ordered or
sorted.
To implement this we'll need these tests:
Test 1. An empty list should have a size of zero.

Test 2. Adding a movie to an empty list should result in a list with a size
of one.

Test 3. If we add a movie to a list, we should be able to ask if it's there


and receive a positive response.

Test 4. Asking about the presence of a movie that wasn't added should
result in a negative response.

In Chapter 26, the chapter on Visual Basic (it being a different sort of beast), Kay takes
a slightly different approach. As in most cases, the differences are themselves instructive.

Next

Amazon

Prev

don't be afraid of buying books

Next

Chapter 21. RUBYUNIT


This chapter presents RubyUnit, the xUnit family member for the increasingly popular Ruby Programming
language. The examples in this chapter are written using Ruby 1.6.7.

Amazon

Prev

don't be afraid of buying books

Next

THE FRAMEWORK
You can download RubyUnit from [URL 46]. RubyUnit includes all the expected asserts, such as:
assert(boolean)

assert equal(expected, actual)

assert not nil(object)

As well, there are some Ruby-specific assertions, for example:


assert respond to(method, object) asserts that the ob je c t responds to me th o d.

assert match(string, regex) asserts that s t r ing matches against the regular
expression reg ex .

All assertions take an optional final message parameter.


In addition to having to requ ire the classes being tested, test case classes must include the following:
req u ire " run it /te st case "
req u ire " run it /cu i/ test runn er"
req u ire " run it /te st suit e"

Additionally, our test case must extend RU N I T:: T e stC a se . A useful idiom is to use a template similar
to the following for test cases. As when using JUnit, this allows the test case class to be run in order to
run its tests. Here's the template:
req u ire " run it /te st case "
req u ire " run it /cu i/ test runn er"
req u ire " run it /te st suit e"
cla s s Tes tSo me thi ng
< R U NIT:: Tes tC ase
R U NIT:: CUI :: Tes tR unne r.ru n(Te s t Som e t hin g . su it e)

end

Amazon

Prev

don't be afraid of buying books

Next

EXAMPLE
Test 1: An empty list should have a size of zero.
c la s s Te stM o vieL i st
< R U NI T: :Te s tCas e
d e f te stE m ptyL i stSi z e
@l is t = Movi e List . new
as se rt e qual ( 0, @ l ist. size )
end
e nd
c la s s Mo vie L ist
e nd

This fails with:


T es t Mo vi eLi s t#te s t_em p ty_m ovie l i s t F .
T im e : 0. 004 0 3
F AI L UR ES !!!
T es t R es ult s :
Ru n : 1/ 1(1 asse r ts) F ailu res: 1 E r r o r s : 0
F ai l ur es : 1
T es t Mo vi eLi s t.rb : 10:i n 'te st_e m p t y _ m o v i e l i s t ' ( T e s t M o v i e L i s t ) :
e x pe ct ed: < 0> b u t wa s :<ni l> ( R U N I T : : A s s e r t i o n F a i l e d E r r o r )
fro m Tes t Movi e List .rb: 1 3

Now we need to fake it:


c la s s Mo vie L ist
d e f si ze
0
end
e nd

Green.

Test 2: Adding a movie to an empty list should result in a list with a size
of one.
The next test is:
d ef te st One M ovie L istS i ze
@ l is t = M o vieL i st.n e w
@ s ta rW ars = Mo v ie.n e w("S tar W a r s " )
@ l is t. add ( @sta r Wars )
a s se rt eq u al(1 , @li s t.si ze)
e nd

This requires that we create a stub M ov i e class:


c la s s Mo vie
d e f in iti a lize ( aNam e )
end
e nd

We also need to stub an a d d method for M o v i e L i s t :


d ef ad d (mo v ieTo A dd)
e nd

We run the test and it fails because we've faked M o v i e L i s t . s i z e ( ) to return 0 . Now we can fake it
one step better by counting the number of movies that are added:
c la s s Mo vie L ist
d e f in iti a lize
@n um ber O fMov i es = 0
end
d e f si ze
@n um ber O fMov i es
end
d e f ad d ( m ovie T oAdd )
@n um ber O fMov i es + = 1
end
e nd

Our tests pass and there isn't anything in M o v i e L i s t to clean up.

Test 3: If we add a movie to a list, we should be able to ask if it's there


and receive a positive response.
This test should verify that the movie that was added is really there; again we'll fake it at first:
d ef te st One M ovie L istC o ntai ns
@ l is t = M o vieL i st.n e w
@ s ta rW ars = Mo v ie.n e w("S tar W a r s " )
@ l is t. add ( @sta r Wars )
a s se rt (@l i st.c o ntai n sMov ieNa m e d ? ( " S t a r W a r s " ) )
e nd

We next need a stub c o ntai n sMov ieN a m e d ? method in M o v i e L i s t that fakes it for the purposes of
this test:
d ef co nt ain s Movi e Name d ? (a Name )
t r ue
e nd

OK, now we have some duplication in T e s t M o v i e L i s t . The methods t e s t O n e M o v i e L i s t S i z e and


t es t On eM ovi e List C onte n ts have a common fixture that should be extracted. But wait, what about
the fixture for test E mpty L istS ize? It's different than the fixture used by the other tests. We not
only need to extract the fixtures, but we also need to split the test case in two. Here's the result:
c la s s Te stE m ptyM o vieL i st
< R U NI T: :Te s tCas e
d e f se tup
@l is t = Movi e List . new
end
d e f te stS i ze
as se rt e qual ( 0, @ l ist. size )
end
R U NI T: :CU I ::Te s tRun n er.r un(T e s t E m p t y M o v i e L i s t . s u i t e )
e nd
c la s s Te stO n eIte m Movi e List
< R U NI T: :Te s tCas e
d e f se tup
@l is t = Movi e List . new
@s ta rWa r s = M ovie . new( "Sta r W a r s " )
@l is t.a d d(@s t arWa r s)
end
d e f te stS i ze
as se rt e qual ( 1, @ l ist. size )
end
d e f te stC o ntai n s
as se rt( @ list . cont a insM ovie N a m e d ? ( " S t a r W a r s " ) )
end
R U NI T: :CU I ::Te s tRun n er.r un(T e s t O n e I t e m M o v i e L i s t . s u i t e )
e nd

Notice how we've renamed the test methods when we refactored to remove the fixture-related prefix.
Having a prefix like that is a good indication that we're mixing fixtures.

Test 4: Asking about the presence of a movie that wasn't added should
result in a negative response.
Now we need to test that a movie that we didn't add is, in fact, not reported as being in the list:
d ef te st Doe s NotC o ntai n
a s se rt (!@ l ist. c onta i nsMo vieN a m e d ? ( " S t a r T r e k " ) )
e nd

This test drives us to move from fake to make in terms of keeping track of what movies have been
added to the list:
c la s s Mo vie L ist
d e f in iti a lize
@m ov ies = Ha s h.ne w
@n um ber O fMov i es = 0
end
d e f si ze
@n um ber O fMov i es
end
d e f ad d ( m ovie T oAdd )
@m ov ies . stor e (mov i eToA dd.n a m e , m o v i e T o A d d )
@n um ber O fMov i es + = 1
end

d e f co nta i nsMo v ieNa m ed? (aNa m e )


@m ov ies . incl u de?( a Name )
end
e nd

We also need to extend Mo vie to retain its name:


c la s s Mo vie
a t tr r ead e r :n a me
d e f in iti a lize ( aNam e )
@n am e = aNam e
end
e nd

As you can see, we are now duplicating the information about how many movies have been added. We
keep track of it explicitly (as before) and it is also available as an attribute of the H a s h . To remove it,
we first change size to return the information fetched from the H a s h :
d ef si ze
@ m ov ie s.s i ze
e nd

The tests still pass, so we can remove all of the code related to explicitly counting how many movies
have been added. Now Movi e List is:
c la s s Mo vie L ist
d e f in iti a lize
@ mo vi es = Has h .new
end
d e f si ze
@m ov ies . size
end

d e f ad d ( m ovie T oAdd )
@m ov ies . stor e (mov i eToA dd.n a m e , m o v i e T o A d d )
end
d e f co nta i nsMo v ieNa m ed? (aNa m e )
@m ov ies . incl u de?( a Name )
end
e nd

Amazon

Prev

don't be afraid of buying books

Next

Chapter 22. SUNIT


This chapter presents SUnit, the xUnit family member for the Smalltalk programming language. SUnit is
the granddaddy of xUnit family. It was first described by Beck in [7]. The example in this chapter uses
VisualWorks Smalltalk 7 from Cincom. However, I avoided doing any GUI work, so this code should run
(with possible minor tweaks) on any Smalltalk system.

Amazon

Prev

don't be afraid of buying books

Next

THE FRAMEWORK
The SUnit framework is included in the recent releases of VisualWorks Smalltalk [URL 43], Squeak [URL
44], and probably others that I'm not as familiar with. Versions for other varieties of Smalltalk can be
found at the xprogramming.com Website [URL 11].
This framework takes advantage of Smalltalk's blocks to simplify the assertion mechanism. There are
several assertion calls which we will briefly list here.

Amazon

Prev

don't be afraid of buying books

BOOLEAN ASSERTIONS
assert: aBoolean asserts that aB o o lea n is t r u e.

deny: aBoolean asserts that aBo o l ean is fa l s e.

Next

Amazon

Prev

don't be afraid of buying books

BLOCK ASSERTIONS
should: aBlock asserts that a Bl oc k val u e evaluates to t ru e .

shouldnt: aBlock asserts that aB lo c k va l u e evaluates to fa l se .

Next

Amazon

Prev

don't be afraid of buying books

EXCEPTION ASSERTIONS
should: aBlock raise: anExceptionalEvent asserts that executing a Bl oc k signals an
exception identified by anEx cept i o nal E v ent . Not signaling an exception, or signaling
a different exception, causes this assertion to fail.

shouldnt: aBlock raise: anExceptionalEvent the logical opposite of sh ou l d: a B lo ck


rais e: an Exc ep tion alEv ent.

Next

Amazon

Prev

don't be afraid of buying books

FAILURE
signalFailure: aString fails immediately, providing a S tr in g as a message.

Next

Amazon

Prev

don't be afraid of buying books

Next

EXAMPLE
Test 1: An empty list should have a size of zero.
Sma l ltalk .Sa or sa de fine Clas s: # T e stM o v ieL i s t
s u percl ass : #{X Pr ogra mmin g.SU n i t.T e s tCa s e }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : 'e mpty L i st'
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'S aor sa '
set U p
e m ptyLi st := Mo vi eLis t ne w
tes t Empty Lis tS ize
s e lf sh oul d: [e mp tyLi st s ize = 0]

When we try to accept the test method, it complains that Mo vi e Li st is undefined. We define it as a
stub class with nothing in it:
Sma l ltalk .Sa or sa de fine Clas s: # M o vie L i st
s u percl ass : #{C or e.Ob ject }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : ''
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'S aor sa '

Now t estEm pty Li stS iz e is accepted. When we run the test, it passes. Why? We haven't even written
the s ize method for Mo vieL ist.
When using methods with a common name (like size) be careful of
inherited methods.

The problem is that s ize is a method in Ob j e ct from which we inherit. This is the Smalltalk way. Let's
write a stub s iz e method with no body and try the test again. OK. Our test now fails. Now we can
write a bit of code to pass the test:
Mov i eList
>> s i ze
^0

Accept, test, green. Onward.

Test 2: Adding a movie to an empty list should result in a list with a size
of one.
This test adds one item to the list and checks that this size returns 1 . This is a different fixture (i.e., not
an empty list), so we start a new TestC a se:
Sma l ltalk de fi neC la ss: #Tes tOne I t emL i s t
s u percl ass : #{X Pr ogra mmin g.SU n i t.T e s tCa s e }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : 't heLi s t '
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'S aor sa '
set U p
t h eList := M ovi eL ist new.
t h eList ad d: (M ov ie n ame: 'St a r Wa r s ')
tes t Size
s e lf sh oul d: [t he List siz e = 1 ]

To get this to accept we need to define the class Mov i e :


Sma l ltalk .Sa or sa de fine Clas s: # M o vie
s u percl ass : #{C or e.Ob ject }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : ''
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'N owP la ying '
cla s s nam e: aS tri ng
^ s elf b asi cN ew
name: aS tr ing
nam e : aSt rin g

Running the tests now results in Tes tOne I t emL i s t>>t e st S iz e failing with an error because the
method Mov ie Lis t>>add: isn't understood (i.e., is undefined). We create a stub and now it fails with
an assertion failure since everything runs, but we aren't really doing anything yet. This is the state we
want to be in for the red bar. Now we can write some code to get the green bar back. We start by
capturing the fact that a movie was added:
Mov i eList >>a dd : a Mo vie
nu m berOf Mov ie s : = numb erOf Movi e s + 1

To do this we need to add an instance variable to the class definition:


Sma l ltalk .Sa or sa de fine Clas s: # M o vie L i st
s u percl ass : #{C or e.Ob ject }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : 'n umbe r O fMo v i es '
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'N owP la ying '

Finally, we need to return that variable from size:


Mov i eList
>> s i ze
^ n umber OfM ov ies

Now T estOn eIt em Lis t>>test Size passes, but Tes t Em p ty Li s t>>t es tS i ze fails. Why is that?
Because we aren't initializing n umbe rOfM o v ies to 0.
In Smalltalk, uninitialized variables have the value nil.

To fix this, we can add an explicit creation method (this is a class method):
Mov i eList cla ss
>> n e w
^ s elf b asi cN ew in it

and an initialization method (an instance method):


Mov i eList >>i ni t
nu m berOf Mov ie s : = 0

With these two methods in place, we get a green bar.

Test 3: If we add a movie to a list, we should be able to ask if it's there


and receive a positive response.
The next test checks that any movies that are added are reported as being in the list:
Tes t OneIt emL is t

>> t e stCon tai ns


se l f sho uld : [th eL ist cont ains M o vie N a med : 'S ta r W ar s ']

For this to work, we need a cont ainsM o vie N a med : method in Mo v ie Li s t. We can start off by
faking it:
con t ainsM ovi eN ame d: aSt ring
^ t rue

This makes the test pass. Now we need to test a counter-example.

Test 4: Asking about the presence of a movie that wasn't added should
result in a negative response.
tes t DoesN otC on tai n
s e lf sh oul dn t: [t heLi st c onta i n sMo v i eNa m e d: ' St a r T re k' ]

This test drives us to add the ability to remember what movies have been added to the list. Let's take a
bigger step and add a collection to hold onto them:
Sma l ltalk de fi neC la ss: #Mov ieLi s t
s u percl ass : #{C or e.Ob ject }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : 'n umbe r O fMo v i es m o vi es '
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'S aor sa '
ini t
n u mberO fMo vi es := 0.
m o vies := Di cti on ary new
add : aMov ie
n u mberO fMo vi es := num berO fMov i e s + 1 .
m o vies at: a Mov ie nam e pu t: a M o vie
siz e
^ n umber OfM ov ies
con t ainsM ovi eN ame d: aSt ring
^ m ovies in cl ude sK ey:a Stri ng

If we run the tests now, they all fail (with an error) because Mo vi e doesn't understand na me . Further,
Mov i e isn't storing its name. That's easy enough to fix:
Sma l ltalk de fi neC la ss: #Mov ie
s u percl ass : #{C or e.Ob ject }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : 'n ame '
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'S aor sa '

nam e
^ n ame
nam e : aSt rin g
n a me := aS tr ing

There, green. Now we have a little refactoring to do. We're keeping an explicit count of how many
movies have been added, but this information is available from the collection of movies. The first step is
to return the size of the collection:
Mov i eList
>> s i ze
^m o vies siz e

All tests still pass. Now we can go through M o vie L i st and remove all references to n u mb er O fM ov i es .
The result is:
Sma l ltalk de fi neC la ss: #Mov ieLi s t
s u percl ass : #{C or e.Ob ject }
i n dexed Typ e: #n on e
p r ivate : f al se
i n stanc eVa ri abl eN ames : 'm ovie s '
c l assIn sta nc eVa ri able Name s: ' '
i m ports : ' '
c a tegor y: 'S aor sa '
ini t
m o vies := Di cti on ary new
siz e
^ m ovies si ze
add : aMov ie
m o vies at: a Mov ie nam e pu t: a M o vie
con t ainsM ovi eN ame d: aSt ring
^ m ovies in cl ude sK ey:a Stri ng

Amazon

Prev

don't be afraid of buying books

Next

Chapter 23. CPPUNIT


This chapter presents CppUnit, the xUnit family member for the C++. The examples in this chapter are
written using G++ 2.96 and CppUnit 1.8.0.

Amazon

Prev

don't be afraid of buying books

Next

THE FRAMEWORK
You can download CppUnit from [URL 48]. CppUnit has quite a different feel to it from the xUnit
members we've looked at so far. This is largely due to the fact that C++, being compiled to native code,
does not support reflection. This means that we have to do all the work ourselves. Instead of being able
to have the test case class figure out what the tests are, we have to do it ourselves, like so:
voi d Test One It emL is t::r egis terT e s ts( T e stS u i te * su i te )
{
s u ite
-> a d dTest (ne w Tes tC alle r
< Te s tOneI tem Li st
>(
"t e s tSi z e ",
&T e s tOn e I te mL is t :: t es tS i ze ,
*t h i s)) ;
s u ite
-> a d dTest (ne w Tes tC alle r
< Te s tOneI tem Li st
>(
"t e s tCo n t ai ns ",
&T e s tOn e I te mL is t :: t es tC o nt ai n s,
*t h i s)) ;
s u ite
-> a d dTest (ne w Tes tC alle r
< Te s tOneI tem Li st
>(
"t e s tDo e s No tC on t ai n ",
&T e s tOn e I te mL is t :: t es tD o es No t Co nt a in ,
*t h i s)) ;
}

Assertions are also handled differently. In this case, they are implemented as macros:
CPP U NIT
CPP U NIT
CPP U NIT
CPP U NIT
CPP U NIT
CPP U NIT

A SSE RT (co nd itio n)


A SSE RT ME SS AGE( mess age, c ond i t ion )
F AIL (m ess ag e)
A SSE RT EQ UA L(ex pect ed, a c tua l )
A SSE RT EQ UA L ME SSAG E(me s s age , exp e c te d, a c tu a l)
A SSE RT DO UB LES EQUA L(ex p e cte d , ac t u al , de l ta )

This isn't the most elegant looking solution, but it does have the beneficial effect that assertions are
visually obvious.

Amazon

Prev

don't be afraid of buying books

EXAMPLE
Test 1: An empty list should have a size of zero.
We'll start with the test for an empty list:
c la ss T est Em pty Mo vie Li st : pub li c C p pUn i t ::T e st C as e {
pr iva t e:
M ovi e Lis t *th eL ist ;
pu bli c :
T est E mpt yM ovi eL ist (s td:: stri ng n a m e);
v irt u al vo id re gis te rTes ts(T es tS u i te * s uit e );
v oid set Up ();
v oid tes tS ize () ;
};
T es tEm p tyM ov ieL is t:: Te stEm ptyM ov ie L i st( s t d:: s tr i ng n a me )
:C ppU n it: :T est Ca se( na me)
{
}
v oi d T e stE mp tyM ov ieL is t::r egis te rT e s ts( T e stS u it e * su i te )
{
s uit e
-> a ddT e st( ne w T es tCa ll er
< Te stE m pty Mo vie Li st
>(
" t est S i ze" ,
& T est E m pty M ov i eL is t :: t es tS i ze ,
* t his ) ) ;
}
v oi d T e stE mp tyM ov ieL is t::s etUp ()
{
t heL i st= ne w M ov ieL is t();
}
v oi d T e stE mp tyM ov ieL is t::t estS iz e( )
{
C PPU N IT AS SER T EQU AL MES SAGE (" Em p t y l i s t s h ou l d be em p ty ",
0,
e mp t y Lis t
-> s ize ( ));
}

Here's the stub we need to compile:


c la ss M ovi eL ist
pu bli c :
M ovi e Lis t( );
i nt s ize () ;
};
M ov ieL i st: :M ovi eL ist () {
}

Next

i nt Mo v ieL is t:: si ze( ) {


r etu r n 0 ;
}

This passes because our stub implementation of Mov i e Lis t :: s iz e( ) returns 0 to keep the compiler
happy.

Test 2: Adding a movie to an empty list should result in a list with a size
of one.
This test will add a movie and verify that the size is now 1. As in earlier chapters, we'll move to a new
fixture for this:
c la ss T est On eIt em Lis t : pu blic C pp U n it: : T est C as e {
pr iva t e:
M ovi e Lis t *th eL ist ;
pu bli c :
T est O neI te mLi st (st d: :str ing na me ) ;
v irt u al vo id re gis te rTes ts(T es tS u i te * s uit e );
v oid set Up ();
v oid tes tS ize () ;
};
T es tOn e Ite mL ist :: Tes tO neIt emLi st (s t d ::s t r ing na m e)
: Cpp U nit :: Tes tC ase (n ame)
{
}
v oi d T e stO ne Ite mL ist :: regi ster Te st s ( Tes t S uit e * s ui te )
{
s uit e
-> a ddT e st( ne w T es tCa ll er
< Te stO n eIt em Lis t
> (" tes t Siz e" ,
}

&T e st On e It em L is t: : te st S iz e,
* t hi s) ) ;

v oi d T e stO ne Ite mL ist :: setU p()


{
t heL i st = new M ovi eL ist( );
M ovi e st ar War s( "St ar War s");
t heL i st
-> a dd( s tar Wa rs) ;
}
v oi d T e stO ne Ite mL ist :: test Size ()
{
C PPU N IT AS SER T EQU AL MES SAGE (" On e ite m lis t s h ou ld ha v e on e i te m ",
1,
t h eLi s t
-> s ize ( ));
}

This requires that we create a stub Movi e class:


c la ss M ovi e {
pu bli c :
M ovi e (st d: :st ri ng aN ame) ;

};

M ov ie: : Mov ie (st d: :st ri ng a Name )


{
}

We also need to stub an a dd method for M o vie L i st:


v oi d M o vie Li st: :a dd( Mo vie movi e)
{
}

This now compiles and fails with:


.F
! !! FAI L URE S! !!
T es t R e sul ts :
R un : 1 Fai lu res : 1 E rr ors: 0
1 ) tes t : t es tSi ze (F ) line : 27 T es t O neI t e mLi s t. c pp
e xp ect e d: 1
b ut wa s : 0
a dd iti o nal m ess ag e:
O ne it e m l is t s ho uld h ave one it em

Now we can fake it one step better by counting the number of movies that are added:
c la ss M ovi eL ist {
pr iva t e:
i nt n umb er OfM ov ies ;
pu bli c :
M ovi e Lis t( );
i nt s ize () ;
v oid add (M ovi e aMo vi e);
};
M ov ieL i st: :M ovi eL ist () {
n umb e rOf Mo vie s = 0 ;
}
i nt Mo v ieL is t:: si ze( ) {
r etu r n n um ber Of Mov ie s;
}
v oi d M o vie Li st: :a dd( Mo vie movi e)
{
n umb e rOf Mo vie s+ +;
}

Our tests pass and there isn't anything in M ovi e L ist to clean up.

Test 3: If we add a movie to a list, we should be able to ask if it's there


and receive a positive response.
Our next test should verify that the movie that was added is really there; again, we'll fake it at first:
v oi d T e stO ne Ite mL ist :: test Cont ai ns ( )
{
C PPU N IT AS SER T MES SA GE(" Star W ar s sho u l d b e i n t he li s t. ",
t he L is t
-> c ont a ins Mo vie Na med (" Star War s" )) ;
}

We next need a stub c on tain sMov ieNam e d method in M ov ie Li s t that fakes it for the purposes of this
test:
b oo l M o vie Li st: :c ont ai nsMo vieN am ed ( s td: : s tri n g n am e)
{
r etu r n t ru e;
}

Test 4: Asking about the presence of a movie that wasn't added should
result in a negative response.
Now we need to test that a movie that we didn't add is, in fact, not reported as being in the list:
v oi d T e stO ne Ite mL ist :: test Does No tC o n tai n ( )
{
C PPU N IT AS SER T MES SA GE(" Star T re k sho u l d n o t b e in th e l is t ." ,
! theL is t
-> c ont a ins Mo vie Na med (" Star Tre k" )) ;
}

This, of course, fails with:


. .. F
! !! FAI L URE S! !!
T es t R e sul ts :
R un :
3
Fa ilu re s: 1

Er rors : 0

1 ) tes t : t es tDo es Not Co ntai n (F ) li n e : 3 9 Tes t On e It em L is t .c pp


"S tar Tre k sho ul d n ot be in t he l i s t."

This test drives us to move from fake to make in terms of keeping track of what movies have been added
to the list:
c la ss M ovi eL ist {
pr iva t e:
i nt n umb er OfM ov ies ;
m ap
< st d:: s tri ng , M ov ie
> m ovi e s;
pu bli c :

};

M ovi e Lis t( );
i nt s ize () ;
v oid add (M ovi e aMo vi e);
b ool con ta ins Mo vie Na med( std: :s tr i n g n a m e);

M ov ieL i st: :M ovi eL ist () {


nu mbe r OfM ov ies = 0;
}
i nt Mo v ieL is t:: si ze( ) {
r etu r n n um ber Of Mov ie s;
}
v oi d M o vie Li st: :a dd( Mo vie movi e)
{
n umb e rOf Mo vie s+ +;
m ovi e s[m ov ie. ge tNa me ()] = mo vi e;
}
b oo l M o vie Li st: :c ont ai nsMo vieN am ed ( s td: : s tri n g n am e)
{
ma p
< st d:: s tri ng , M ov ie
> :: ite r ato r res ul t = m ovie s.fi nd (n a m e);
re tur n re su lt != mo vi es.e nd() ;
}

We also need to extend M ov ie to retain its name (and add a default constructor to satisfy the
requirements of ma p):
c la ss M ovi e {
pr iva t e:
s td: : str in g n am e;
pu bli c :
M ovi e ();
M ovi e (st d: :st ri ng aN ame) ;
s td: : str in g g et Nam e( );
};
M ov ie: : Mov ie ()
{
na me = "" ;
}
M ov ie: : Mov ie (st d: :st ri ng a Name )
{
na me = aN am e;
}
s td ::s t rin g Mov ie ::g et Name ()
{
re tur n na me ;
}

As you can see, we are now duplicating the information about how many movies have been added. We
keep track of it explicitly (as before) and it is also available as an attribute of the m ap . To remove it, we
first change si ze to return the information fetched from the m a p:

i nt Mo v ieL is t:: si ze( ) {


r etu r n m ov ies .s ize () ;
}

The tests still pass, so we can remove all of the code related to explicitly counting how many movies have
been added. Now Mov ie List is:
c la ss M ovi eL ist {
pr iva t e:
m ap
< st d:: s tri ng , M ov ie
> m ovi e s;
pu bli c :
M ovi e Lis t( );
i nt s ize () ;
v oid add (M ovi e aMo vi e);
b ool con ta ins Mo vie Na med( std: :s tr i n g n a m e);
};
M ov ieL i st: :M ovi eL ist () {
}
i nt Mo v ieL is t:: si ze( ) {
r etu r n m ov ies .s ize () ;
}
v oi d M o vie Li st: :a dd( Mo vie movi e)
{
mo vie s [mo vi e.g et Nam e( )] = mov ie ;
}
b oo l M o vie Li st: :c ont ai nsMo vieN am ed ( s td: : s tri n g n am e)
{
ma p
< st d:: s tri ng , M ov ie
> :: ite r ato r res ul t = m ovie s.fi nd (n a m e);
re tur n re su lt != mo vi es.e nd() ;
}

Amazon

Prev

don't be afraid of buying books

Next

Chapter 24. NUNIT


WITH JAMES NEWKIRK
This chapter presents NUnit, an xUnit family member for the Microsoft .NET framework. I had the good
fortune to cross paths with James as I was thinking about this chapter. I had a few questions about
NUnit which I asked James, explaining my reasons. He responded and kindly offered to write and
contribute the chapter. So, here's the chapter on NUnit, by one of its authors.
One thing to note: While C# and .NET are Microsoft creations, there is an open source implementation
(that is progressing nicely) from Ximian, the folks behind Gnome. It's named The Mono Project and can
be found at [URL 66].

Amazon

Prev

don't be afraid of buying books

Next

THE FRAMEWORK
The example is written using V1.0 of the .NET Framework and V2.0 of NUnit. See [URL 52] for the site
from which NUnit can be downloaded.

History
NUnit V1.x was written by Philip Craig in the summer of 2000. Philip released an early version in
September of 2000 and was keeping the program up to date with each new version of the .NET
Framework as they were being released. In February of 2002 a small group gathered to develop a new
version of NUnit, one that looked less like JUnit and tried to leverage some of the capabilities of the
.NET platform. The group consisted of Alexei Vorontsov, Mike Two, and James Newkirk. The one feature
in .NET that we focused on was attributes. Attributes are descriptive elements that provide information
about programming elements such as types, fields, methods, and properties that can be retrieved via
reflection at runtime. Looking at JUnit, we saw areas where naming conventions and inheritance were
used to identify test fixtures and test methods and thought that attributes provided a cleaner and more
explicit approach to the same problems. This group released NUnit V2.0 in October of 2002.

TestFixture
In order to write your first test in NUnit, you must create a class that is marked with the
[Te s tFixt ure ] attribute. The attribute definition is contained in an assembly called
nun i t.fra mew or k.d ll . Also, you must provide a using statement that identifies the NUnit classes
which are contained in the NU nit. Frame w ork namespace. One thing to note is that the only
requirement for the class is that it has a default constructor which means that you can place the
[Te s tFixt ure ] attribute on any class and it does not have to use inheritance.
usi n g Sys tem ;
usi n g NUn it. Fr ame wo rk;
[Te s tFixt ure ]
pub l ic cl ass S ome th ing
{}

Test
A test method in NUnit must be a method that is public, returns void, and has no parameters. It must
also be marked with the [ Test] attribute and be in a class marked with a [T e st Fi x tu re ] attribute.
[Te s t]
pub l ic vo id Bl ueS ky ()
{}

SetUp
NUnit, like many xUnit implementations, has a way to allow the programmer to extract common fixture
setup code and have it executed prior to each test being run.

A method can be marked with the custom attribute [ S etU p ] to identify it as the method that NUnit will
use to build the fixture for the tests in this T est F i xtu r e. This allows for common code to be removed
from the individual tests.
[Se t Up]
pub l ic vo id Cr eat eL ist( )
{
l i st = new M ovi eL ist( );
}

Assertion
The NUnit framework, like other xUnit frameworks, comes with the normal set of expected assertions.
Since inheritance is not used to identify the test fixtures, the assertions in NUnit are static methods of a
class called A ss ert io n. This class is also defined in the N U ni t .F ra m ew o rk namespace. The following
is a sample of assert methods contained in the framework (see the class Assertion for the complete set):
Ass e rtion .As se rtE qu als( obje ct e x p ect e d , o b j ec t ac t ua l )
Ass e rtion .As se rt( bo olea n)
Ass e rtion .As se rtN ot Null (obj ect)

All of the assertion methods also take an initial parameter, which is a message that will be printed if
there is a failure.

Amazon

Prev

don't be afraid of buying books

Next

EXAMPLE
The task is to write a class that contains a list of movies. The list should be able to determine if certain
movies are in the list and it should also be able to tell you the number of movies contained in the list.

Test 1: An empty list should have a size of zero.


We'll start by creating a test fixture to hold all of the tests called T e s t M o v i e L i s t . The name of the
method that will create the list and test to see if the count of movies is equal to 0 is called E m p t y L i s t .
As you can see here they are each marked with their respective attributes.
u si n g Sy ste m ;
u si n g NU nit . Fram e work ;
[ Te s tF ix tur e ]
p ub l ic c las s Test M ovie L ist
{

[ T es t]
p u bl ic vo i d Em p tyLi s t()
{
Mo vi eLi s t li s t = n ew M ovie L i s t ( ) ;
As se rti o n.As s ertE q uals (0, l i s t . C o u n t ) ;
}

When I compile this test it fails since we have yet to implement the M o v i e L i s t class. The simplest
version of Mo v ieLi s t that is needed to get this to compile is as follows:
u si n g Sy ste m ;
p ub l ic c las s Mov i eLis t
{
p u bl ic in t Cou n t
{
ge t { r e turn 0; }
}
}

Compiling and running this yields a green bar. The first step is done. Let's go on to the next test.

Test 2: Adding a movie to an empty list should result in a list with a size
of one.
The test for this step is also in the Tes t M o v i e L i s t fixture and looks like this:
[ Te s t]
p ub l ic v oid OneM o vieL i st()
{
M o vi eL ist list = ne w Mov ieLi s t ( ) ;
M o vi e sta r Wars = ne w Mov ie(" S t a r W a r s " ) ;
l i st .A dd( s tarW a rs);
A s se rt ion . Asse r tEqu a ls(1 , li s t . C o u n t ) ;
}

When I compile this it fails for a couple of reasons. First, I have not defined a class called M o v i e and I
have not defined a method in M ovie Li s t called A d d . Let's go ahead and implement them, keeping in

mind the goal here is to get this test to pass, nothing else.
The simplest version of M ovie that is needed to get this to compile is as follows:
u si n g Sy ste m ;
p ub l ic c las s Movi e
{
p u bl ic Mo v ie(s t ring titl e)
{}
}

Nothing else is needed. Remember, you need to focus on what is needed now, not in the future.
Obviously, the M ov ie class is nothing more than a placeholder at this point. We also need to implement
the A dd method in Mov i eList . At first, I choose to implement A d d like the following:
p ub l ic v oid Add( M ovie movi eToA d d )
{}

Compiling and running this yields a failure. Obviously, we have to implement A d d for the test to pass.
Since we only have tests that verify that the size of the list is correct, we can still fake it somewhat by
keeping track of the number of movies that were added to the list and not the contents.
u si n g Sy ste m ;
p ub l ic c las s Mov i eLis t
{
p r iv at e i n t nu m berO f Movi es;
p u bl ic Mo v ieLi s t()
{
nu mb erO f Movi e s = 0 ;
}

p u bl ic in t Cou n t
{
ge t { r e turn numb e rOfM ovie s ; }
}

p u bl ic vo i d Ad d (Mov i e mo vieT o A d d )
{
nu mb erO f Movi e s += 1;
}

Compiling and running this yields a green bar. However, there is some code duplication in the test code.
Each test method creates a M ovie List class. We can extract this commonality into a S e t U p method.
Performing this refactoring on T estM ov i e L i s t looks like this:
u si n g Sy ste m ;
u si n g NU nit . Fram e work ;
[ Te s tF ix tur e ]
p ub l ic c las s Test M ovie L ist
{
p r iv at e M o vieL i st l i st;
[ S et Up ]
p u bl ic vo i d Cr e ateL i st()
{
li st = n ew M o vieL i st() ;
}

[ T es t]
p u bl ic vo i d Em p tyLi s t()
{
As se rti o n.As s ertE q uals (0, l i s t . C o u n t ) ;
}

[ T es t]
p u bl ic vo i d On e Movi e List ()
{
Mo vi e s t arWa r s = n ew M ovie ( " S t a r W a r s " ) ;
li st .Ad d (sta r Wars ) ;
As se rti o n.As s ertE q uals (1, l i s t . C o u n t ) ;
}

Compiling and running this test yields a successful result and a scan of the code indicates that there is
nothing else to refactor. It's time to move on to the next test.

Test 3: If we add a movie to a list, we should be able to ask if it's there


and receive a positive response.
The test method for this test is called O n e M o v i e L i s t C o n t a i n s and is defined as follows:
[ Te s t]
p ub l ic v oid OneM o vieL i stCo ntai n s ( )
{
s t ri ng ti t le = "Sta r War s";
M o vi e sta r Wars = ne w Mov ie(t i t l e ) ;
l i st .A dd( s tarW a rs);
A s se rt ion . Asse r t(li s t.Co ntai n s M o v i e N a m e d ( t i t l e ) ) ;
}

When this is compiled it fails due to Mo v i e L i s t not having implemented the method
C on t ai ns Mov i eNam e d. Implementing a stub to get the test to pass is simple and looks like this:
p ub l ic b ool Cont a insM o vieN amed ( s t r i n g t i t l e )
{
r e tu rn tr u e;
}

Compiling and running this yields a successful result. At this point, most people question my judgment
and wonder if this is just silly. Remember, we are not done with the task; we are moving forward stepby-step doing just enough to get the tests to pass. We will have tests in the future that expose that
this implementation is naive, but I intend to wait until the tests expose that naivet instead of
speculating about it.
Before we move on we have some more code duplication to correct in the test code. Looking at the test
code, we see that the OneM o vieL istC o n t a i n s and O n e M o v i e L i s t methods have a common setup
code. However, that setup code is different from the E m p t y L i s t setup code. The way to correct this is
to split the test class into two different classes, one called T e s t E m p t y M o v i e L i s t and the other called
T es t On eM ovi e List . Once we move the methods into their new classes they can also be renamed.
Performing this refactoring yields the following result:
u si n g Sy ste m ;
u si n g NU nit . Fram e work ;
[ Te s tF ix tur e ]
p ub l ic c las s Tes t Empt y Movi eLis t
{
p r iv at e M o vieL i st l i st;
[ S et Up ]
p u bl ic vo i d Cr e ateL i st()

{
l i st = ne w Mov i eLis t ();
}
[ T es t]
p u bl ic vo i d Co u nt()
{
A s se rt ion . Asse r tEqu a ls(0 , li s t . C o u n t ) ;
}

[ Te s tF ix tur e ]
p ub l ic c las s Tes t OneI t emMo vieL i s t
{
p r iv at e r e adon l y st a tic stri n g t i t l e = " S t a r W a r s " ;
p r iv at e M o vieL i st l i st;

[ S et Up ]
p u bl ic vo i d Cr e ateL i st()
{
li st = n ew M o vieL i st() ;
Mo vi e s t arWa r s = n ew M ovie ( t i t l e ) ;
li st .Ad d (sta r Wars ) ;
}
[ T es t]
p u bl ic vo i d Co u nt()
{
As se rti o n.As s ertE q uals (1, l i s t . C o u n t ) ;
}
[ T es t]
p u bl ic vo i d Co n tain s ()
{
As se rti o n.As s ert( l ist. Cont a i n s M o v i e N a m e d ( t i t l e ) ) ;
}

Once this refactoring is complete we compile and run; everything still works so we are finished with this
step.

Test 4: Asking about the presence of a movie that wasn't added should
result in a negative response.
The last test that we outlined earlier is to verify that a movie is not in the list. The test method can be
implemented in either test fixture classes. In this example, it is implemented in T e s t O n e M o v i e L i s t
test fixture and is called Do e sNot Conta i n :
[ Te s t]
p ub l ic v oid Does N otCo n tain ()
{
A s se rt ion . Asse r t(!l i st.C onta i n s M o v i e N a m e d ( " S t a r T r e k " ) ) ;
}

Compiling and running the test yields the following output from NUnit:
N Un i t ve rsi o n 2. 0 .6
. .. . F
T es t s ru n: 4 , Fa i lure s : 1, Not r u n : 0 , T i m e : 0 . 0 3 0 0 4 3 2 s e c o n d s
F ai l ur es :
1 ) T es tO neI t emMo v ieLi s t.Do esNo t C o n t a i n :
a t Te stO n eIte m Movi e List .Doe s N o t C o n t a i n ( ) i n t e s t m o v i e l i s t . c s : l i n e 5 1

This test informs us that the implementation of C o n t a i n s M o v i e N a m e d is not sufficient given the
current tests. In order to get this test to pass we need to change the implementation of M o v i e L i s t to
save the Movie objects that are added so that the C o n t a i n s M o v i e N a m e d method can determine if
they are in the list. A way to implement this is with a H a s h t a b l e from the S y s t e m . C o l l e c t i o n s
class in the .NET Framework.
u si n g Sy ste m ;
u si n g Sy ste m .Col l ecti o ns;
p ub l ic c las s Mov i eLis t
{
p r iv at e H a shta b le m o vies ;
p r iv at e i n t nu m berO f Movi es;
p u bl ic Mo v ieLi s t()
{
mo vi es = new Hash t able ();
nu mb erO f Movi e s = 0 ;
}
p u bl ic in t Cou n t
{
ge t { r e turn numb e rOfM ovie s ; }
}
p u bl ic vo i d Ad d (Mov i e mo vieT o A d d )
{
mo vi es. A dd(m o vieT o Add. Name , m o v i e T o A d d ) ;
nu mb erO f Movi e s += 1;
}

p u bl ic bo o l Co n tain s Movi eNam e d ( s t r i n g t i t l e )


{
re tu rn m ovie s .Con t ains Key( t i t l e ) ;
}

Compiling this yields a compilation failure due to the property Name not being defined in the M o v i e
class. It's finally time to add some functionality to the M o v i e class.
u si n g Sy ste m ;
p ub l ic c las s Movi e
{
p r iv at e s t ring titl e ;
p u bl ic Mo v ie(s t ring titl e)
{
th is .ti t le = titl e ;
}
p u bl ic st r ing N ame
{
ge t { r e turn titl e ; }
}
}

Compiling and running this yields the familiar green bar. Upon further review, however, there is a smell
that exists in the M o vie Li st class. The H a s h t a b l e class can perform the function of keeping track of
the number of elements so we do not need the n u m b e r O f M o v i e s variable. Making this change to
M ov i eL is t looks like this:
u si n g Sy ste m ;

u si n g Sy ste m .Col l ecti o ns;


p ub l ic c las s Mov i eLis t
{
p r iv at e H a shta b le m o vies ;
p u bl ic Mo v ieLi s t()
{
mo vi es = new Hash t able ();
}
p u bl ic in t Cou n t
{
ge t { r e turn movi e s.Co unt; }
}
p u bl ic vo i d Ad d (Mov i e mo vieT o A d d )
{
mo vi es. A dd(m o vieT o Add. Name , m o v i e T o A d d ) ;
}

p u bl ic bo o l Co n tain s Movi eNam e d ( s t r i n g t i t l e )


{
re tu rn m ovie s .Con t ains Key( t i t l e ) ;
}

Doing this refactoring has simplified the code dramatically. Reviewing the existing code along with our
to-do list indicates that we are finished with the task, so let's check it back into the repository and
move on to the next task.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 25. PYUNIT


WITH BOB PAYNE
This chapter presents PyUnit, the xUnit framework for the Python programming language. Bob Payne is
the president of Electroglide Inc., a small software development firm specializing in eXtreme
Programming consulting, implementation, development, and training. He is the co-founder of the
Washington D.C. XP users group, an active presenter at XP conferences, and an agitator for Agile
Methodologies in general.

Amazon

Prev

don't be afraid of buying books

Next

THE FRAMEWORK
PyUnit (uni tte st .py ) is a core module in current Python distributions. This example was coded using
Python 2.2.2 on Windows, but should run unchanged on virtually any Python platform. Distributions and
information on Python can be found at [URL 67]. Development of the framework is ongoing under the
supervision of Steve Purcell at [URL 53]. If you are doing Web development there are several test
frameworks that are similar to the HTTPUnit framework in Java; these include:
Puffin An XML configurable Web application/service testing framework built in Python
by Keyton Weissinger. Information on the Puffin framework is available at [URL 68].

WebUnit WebUnit is a unit testing framework for testing Web applications in PyUnit
tests. WebUnit was developed by Steve Purcell, the author of PyUnit. Information about
WebUnit can be found at [URL 69].

PyUnit has all the standard xUnit asserts and provides some name mappings to allow the use of the
logically equivalent asserts. The asserts and their logical equivalents are listed below with their
parameter listings (msg parameter is for an optional message):

Testing Equivalence
Equality tests are more liberal in Python than in Java. Python generally tests for value equivalence of
built-in objects and utilizes the overloadable equivalence operator for other classes.
failUnlessEqual(first, second, msg=None) fails if the two objects are unequal as
determined by the '!=' operator.

assertEqual(first, second, msg=None) and

assertEquals(first, second, msg=None) are logically equivalent to


fail Unl es sEq ua l.

failIfEqual(first, second, msg=None) fails if the two objects are equal as determined
by the '= =' operator.

assertNotEqual(first,second, msg=None) and

assertNotEquals(first, second, msg=None) are logically equivalent to f ai l lf Eq u al .

Testing Logical comparison


Test an expression to determine if it evaluates to t r ue or fa l se . This evaluation is much more liberal
in Python than in most other languages because Python evaluates non zero, non empty, or non No ne
(i.e., n u ll) as true. This liberal interpretation of true allows great flexibility in assertions but can lead to
false positives if care is not taken. The comparison operators i n and n o t in check whether a value
occurs (or does not occur) in a sequence. The operators i s and i s n ot compare whether two objects
are really the same object.
failIf(expr, msg=None) fails the test if the expression is t ru e .

failUnless(expr, msg=None) fails the test unless the expression is true as se r t


(exp r, ms g=N on e); logically equivalent to fa i lU n le ss .

Testing Exceptions
PyUnit provides assertions to test exception handling in applications. These assertions test if specific
exceptions are or are not raised given the evaluation of a call to an object. If a different type of
exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error,
exactly as for an unexpected exception.
failUnlessRaises(excClass, callableObj, *args, **kwargs) fails unless an exception of
class ex cCl as s is thrown by cal l abl e O bj when invoked with arguments a rg s and
keyword arguments kwa rgs.

assertRaises(excClass, callableObj, *args, **kwargs) is logically equivalent to


fail Unl es sRa is es.

Automatic Failure
PyUnit also provides a fa il () method to allow programmers to create unit tests that leverage complex
conditional blocks that would not easily map to the other assertion structures.
fail(msg=None) fails immediately.

Writing tests in PyUnit


Tests in PyUnit are collected in test methods on test objects. Each test method should have one or more
assertions in the body of the method and method names should begin with "te st ". Test objects must
import the uni tt est module, extend u n itt e s t.T e s tCa s e, and their names should begin with
"Tes t ". Optional s et Up( ) and tea rDow n ( ) methods on test objects are used to build a fixture and
clean it up after each test method is run.
The simplest form of a test is shown here:
Imp o rt un itt es t
cla s s Tes tMy Te stO bj ect (uni ttes t . Tes t C ase ) :
def t est Eq uiv al ence Test s(se l f ):
s elf .a sse rt Equa ls(' Bob' , 'Bo b ' )
if _ _name == ' __m ai n__' :
unitt est .m ain ()

Tests can be collected into test suites explicitly or through introspection. While each has its advantages,
I find that utilizing introspection reduces the chance that a test will be missed. The above example
utilizes one form of introspection by calling the u nit t e st .m ai n () . When the script is run, Python will
look for all objects beginning with "Te st " and add them to a test suite; in turn, each method beginning
with "t est" will be evaluated when each T est C a se in the suite is run. Order of test execution cannot
be guaranteed.

Amazon

Prev

don't be afraid of buying books

Next

EXAMPLE
We must now write our first failing test before we can code.

Test 1: An empty list should have a size of zero


i mp o r t u ni ttes t
f ro m mov ie .Mov i e i m port *
c la s s Te st Movi e Lis t (unit test .T e s t C a s e ) :
d e f te st Zero L eng t hMovi eLis t( s e l f ) :
mo vi eLis t = M ovieL ist( )
se lf .ass e rtE q uals( movi eL i s t . g e t S i z e ( ) , 0 )
i f_ _ n ame = = '_ _ mai n __':
u n itt es t.ma i n()

The test fails with:


= == = = === == ==== = === = ===== ==== == = = = = = = = = = = = = = = = = =
E RR O R : t es tZer o Len g thMov ieLi st ( _ _ m a i n _ _ . T e s t M o v i e L i s t )
- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - T ra c e bac k (mos t re c ent c all la s t ) :
F i l e " c: \pyt h on2 2 \lib\ movi e\ T e s t M o v i e . p y " , l i n e 7 , i n t e s t Z e r o L e n g t h M o v i e L i s t
m ovi eL ist = Mo v ieLis t()
N am e E rro r: glo b al n ame ' Movi eL i s t ' i s n o t d e f i n e d
- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - R an 1 te st s in 0.0 0 0s
F AI L E D ( er rors = 1)

Now we will do the simplest thing that will give us a passing test. We will stub out s i z e in the movie
object to return zero.
c la s s Mo vi eLis t :
d ef ge tSiz e (se l f):
re turn 0

Green barthe test passes with:


- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - R an 1 te st s in 0.0 0 0s
OK

Test 2: Adding a movie to an empty list should result in a list with a size
of one
It is now our responsibility to expand and test for other behaviors of the MovieList object. We have also
refactored the test to remove redundancy of creating an instance of MovieList and cleaning up the
s et U p () and t e arDo w n() methods of the test object.

c la s s Te st Movi e Lis t (unit test .T e s t C a s e ) :


d e f se tU p(se l f):
se lf .mov i eLi s t = M ovie Li s t ( )
d e f te st Zero L eng t hMovi eLis t( s e l f ) :
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 0 )
d e f te st AddM o vie ( self) :
se lf .mov i eLi s t.add Movi e( M o v i e ( ' S t a r W a r s ' ) )
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 1 )
d e f te ar Down ( sel f ):
se lf .mov i eLi s t=Non e

Red barthe test fails with:


= == = = === == ==== = === = ===== ==== == = = = = = = = = = = =
E RR O R : t es tAdd M ovi e (__m ain_ _. T e s t M o v i e L i s t )
- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - T ra c e bac k (mos t re c ent c all la s t ) :
F i l e " c: \pyt h on2 2 \lib\ movi e\ T e s t M o v i e . p y " , l i n e 1 1 , i n t e s t A d d M o v i e
s elf .m ovie L ist . addMo vie( Mo v i e ( ' S t a r W a r s ' ) )
A tt r i but eE rror : Mo v ieLis t in st a n c e h a s n o a t t r i b u t e ' a d d M o v i e '
- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - R an 2 te st s in 0.0 0 0s
F AI L E D ( er rors = 1)

Stub out the addM o vie ( movie ) method with a pass instruction. This method returns None when it is
called and does nothing.
c la s s Mo vi eLis t :
d ef ge tSiz e (se l f):
re turn 0
d ef ad dMov i e(s e lf, m ovie ):
pa ss

Red bar M ovi e object is not defined; we must stub out a M o v i e class. Before we do this we need to
add a te st Mov i e class to test the behavior of a M o v i e and stub out M o v i e . This class only needs to
retain its name for now, so that is the only behavior we will test for.
Add test class:
c la s s te st Movi e (un i ttest .Tes tC a s e ) :
d e f te st Movi e (se l f):
mo vi e=Mo v ie( ' Star Wars ')
se lf .ass e rtE q uals( movi e. n a m e , ' S t a r W a r s ' )

Create the code for the Movie class, a simple class with only a constructor method.
c la s s Mo vi e:
d ef __ init _ _(s e lf, n ame= No n e ) :
se lf.n a me = name

Red bar M ovi e List is still returning 0 for all g e t S i z e ( ) requests:


= == = = === == ==== = === = ===== ==== == = = = = = = = = = = = = =
F AI L : te st AddM o vie (__ma in__ .T e s t M o v i e L i s t )
- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

T ra c e bac k (mos t re c ent c all la s t ) :


F i l e " c: \pyt h on2 2 \lib\ movi e\ T e s t M o v i e . p y " , l i n e 1 2 , i n t e s t A d d M o v i e
s elf .a sser t Equ a ls(se lf.m ov i e L i s t . g e t S i z e ( ) , 1 )
F i l e " C: \Pyt h on2 2 \Lib\ unit te s t . p y " , l i n e 2 7 3 , i n f a i l U n l e s s E q u a l
r ais e self . fai l ureEx cept io n , ( m s g o r ' % s ! = % s ' % ( f i r s t , s e c o n d ) )
A ss e r tio nE rror : 0 ! = 1
- -- - - --- -- ---- - --- - ----- ---- -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - R an 3 te st s in 0.0 0 0s
F AI L E D ( fa ilur e s=1 )

Modify M ov ieL i st to track the size. Yes, we know this is a stupid way to do it but we also know that
first we will make it work and then we will make it elegant.
c la s s Mo vi eLis t :
d ef __ init _ _(s e lf):
se lf.s i ze= 0
d ef ge tSiz e (se l f):
re turn sel f .size
d ef ad dMov i e(s e lf, m ovie ):
se lf.s i ze + = 1

Green bar All three of our tests are running at 100 percent. We could now integrate our code with the
main codebase depending on our check-in strategy. Now let's add tests for the functionality to see if a
movie has already been added to the Mo v i e L i s t .

Test 3: If we add a movie to a list, we should be able to ask if it's there


and receive a positive response
c la s s Te st Movi e Lis t (unit test .T e s t C a s e ) :
d e f se tU p(se l f):
se lf .mov i eLi s t = M ovie Li s t ( )
d e f te st Zero L eng t hMovi eLis t( s e l f ) :
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 0 )
d e f te st AddM o vie ( self) :
se lf .mov i eLi s t.add Movi e( M o v i e ( ' S t a r W a r s ' ) )
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 1 )
d e f t es tCon t ain s Movie (sel f) :
se lf .mov i eLi s t.add Movi e( M o v i e ( ' S t a r W a r s ' ) )
se lf .fai l Unl e ss(se lf.m ov i e L i s t . c o n t a i n s M o v i e ( ' S t a r W a r s ' ) )
d e f te ar Down ( sel f ):
se lf .mov i eLi s t=Non e

Red bar Stub out the functionality in M o v i e L i s t by simply returning T r u e when the
c on t a ins Mo vie( n ame ) method is called.
c la s s Mo vi eLis t :
d ef __ init __( s elf):
s el f.si z e=0
d ef ge tSiz e (se l f):
re turn sel f .size
d ef ad dMov i e(s e lf, m ovie ):
se lf.s i ze + = 1

d ef co ntai n sMo v ie(se lf, na m e ) :


re turn Tru e

Green bar All four of our tests pass. Refactor test objects to be a bit more modular. Notice that we
pulled the test for zero length list into another test object and created and then added "Star Wars" to the
list of movies. The methods setU p() and t e a r d o w n ( ) are called for each of the test method
invocations in that class so we do not have to worry about test crosstalk in the object.
c la s s Te st Zero L eng t hMovi eLis t( u n i t t e s t . T e s t C a s e ) :
d e f te st Zero L eng t hMovi eLis t( s e l f ) :
mo vi eLis t =Mo v ieLis t()
se lf .ass e rtE q uals( movi eL i s t . g e t S i z e ( ) , 0 )
c la s s Te st Movi e Lis t (unit test .T e s t C a s e ) :
d e f se tU p(se l f):
se lf .mov i eLi s t = M ovie Li s t ( )
se lf .mov i eLi s t.add Movi e( M o v i e ( ' S t a r W a r s ' ) )
d e f te st OneM o vie L ist(s elf) :
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 1 )
d e f te st Cont a ins M ovie( self ):
se lf .fai l Unl e ss(se lf.m ov i e L i s t . c o n t a i n s M o v i e ( ' S t a r W a r s ' ) )
d e f te ar Down ( sel f ):
se lf .mov i eLi s t=Non e

Green bar Now we must extend the test to express the true intent of the c o n t a i n s - M o v i e ( n a m e )
method by testing to ensure that we get a false if the movie has not been added.

Test 4: Asking about the presence of a movie that wasn't added should
result in a negative response
c la s s Te st Movi e Lis t (unit test .T e s t C a s e ) :
d e f se tU p(se l f):
se lf .mov i eLi s t = M ovie Li s t ( )
se lf .mov i eLi s t.add Movi e( M o v i e ( ' S t a r W a r s ' ) )
d e f te st OneM o vie L ist(s elf) :
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 1 )
d e f te st Cont a ins M ovie( self ):
se lf .fai l Unl e ss(se lf.m ov i e L i s t . c o n t a i n s M o v i e ( ' S t a r W a r s ' ) )
d e f te st NotC o nta i nsMov ie(s el f ) :
se lf .fai l If( s elf.m ovie Li s t . c o n t a i n s M o v i e ( ' S t a r T r e k ' ) )
d e f te ar Down ( sel f ):
se lf .mov i eLi s t=Non e

Red bar Extend M ovi e List to check if a movie has been added:
c la s s Mo vi eLis t :
d ef __ init _ _(s e lf):
se lf.s i ze= 0
se lf.m o vie s =[]
d ef ge tSiz e (se l f):
re turn sel f .size
d ef ad dMov i e(s e lf, m ovie ):

se lf.s i ze + = 1
se lf.m o vie s .appe nd(m ov i e )
d ef co ntai n sMo v ie(se lf, na m e ) :
fo r mo v ie i n sel f.mo vi e s :
if mov i e.nam e == n a m e :
re t urn T rue
re turn Fal s e

Green bar All tests pass. We are done. Or are we? We still must refactor to make the implementation
of our objects as clear as possible and remove any redundancy. Our M o v i e L i s t class is using a
counting scheme rather than checking the length of our internal movie list.
c la s s Mo vi eLis t :
d ef __ init _ _(s e lf):
se lf.m o vie s =[]
d ef ge tSiz e (se l f):
re turn len ( self. movi es )
d ef ad dMov i e(s e lf, m ovie ):
se lf.m o vie s .appe nd(m ov i e )
d ef co ntai n sMo v ie(se lf, na m e ) :
fo r mo v ie i n sel f.mo vi e s :
if mov i e.nam e == n a m e :
re t urn T rue
re turn Fal s e

Green bar Done. Let's be a bit more thorough and expand our tests to check if multiple movies in the
list bother us.
c la s s Te st Movi e Lis t (unit test .T e s t C a s e ) :
d e f se tU p(se l f):
se lf .mov i eLi s t = M ovie Li s t ( )
se lf .mov i eLi s t.add Movi e( M o v i e ( ' S t a r W a r s ' ) )
d e f te st OneM o vie L ist(s elf) :
se lf .ass e rtE q uals( self .m o v i e L i s t . g e t S i z e ( ) , 1 )
d e f te st Cont a ins M ovie( self ):
se lf .fai l Unl e ss(se lf.m ov i e L i s t . c o n t a i n s M o v i e ( ' S t a r W a r s ' ) )
d e f te st NotC o nta i nsMov ie(s el f ) :
se lf .fai l If( s elf.m ovie Li s t . c o n t a i n s M o v i e ( ' S t a r T r e k ' ) )
d e f te st Mult i ple M ovies InLi st ( s e l f ) :
sel f. asse r tEq u als(s elf. mo v i e L i s t . g e t S i z e ( ) , 1 )
sel f. movi e Lis t .addM ovie (M o v i e ( ' U n C h i e n A n d a l o u ' ) )
sel f. asse r tEq u als(s elf. mo v i e L i s t . g e t S i z e ( ) , 2 )
sel f. movi e Lis t .addM ovie (M o v i e ( ' R u n L o l a R u n ' ) )
sel f. asse r tEq u als(s elf. mo v i e L i s t . g e t S i z e ( ) , 3 )
sel f. fail U nle s s(sel f.mo vi e L i s t . c o n t a i n s M o v i e ( ' U n C h i e n A n d a l o u ' ) )
sel f. fail I f(s e lf.mo vieL is t . c o n t a i n s M o v i e ( ' S t a r T r e k ' ) )
d e f te ar Down ( sel f ):
sel f. movi e Lis t =None

Green bar This last test is somewhat like an acceptance test since it tests multiple scenarios. Many
people would argue against this level of redundancy, but for my money I believe that tests should be
fragile and that some level of acceptance test should be expressed while unit-testing the application. The
redundant asserts could certainly be factored out into private methods if one wishes.

Amazon

Prev

don't be afraid of buying books

Next

Chapter 26. VBUNIT


WITH KAY PENTECOST
This chapter presents vbUnit, the xUnit family member for the Visual Basic platform. Kay Pentecost has
agreed to write this chapter, as she has been doing TDD in VB for some time now.

Amazon

Prev

don't be afraid of buying books

Next

THE FRAMEWORK
This chapter presents vbUnit3 Professional, the xUnit family member for Visual Basic. There are several
vbUnit implementations. The one used in this chapter is vbUnit3 Professional, version 3.06.02. vbUnit3
Professional was written by Bodo Maass, and is a commercial product. It's nicely integrated with the VB
IDE, and very easy to use, so I think it's worth it. There is also a free version of vbUnit Basic on Maass'
site at [URL 51]. Other versions of vbUnit are available. You can find them at [URL 11].
Installing the vbUnit3 Professional software installs it as an add-in to Visual Basic. This adds three menu
items to the add-ins menu: Show vbUnit Window (which changes to Hide vbUnit Window when the
window is displayed), Run vbUnit, and vbUnit Options.
To create a new project with vbUnit, select New Project from the File menu. On the New Project window,
select vbUnitTestDLL as the project type. This opens a vbUnit project (the default name is
vbU n itTes tDL L) with two classes: the first test fixture (vb uT e st Fi x tu r e) and the test suite
(vbu T estSu ite ). You can and should rename these classes and files to be meaningful in your project.
The fixture implements the I Fixt ure interface and instantiates a variable for the I A ss er t class. This
gives us a selection of assertion methods, including:
Ass e rt.Ve rif y( Boo le an)
Ass e rt.St rin gs Equ al (exp ecte d, a c t ual )
Ass e rt.Lo ngs Eq ual (e xpec ted, act u a l)

As expected, the A ss ert methods each have the optional message parameter.
The IFix t ure interface provides two methods: I Fix t u re _S et u p and I Fi xt u re _T e ar Do w n. As
you'll see, you can use these methods to put common set up and tear down code for the tests when we
refactor in the project. I Fi xtur e_Se tu p runs before every test, and IF i xt ur e _T ea r Do wn runs after
every test.
The default Fixture already has a failing test:
Pub l ic Su b T es tFa il ()
m A ssert .Ve ri fy Fa lse, "he llo"
End Sub

To get your first green bar, you need to change the preceding Fa ls e to T ru e.

Amazon

Prev

don't be afraid of buying books

Next

EXAMPLE
Getting Started with vbUnit
We start by opening a new project in VB6. We select vbUnitTestDLL as the type of project. This creates
a project (that I renamed to vbu Movie ), a test fixture (renamed to v b u M o v i e F i x t u r e ), and a test
suite (renamed to vb u Movi e Suit e). Figure 26.1 shows the environment. The vbUnit window is docked
above the immediate window.

Figure 26.1. The Visual Basic environment.


[View full size image]

We expect that clicking the Run button will give us a red bar, but instead it gives us a message saying
that a user-defined type is not found. That's because we changed the name from v b u T e s t F i x t u r e to
v bu M ov ie Fix t ure. The test suite vbu M o v i e S u i t e is looking for the old name. We can fix that by
changing the code in the vbu M ovie Sui t e to look for v b u M o v i e F i x t u r e .
s ui t e. Ad dFi x ture New v buMo vieF i x t u r e

Now, clicking the Run button on the vbUnit window gives us a red bar. It failed. But we haven't really
done anything yet...so what's wrong?
Nothing's wrong. I like to start with the failing test provided by vbUnit...just to confirm that it's set up

and working correctly (see Figure 26.2).

Figure 26.2. The vbUnit window with a failed test.

To find the method that failed, we double-click on the test result line:
v bu M ov ie Fix t ure. T estF a il : Ver i f y f a i l e d : h e l l o

which brings us to the highlighted line of code:


m As s er t. Ver i fy F a lse, "hel lo"

Aha! If Ve ri fy is Fa l se, which it is here, the test will fail. Now we've confirmed that vbUnit is working
correctly...so we can change the Fal se to T r u e , and get a green barthe passing test.
Now the vbUnit window has the text:
O K ( 1 Te st, 1 As s erti o n) F inis h e d i n 3 . 8 2 1 s e c o n d s

So now, having warmed up with a small test that vbUnit is correctly installed and the project is running,
we're ready to start the real work.

Creating the MovieList Functionality


Our first test will be the test for an empty M o v i e L i s t . And it looks pretty much like the first Java test:

m As s er t. Lon g sEqu a l 0, Empt yLis t . S i z e , " S i z e o f e m p t y l i s t s h o u l d b e 0 . "

Next, we need to dimension Empt yList as a M o v i e L i s t .


D im Em pt yLi s t As New M ovie List
m As s er t. Lon g sEqu a l 0, Empt yLis t . S i z e , " S i z e o f e m p t y l i s t s h o u l d b e 0 . "

This won't compile, of course. We have no class M o v i e L i s t yet. The error message is:
u se r -d ef ine d typ e not foun d

So, we add a class to the test and name it M o v i e L i s t . Now the test fails again because we have no
method called Size :
M et h od o r d a ta m e mber not foun d

At this point, the test requires a Mo vie L i s t class, which we gave it, and a method in M o v i e L i s t
called Siz e. We know that the method should return zero for this case. And that's all we really know.
Oh, we figure the M ovi e List object will eventually hold a list of movies. And the size of the
M ov i eL is t will be the number of movies it holds. If we were doing some other style of programming,
we might create some movies, put them in the M o v i e L i s t , and count them, wouldn't we? Well, then
we'd have to decide what a movie looked like, and how to put movies into the list, and so on and so on
and so on.
But all the test is asking for is a Siz e method that returns zero. So that's all we have to do. We do the
simplest thing that could possibly work, and create a S i z e method that returns zero.
P ub l ic F unc t ion S ize( ) As Long
S i ze = 0
E nd Fu nc tio n

And run the test. And if we typed everything correctly, we get a green bar!
Once the test runs, we can look at the code for opportunities to refactor, but there's not much here to
do. We're ready to do the next test, which we'll call T e s t S i z e A f t e r A d d i n g O n e , just as we saw in
Chapter 10. This test will confirm that when we add a movie to the list, the list knows there's one movie
in there. Our assertion will be:
m As s er t. Lon g sEqu a l 1, oneI temL i s t . S i z e , " L i s t s h o u l d h a v e 1 "

To set up for the assertion we need to:


D im on eI tem L ist A s Ne w Mov ieLi s t
D im St ar War s As M ovie
S et St ar War s = N e w Mo v ie
o ne I te mL ist . Add S tarW a rs

The compiler will complain until we have a M o v i e class and an A d d method, so we add a M o v i e class
and put an empty A d d method in M ovi e L i s t . Now, running the tests, we get a red bar.
It failed because the Size method is only returning zero, and the A d d method doesn't do anything.
So we're going to let the Add method increment a counter that tracks how many movies we've added.
Notice that at this point we don't care anything else about the movie or the movie listjust that when
we add a movie the movie list knows it has one more movie.

So we'll create a private variable, N umb e r O f M o v i e s .


P ri v at e Num b erOf M ovie s As Long

Now we can increment Num b erOf Movie s in the A d d method, and have the S i z e method return the
number of movies.
Our M ov ieL i st class looks like this now:
P ri v at e Num b erOf M ovie s As Long
P ub l ic F unc t ion S ize( ) As Long
S i ze = Nu m berO f Movi e s
E nd Fu nc tio n
P ub l ic F unc t ion A dd(a M ovie As M o v i e )
N u mb er OfM o vies = Nu m berO fMov i e s + 1
E nd Fu nc tio n

Do we think the next test will run correctly the first time? Let's try it:
P ub l ic S ub T estS i zeAf t erAd ding T w o ( )
D i m tw oIt e mLis t As N ew M ovie L i s t
D i m St arW a rs A s Mov i e
D i m St arT r ek A s Mov i e
S e t St arW a rs = New M ovie
S e t St arT r ek = New M ovie
t w oI te mLi s t.Ad d Sta r Wars
t w oI te mLi s t.Ad d Sta r Trek
m A ss er t.L o ngsE q ual 2 , tw oIte m L i s t . S i z e , " L i s t s h o u l d h a v e 2 "
E nd Su b

As we expected, this gives us a green bar. Now, looking at the code for opportunities to refactor, we
see a lot of duplication. What's duplication? Duplication appears whenever we do something more than
once. . . even when we use different names in different methods. We're dimensioning a new
M ov i eL is t, for example, in almost every method:
D im Em pt yLi s t As New M ovie List
D im on eI tem L ist A s Ne w Mov ieLi s t
D im tw oI tem L ist A s Ne w Mov ieLi s t

In each case, it's just a Movi e List , although we named methods to tell us what kind of a M o v i e L i s t
they are.
So, we can move this to the Set up method in v b u M o v i e F i x t u r e . We also dimension and instantiate
two M ov ie objects, so we can put that code in the S e t u p , too. We'll put the D i m statements in the
G en e ra l section of the class so they have class-wide scope.
P ri v at e Sta r Wars As M o vie
P ri v at e Sta r Trek As M o vie
P ri v at e mov i eLis t As M ovie List
P ri v at e Sub IFix t ure S etup (ass e r t A s I A s s e r t )
S e t mA sse r t = a sser t
S e t Mo vie L ist = New M ovie List
S e t St arW a rs = New M ovie
S e t St arT r ek = New M ovie
E nd Su b

And, of course, we want to release memory in T e a r D o w n :


P ri v at e Sub IFix t ure T earD own( )
S e t mo vie L ist = Not h ing
S e t St arW a rs = Noth i ng

S e t St arT r ek = Noth i ng
S e t mA sse r t = N othi n g
E nd Su b

When we press the run button, if it still gives us a green bar, we can delete the duplicated code so the
dimension statements and the instantiations go away. All that is left in each test is the M o v i e L i s t . A d d
method, and the assert that we are testing.
Now we know how many movies are in the M o v i e L i s t . . . but we don't know anything about the
M ov i eL is t other than that.
So our next test will be to see if, once we put a movie in the M o v i e L i s t , if M o v i e L i s t knows it's in
there.
P ub l ic S ub T estC o nten t s
m o vi eL ist . Add S tarW a rs
m A ss er t.V e rify movi e List .Con t a i n s M o v i e N a m e ( " S t a r W a r s " )
E nd Su b

The compiler will remind us that we haven't written a contains method, yet. So we create a method in
M ov i eL is t called C ont ai nsMo vieNa m e .
P ub l ic F unc t ion C onta i nsMo vieN a m e ( N a m e O f M o v i e T o C h e c k F o r A s S t r i n g ) A s B o o l e a n

And, of course, the simplest thing we can do is to have C o n t a i n s return True.


C on t ai ns Mov i eNam e = T r ue

As I do this, I really like the feeling I get when the green bar appears. The more functionality I add, the
more confidence the green bar gives me.
We don't actually have to refactor, yet. We can add another test to check that a movie that has not
been added returns F a lse when we call C o n t a i n s M o v i e N a m e . This time, we don't call the A d d
method.
P ub l ic S ub T estN o tCon t ains
m A ss er t.V e rify Not ( Movi eLis t . C o n t a i n s M o v i e N a m e ( " S t a r W a r s " ) )
E nd Su b

When this fails, we can write the functionality that actually checks the contents of the M o v i e L i s t .
P ub l ic F unc t ion C onta i nsMo vieN a m e ( N a m e O f M o v i e T o C h e c k F o r A s S t r i n g ) A s B o o l e a n
D i m mo vie As M o vie
C o nt ai ns = Fal s e
F o r Ea ch m ovie In M o vies
If m ovi e .tit l e = N ameO fMov i e T o C h e c k F o r T h e n
C ont a insM o vieN a me= True
E xit For
En d If
N e xt m ovi e

We haven't dimensioned Movies yet. Movies will be a collection that will hold the movies for MovieList.
So we dimension Movies in the general section of the MovieList class, so it's available to the entire
class.
P ri v at e Mov i es A s New Coll ecti o n

Now that we've got a collection, we can refactor the add method to actually add a movie to movieList,
instead of just incrementing NumberOfMovies. Then, we can use the Collection method, Count, to find
out the Size of the MovieList.
First, we add a line to the MovieList.Add method:
M ov i es .A dd

We see that we're incrementing Num ber O f M o v i e s to get the M o v i e L i s t . S i z e . Now, however, we
have the collection Mo v ies, so we can use the C o l l e c t i o n C o u n t method to get the number of
movies. We add the line:
S iz e = M ovi e s.Co u nt

to the Mo vi eLi st .Si z e method and delete the


S iz e = N umb e rOfM o vies

The first thing we need here is a movie title. So we'll add T i t l e properties to M o v i e :
P ri v at e mTi t le A s Str i ng
P ub l ic P rop e rty G et T i tle( ) As S t r i n g
T i tl e = m T itle
E nd Pr op ert y
P ub l ic P rop e rty L et T i tle( aTit l e A s S t r i n g )
m T it le = a Titl e
E nd Pr op ert y

And we'll add the title to the movie in the I F i x t u r e S e t U p :


S ta r Wa rs .Ti t le = "Sta r War s"
S ta r Tr ek .Ti t le = "Sta r Tre k"

Green bar!!
This runs and gives us a green bar, so we can delete the N u m b e r O f M o v i e s dimension statement, and
the assignment of N u mber O fMov ies to Size.
And here we have to do some thinking. Is a M o v i e ever going to exist without a title? No. We don't
want any titleless movies hanging around. . . the only time we'd have a titleless movie would be some
sort of movie object we use as an iterator, and it would never be directly instantiated. So a Movie must
have a title. We can't do that with a constructor like we could in other object-oriented languages.
To make sure that every movie has a title, we need some sort of factory object to create movies. Let's
call it St ud io, and give it a method of M a k e N e w M o v i e .
Now, of course, we hear our pair yelling "Test first, test first!"
OK. Point taken. Create a new test fixture called v b u T e s t S t u d i o F i x t u r e . Here's the test:
First, the assert, since we do that before anything else.
m As s er t. Str i ngsE q ual " Star War s " , S t a r W a r s . T i t l e , " T i t l e s h o u l d b e ' S t a r W a r s ' "

Then we add the assignment, before the assert:


S et St ar War s = S t udio . make NewM o v i e ( " S t a r W a r s " )

To get this to run, we need the Studi o class, the m a k e N e w M o v i e method. We already have the Title
property in Movie.
So we dimension a Stu d io object, and we create the S t u d i o Class.
We put this in the vbuTestStudioFixture:
D im St ud io A s Ne w Stu d io

And we create this in the Studio Class:


P ub l ic F unc t ion m akeN e wMov ie(M o v i e T i t l e A s S t r i n g ) A s M o v i e
D i m mo vie As N e w Mo v ie
m o vi e. Tit l e = M ovie T itle
S e t ma keN e wMov i e = m ovie
E nd Fu nc tio n

Now we change the assignments in the I F i x t u r e _ S e t U p of the v b u M o v i e F i x t u r e for the new


functionality.
P ri v at e Stu d io A s New Stud io
S et St ar War s = S t udio . make NewM o v i e ( " S t a r W a r s " )
S et St ar Tre k = S t udio . make NewM o v i e ( " S t a r T r e k " )

Green bar!
Now we can modify the IFix t ure_ Set U p in v b u T e s t M o v i e to use the new functionality:
P ri v at e Sub IFix t ure_ S etup (ass e r t A s I A s s e r t )
S e t mA sse r t = a sser t
S e t Mo vie L ist = New Movi eLis t
S e t St arW a rs = Stud i o.ma keNe w M o v i e ( " S t a r W a r s " )
S e t St arT r ek = Stud i o.ma keNe w M o v i e ( " S t a r T r e k " )
E nd Su b

OK, now we have three classes: St udi o , M o v i e , and M o v i e L i s t . These classes allow us to add
movies to a list and check that the list contains them...and the code is pretty straightforward and clean.
Here are the classes in their most recent form:
First, Mo vie :
O pt i on E xpl i cit
P ri v at e mTi t leAs S trin g
P ub l ic P rop e rty G et T i tle( ) As S t r i n g
T i tl e = m T itle
E nd Pr op ert y
P ub l ic P rop e rty L et T i tle( aTit l e A s S t r i n g )
m T it le = a Titl e
E nd Pr op ert y

Next, M ov ieL i st:


O pt i on E xpl i cit
P ri v at e Mov i es A s New Coll ecti o n

P ub l ic F unc t ion S ize( ) As Long


S i ze = Mo v ies. C ount
E nd Fu nc tio n
P ub l ic S ub A dd(a M ovie As m ovie )
M o vi es .Ad d aMo v ie
E nd Su b
P ub l ic F unc t ion C onta i nsMo vieN a m e ( N a m e O f M o v i e T o C h e c k F o r A s S t r i n g ) A s B o o l e a n
D i m mo vie As m o vie
C o nt ai nsM o vieN a me = Fals e
F o r Ea ch m ovie In M o vies
If m ovi e .Tit l e = N ameO fMov i e T o C h e c k F o r T h e n
Co nta i nsMo v ieNa m e = True
Ex it F or
En d If
N e xt m ovi e
E nd Fu nc tio n
P ub l ic F unc t ion m akeN e wMov ie(M o v i e T i t l e A s S t r i n g ) A s m o v i e
D i m mo vie As N e w mo v ie
m o vi e. Tit l e = M ovie T itle
S e t ma keN e wMov i e = m ovie
E nd Fu nc tio n

Finally, St ud io:
O pt i on E xpl i cit
P ub l ic F unc t ion m akeN e wMov ie(M o v i e T i t l e A s S t r i n g ) A s m o v i e
D i m mo vie As N e w mo v ie
m o vi e. Tit l e = M ovie T itle
S e t ma keN e wMov i e = m ovie
E nd Fu nc tio n

There are more things we can do: we can change M o v i e L i s t into a real collection; we can set the
M ov i e class to be P ubl ic Not Crea t e a b l e so that programs outside of the DLL can only create
M ov i es by going through Stud io; we can add functionality to M o v i e , M o v i e L i s t , and S t u d i o as it's
needed.
But we don't need it yet, so this is a good place to stop!

Amazon

Prev

don't be afraid of buying books

Part v: APPENDICES

Next

Amazon

Prev

don't be afraid of buying books

Next

Appendix A. EXTREME PROGRAMMING


'Tis noble Kent, your friend.
King Lear, Act 5 Scene 3 William Shakespeare
This appendix does not try to be an exhaustive overview of eXtreme Programming (XP). Its purpose is
to provide a very brief introduction to XP as background to the discussion of Test-Driven Development.

Amazon

Prev

don't be afraid of buying books

Next

THE AGILE REVOLUTION


Since the late 1990s the field of software development has been undergoing something of a revolution.
It had been fairly common knowledge for some time among more open-minded practitioners that the
field was in trouble. Projects were delivered late, cost much more than was expected, and/or did not
deliver the originally requested features. Additionally, programming was often a stressful job with
programmers feeling overworked and under appreciated.
In 1994 The Standish Group published a report that brought these problems into the light. The nowfamous Chaos Report [21] documented that only 16 percent of projects were completed successfully. A
follow-up report[22] included the significant finding that the likelihood of a project succeeding
diminished the more people worked on it, the more it cost, or the longer it took. What did successful
projects look like? They were low cost, short, and done by small teams.
Around this time, several thought leaders in the field began to develop software development processes
that increased the chances of success. Two of the most notable were Ward Cunningham and Kent Beck,
who called their idea "eXtreme Programming."
In 2001 several of the people involved in this development approach met to discuss this trend and their
ideas. This meeting led to the Agile Manifesto[URL 6], which is included as part of this chapter, in a
sidebar. The group that wrote the manifesto forms the core of what is now The Agile Alliance[URL 5].
For more information on Agile Software Development, see [13], [24], [33].
How are agile methods different from traditional ones? At first glance it seems that there is nothing new
here. Most seasoned developers will see practices that they have been using some time. So what's the
big deal?
Traditional (i.e., non-agile) methodologies advocate and usually try to enforce a defined and repeatable
process. This is based on the belief that software can be manufactured. In essence, these are industrialera approaches.
Agile methodologies are, in contrast, post-industrial. They use introspection, retrospection, and
adaptation to allow people to self-organize based on the application of a set of practicest hat lead to an
emerging process. This idea is expressed in one of the Agile Alliance's principles: "The best architectures,
requirements, and designs emerge from self-organizing teams"[URL 6]. See the sidebar for all of the
principles behind the Agile Manifesto.

Manifesto for Agile Software Development


We are uncovering better ways of developing software by doing it and helping others do it.
Through this work we have come to value:
Individuals and interactions

over

processes and tools

Working software

over

comprehensive documentation

Customer collaboration

over

contract negotiation

Responding to change

over

following a plan

That is, while there is value in the items on the right, we value the items on the left more.
Kent Beck

JamesGrenning

Robert C. Martin

Mike Beedle

Jim Highsmith

Steve Mellor

Arie van Bennekum

Andrew Hunt

Ken Schwaber

Alistair Cockburn

Ron Jeffries

Jeff Sutherland

Ward Cunningham

Jon Kern

Dave Thomas

Martin Fowler

Brian Marick

2001, the above authors. This declaration may be freely copied in any form, but only in
its entirety through this notice.

Amazon

Prev

don't be afraid of buying books

Next

EXTREME PROGRAMMING
eXtreme Programming, commonly referred to simply as XP, is one of the most agile of the agile
processes.
XP is defined by four values and a set of practices. However, XP is more than this, more than just doing
the practices. It is a way of creating software that embodies the values. The practices are activities that
we do in order to learn XP. Beck sees the practices as being much the same as etudes that help one
learn and internalize techniques in playing a musical instrument.

Amazon

Prev

don't be afraid of buying books

Next

THE FOUR VARIABLES


No discussion of XP can begin without talking about the four variables: cost, time, quality, and scope.
We look at each, how the yare interrelated, and how they relate to a project and its risk.
Cost Every project has a cost. This includes equipment, facilities, and man-hours.
Especially man-hours.

Principles Behind the Agile Manifesto


Our highest priority is to satisfy the customer through early and
continuous delivery of valuable software.
Welcome changing requirements, even late in development. Agile
processes harness change for the customer's competitive advantage.
Deliver working software frequently, from a couple of weeks to a
couple of months, with a preference to the shorter timescale.
Business people and developers must work together daily throughout
the project.
Build projects around motivated individuals. Give them the
environment and support they need, and trust them to get the job
done.
The most efficient and effective method of conveying information to
and within a development team is face-to-face conversation.
Working software is the primary measure of progress.
Agile processes promote sustainable development. The sponsors,
developers, and users should be able to maintain a constant pace
indefinitely.
Continuous attention to technical excellence and good design enhances
agility.
Simplicitythe art of maximizing the amount of work not doneis
essential.
The best architectures, requirements, and designs emerge from selforganizing teams.
At regular intervals, the team reflects on how to become more
effective, then tunes and adjusts its behavior accordingly.

Time Every project takes time.

Quality Every project results in a system with a certain level of quality.

Scope Every project consists of a certain set of functionality.

The really interesting thing is how these variables interact. If you increase cost by adding people to the
project, velocity will often decrease (due to the costs of integrating the additional people) and, hence,
time will increase. On the other hand, increasing the cost by buying faster or better equipment or
facilities can boost your velocity and thus decrease time. You cannot decrease time simply by dumping
more money into a project, especially early in the project. In fact, you are limited in what you can
achieve by controlling cost.
Time is often dictated by external constraints such as trade shows, investor requirements, and business
commitments. Because of this, time is often relatively fixed.
Quality can be variable, but lowering the quality of the system has serious negative impact. No worker is
happy for long if they are coerced into producing goods of poor quality. This can lead to a serious morale
issue. Also, low quality code is harder to extend and maintain. If you try to shorten the time required by
limiting quality, it will surely backfire.
Scope is the thing that is most easily controlled. This entails adding or removing functionality from the
requirements of the project. By using a few simple techniques, XP makes this work very well. One
important technique is to always be working on the most important outstanding feature, according to the
customer. That ensures that if unimplemented features are later removed (thereby reducing scope), the
most important things have already been done.
One mistake that people make is thinking that they can control all four variables. This is impossible.
Cost, time, quality, and scope are interrelated such that the value of any one depends on the values of
the others. What this means in practical terms is that you can control at most three of the four, but
never all four. If you attempt to control all four, your project will most likely fail and will certainly be a
miserable one to work on.

Amazon

Prev

don't be afraid of buying books

Next

THE VALUES
The reason XP is so successful lies in the four values it is built upon:
1. Communication
2. Simplicity
3. Feedback
4. Courage
We'll examine each of these values in turn.

Communication
Communication is of paramount importance in XP. This is evidenced in several of its practices:
Programming is done in pairs.
There is a stand-up meeting every morning.
The team works in an open space.
There is a customer in the room with the team.
Heavy use is made of shared white-boards.
Information of general interest is displayed in a prominent location.
These practices promote open, honest communication between programmers, between programmers and
customers, and with management.

Simplicity
The value of simplicity cannot be overstated. Keeping the code and the design as simple as possible
increases the clarity, understandability, extendability, and maintainability of the system.
Exactly what is simplicity? In the context of XP, Beck[8]defines the simplest system by using four criteria
(in order of decreasing importance). The code is simple if it:
runs all the tests
reveals all the intention
has no duplication, and
uses the fewest possible classes and methods.

Feedback
Without honest, continuous feedback everything else falls apart. Feedback is what keeps everyone in
sync. It is what enables the programmers to deliver the system that the customer really wants.
Feedback takes many forms and occurs at many levels on many timescales. This can range from the
feedback you get from running your tests on a minute-by-minute basis, the ongoing feedback from your
pair-programming partner, the feedback from the team in the daily meeting, through to feedback from
the customer during iteration planning when they tell you how you're doing in terms of overall direction.

Courage
Courage is required to do XP. It takes courage to make sweeping changes to working code to make it
clearer or just better. Having a broad test suite gives you confidence, but courage still has to come from
inside. It takes courage to tell your customer that they can't have another feature added without
deferring to something else. It takes courage to throw away code.

Amazon

Prev

don't be afraid of buying books

Next

THE PRACTICES
In this section we'll have a brief look at each of the XP practices. For more detail see [6], [8], [26],
and [URL 12].

Whole Team
Software development is a cooperative game[13]. There are several players, including programmers,
coach, customers, management, and users. All these players are on the same team, and the goal is for
everyone to win. Everyone works together to do that. On a practical note, the core players
(programmers, coach, and ideally customer) sit in the same room so there is minimal barriers to
communication.

Planning Game
There are two aspects to the planning game: release planning and iteration planning. The former deals
with what can be accomplished by a certain date, while the latter deals with what to do next on a dayto-day basis.

Release Planning
In an XP project, requirements exist in the form of stories that are written by the customer. Each story
briefly describes a single aspect of functionality (sometimes called a feature) that is testable. The
programmers estimate the cost of each story. Based on that information and their knowledge of the
priority [1] of each story, the customer sketches out the order in which stories should be done. We accept
that this plan is inaccurate and we will adjust it as we go, as the velocity of the team changes, and as
the customer changes their mind about scheduled stories or comes up with new ones. This set of stories
is then divided into iterations (each of which last between one and three weeks) based on how much
work the team can get done per iteration.
[1] The priority of a story is based on its business value to the customer.

Iteration Planning
Each release is made up of a series of iterations. At the beginning of each iteration the development
team sits down and brainstorms each story that is scheduled for that iteration. The result is a set of
tasks for each story. These tasks are then estimated. [2] Then each programmer signs up for a selection
of tasks that they can accomplish in this iteration. They know how much work they got done during the
previous iteration and assume that they will get the same amount done this time. This same rule of
thumb is used to estimate how much the team as a whole can accomplish in an iteration. This is then
used in release planning to divide stories into iterations. They sign up for that much work. This is the
concept known as yesterday's weather. ..odds are that you'll get the same amount done today as you
did yesterday. If a programmer finds that they have time left toward the end of the iteration, they will
look for more work. If a programmer is running out of time (i.e., has too much work), they will defer
some or pass it to another programmer who has time left.
[2] Task estimates are at a finer level of granularity than story estimates, and are more accurate
since there is now more information to work with.

The key thing to remember about the planning game is that the planning is more important than the
plan.

Small Releases
By keeping releases small and frequent we get more rapid feedback from users. Releases should be no
larger than several months' worth of work. As mentioned before, releases are split into iterations. While
the size of releases can vary depending on external constraints, iterations are a fixed size: between one
and three weeks. Historically, two weeks has been the most popular iteration size, but recent experiences
are favoring one-week iterations.

Customer Tests
As stated in previous sections, the customer writes the requirements as a set of stories. They also write
a test (or tests) for each story so that everyone knows when that story has been successfully
implemented in full. These are the customer tests and are also known as acceptance tests since they
indicate when a story's implementation is acceptable. Ideally, the customer has their tests ready for the
programmers to use (i.e., run) before the associated stories are scheduled.

Simple Design
Design for today, not for some potential future. By working test-first we constrain our design to what is
required to pass the test. By refactoring as required, we keep the design clean and simple. That's one
reason why I claim this is a book about design, not about testing.

Pair Programming
Every line of code (test and production) is written by two programmers sitting at a single computer. The
most common, uninformed objection to pair programming is that you have two people doing one
person's work. But it's more than two people writing code together. There is an ongoing, real-time code
review in progress. The partner (the person not typing) can be thinking strategically, while the driver
(the person typing) thinks tactically. Because of this, the partner can suggest what the next test should
be, what names should be used, where refactoring is warranted, etc. Also, because there is always
someone available to talk to, problems can be talked through and brainstormed as needed.
There have been studies on the effectiveness of pair programming. One was presented at the XP2000
conference[37, Chapter 14] which found that using pair programming took only 15 percent more time
and that the extra development cost would be repaid in quicker and cheaper testing, QA, and support.
Another study was presented at XP2002[38] which found that pair programming increases job satisfaction
among programmers.
The social aspect of pair programming should not be undervalued either. Programmers are people, and
people are social animals.

Test-Driven Development
Write tests first and let that drive your design and coding. That's what this book is all about. One of the
beneficial side effects of TDD is that you end up with a comprehensive test suite. This gives you
confidence to refactor. If the tests run before refactoring (and they must)and they run after, you can be
confident that you haven't changed the behavior of the code.

Design Improvement

As you program, you write a small test (just enough to fail), then write a small bit of code (just enough
to pass the failing test). This can lead to poorly designed or poorly structured code. So the next step is
to refactor to make the code clean again.
By refactoring as required, we continuously make the design better, a little at a time. This makes the
code as simple, clear, and intent-revealing as possible.

Collective Code Ownership


Everybody owns everything. Anybody can change any code as required. This, especially combined with
pair programming, allows and encourages everyone to get familiar with all (or at least most) of the
codebase. It also enables refactoring in that if anyone sees a problem, they can fix it. It lets the team
work as fast as possible since you never have to wait for the owner of the code to make the change you
need.

Continuous Integration
In XP you never go for very long without integrating your work with the codebase. There are several
beneficial results:
Integration is never a headache because there are minimal differences between the codebase and
your work.
The codebase is always very close to the most current state of the system, typically within
hours.
Your working copy is never very far from the baseline codebase in the repository so you never
have the chance to diverge much.
It is very important to stay this close to the codebase because of collective ownership. Anyone is able to
work on any part of the code. The longer you go without integrating, the more likely it is that someone
has made changes that conflict with yours.
At the very least, you should integrate after you finish each task. It is even better to integrate after
each test-code-refactor cycle. That way you are integrating every 1530 minutes. When you're doing
this, it's especially important to have your tests run quickly, because before integrating you have to
verify that all the tests run.
Never, never, NEVER leave anything unintegrated at the end of the day. If you can't integrate before you
leave for the day, throw it out and start fresh in the morning. If a task takes longer than a day to do,
either the task is too complex (and has to be broken up) or your solution is too complex (and you need
to find a simpler one). In either case, start over.

Coding Standard
In order to support collective ownership, everyone on the team needs to conform to the same coding
standard. This allows everyone to be comfortable working on any of the code since it all looks the same.
What this standard is doesn't matter. The main thing is that everyone adheres to it. For obvious reasons,
it makes sense to use a standard close to something that is common in the industry.

Common Vocabulary
Everyone involved in the project must use a common vocabulary for talking about the project. This has

often been accomplished through the use of a metaphor.


The metaphor is a shared, simple description of the system. It must be shared by everyone involved and
so should be cast in terms that everyone understands. For example, the C3 project at Chrysler (the
original XP project) used a metaphor of an assembly line to describe the payroll system they were
building. Everyone at Chrysler understood assembly lines.
Not only does the metaphor provide a common understanding of the system's operation, it also provides
a source for names in the system. This tightly couples the code itself with the shared understanding that
has developed.

Sustainable Pace
If you can't stay awake, you can't program. If you can't think, you can't program. Our industry has
made a hero of the programmer that works until the wee hours of the morning, kept awake (and
possibly alive) on a diet of pizza, chips, and cola. Nobody can last long that way. On an XP project, you
stop work when you are ready to. When you've put in a good day you go home, get some rest, and
have some fun. That way you can come back the next morning rested, refreshed, and ready to work at
your top effectiveness. By doing this, you can keep up the pace indefinitely, and not worry about burning
out before the project is completed.

Amazon

Prev

don't be afraid of buying books

Next

SUMMARY
eXtreme Programming is one of the most revolutionary movements in the history of programming. It
rejects the pomp and ceremony of the heavy methodologies that are standard in our industry. It turns
the focus away from processes that spell out what everyone on the project has to do, and when. It
eschews the use of complex tools and technological solutions in favor of people doing the simplest thing
that could work. Instead of trying to nail down and predict all requirements and timelines at the
beginning of the project, it encourages the customers to think up new requirements and change direction
throughout the project. Instead of trying to fully design the system before the first line of code is written,
it evolves the system in small steps, just enough at any one time.
In short, it lets programmers program, have a life, and have fun doing it.

Amazon

Prev

don't be afraid of buying books

Next

Appendix B. AGILE MODELING


WITH SCOTT AMBLER
We fortify in paper and in figures,
Using the names of men instead of men:
Like one that draws the model of a house
Beyond his power to build it; who, half through,
Gives o'er and leaves his part-created cost
A naked subject to the weeping clouds
And waste for churlish winter's tyranny.
Henry I, part 2, Act 1 Scene 3 William Shakespeare
Some people will tell us that "if you're taking a test-driven development (TDD) approach to development
that you don't need to model." Those people are completely and utterly wrong. These are probably the
exact same people who also told us that "you don't model on an extreme Programming (XP) project,"
and they were completely and utterly wrong about that, too. The best thing that can be said about
them, I guess, is that at least they're consistent.
In this chapter we explore the following issues:
The myths surrounding modeling
An introduction to Agile Modeling (AM)
What are agile models?
Agile Modeling and TDD

Amazon

Prev

don't be afraid of buying books

Next

THE MYTHS SURROUNDING MODELING


Before we explore the relationship between modeling and TDD, I first want to dispel some myths that
you may have about modeling. My experience is that the people who tell you that TDD and modeling
don't go together typically don't have a very good understanding of modern approaches to modeling,
and in particular have little or no experience with Agile Modeling (AM). In the August 2001 issue of
Software Development [URL 63] Scott explored the following misunderstandings that people have about
modeling:
Model = Documentation. The reality is that the concepts of model and document are
orthogonalwe can have models that aren't documents and documents that aren't
models. A sketch on the back of a paper napkin is a model, as is a drawing on a white
board, as is a collection of Class Responsibility Collaboration (CRC) cards, as is a lowfidelity user interface prototype built from flip chart paper and sticky notes. These are all
valuable models, yet questionable documents.

You Can Think Everything Through From the Start. Project teams suffering from this
myth often produce significant amounts of documentation instead of what their users
actually wantworking software that meets their needs. We need to recognize that we
can't think all the minutiae through, that the programmers likely won't follow the detailed
guidance provided by the models anyway, and that our models need to evolve over time
in an iterative and incremental manner. The fundamental reality is that only our code is
ever truly in sync with our code.

Modeling Implies a Prescriptive Software Process. The reality is that we can model in
an agile manner, as we'll see later in this chapter.

You Must "Freeze" Requirements. The good news is that by freezing our requirements
early in the life cycle, the customer is likely to get exactly what they asked for; the bad
news is that they likely won't get what they actually need. The reality is that change
happens, so we need to embrace this fact and act accordingly.

Your Design is Carved in Stone. This is similar to the desire to freeze requirements,
the main difference being that management wants to ensure that every developer is
marching to the same tune by following the design. The result is that developers either
build the wrong things, or build the right things the wrong way, to conform to the
design. The reality is that nobody is perfect; even the best designers aren't going to get
it right the first time. Furthermore, if we don't freeze the requirements, then by
implication we cannot freeze the designchanges to the requirements will force changes
to the design. The best way to look at it is that the design isn't finished for a given
release until we've shipped the code.

You Must Use a CASE Tool. I often create a model to think through an issue, such as
how we might architect one aspect of the system, allowing myself and/or my coworkers
to move forward and implement what we just modeled. As a result, I often don't need a
significant CASE tool to support my modeling effortsa white board or stack of index

cards often suffices. My experience is that CASE tools are fine as long as they provide
the best value for our investment in them, but that most of the time I simply don't need
one to model successfully. Yes, I'll often use tools such as Together/CC [URL 34] because
it generates significant amounts of Java scaffolding code and ERWin [URL 45] because it
generates database schemas. Both of these tools help me to fulfill the true purpose of
software development: the creation of software that meets the needs of my users. Having
said that, the vast majority of my modeling efforts are still done by using simple tools,
not sophisticated modeling tools.

Modeling is a Waste of Time. The reality is that we are very often more productive
sketching a diagram, developing a low-fidelity prototype, or creating a few index cards, in
order to think something through before we code it. Productive developers model before
they code. Furthermore, modeling is a great way to promote communication between
team members and project stakeholders because you're talking through issues, coming to
a better understanding of what needs to be built and building bonds between everyone
involved with the project in the process.

The World Revolves Around Data Modeling. Many organizations hobble their new
development efforts by starting with a data model. Often this is the way the organization
has always done things; the data community has a political death grip on the IT
department and therefore does everything in their power to ensure that they control the
software development projects, or the legacy database(s) are such a mess that we have
no other choice. The reality is that we have a wide variety of models at our disposaluse
cases, business rules, activity diagrams, class diagrams, component diagrams, user
interface flow diagrams, and CRC models to name a fewand data models are merely
one such option. We need to use the right model(s) for the job.

All Developers Know How to Model. Modeling skills are gained over years of
experience and only when a developer chooses to gain them. As the agile community will
tell us, people need to work together and to balance off one another's strengths.
Everyone should have the humility to understand that they don't know everything and
therefore they can always learn something important from everyone else: modelers can
learn details of a certain technology from a programmer and programmers can learn
valuable design and architecture techniques from modelers. My personal philosophy is that
everyone is a novice, including myself.

By understanding the myths surrounding modeling and dealing with them effectively, we put yourself,
our project team, and our organization in a position where we can develop software effectively. Agile
Modeling (AM) describes an effective approach to modeling and documentation that works well within a
TDD environment.

Amazon

Prev

don't be afraid of buying books

Next

AN INTRODUCTION TO AGILE MODELING (AM)


The Agile Modeling (AM) methodology [2] [URL 8] is a chaordic collection of practicesguided by
principles and valuesthat should be applied by software professionals on a day-to-day basis. AM is not
a prescriptive process, in other words, it does not define detailed procedures for how to create a given
type of model. Instead, it provides advice for how to be effective as a modeler. AM is touchy-feely in
that it is not hard and fastthink of AM as an art, not a science.
An agile modeler is anyone who models follows the AM methodology applying AM's practices in
accordance with its principles and values. An agile developer is someone who follows an agile approach
to software development. An agile modeler is an agile developer. Not all agile developers are agile
modelers.
AM has three goals:
1. To define and show how to put into practice a collection of values, principles, and practices
pertaining to effective, lightweight modeling. What makes AM a catalyst for improvement isn't the
modeling techniques themselvessuch as use case models, class models, data models, or user
interface modelsbut how to apply them productively.
2. To address the issue of how to apply modeling techniques on software projects taking an agile
approach, such as eXtreme Programming (XP) [8] or Feature Driven Development (FDD) [36].
Sometimes it is significantly more productive for a developer to draw some bubbles and lines to
think through an idea, or to compare several different approaches to solving a problem, than it is
to simply start writing code. There is a danger in being too code-centricsometimes a quick
sketch can avoid significant churn when we are coding.
3. To address how we can improve our modeling activities following a near-agile approach to
software development, and in particular project teams that have adopted an instantiation of the
Unified Process such as the Rational Unified Process (RUP) [30] or the Enterprise Unified Process
(EUP) [3]. Although we must be following an agile software process to truly be agile modeling,
we may still adopt and benefit from many of AM's practices on non-agile projects.

AM Values
The values of AM include those of XPcommunication, simplicity, feedback, and courageand extend it
with humility. It is critical to have effective communication within our development team as well as with
and between all project stakeholders. We should strive to develop the simplest solution possible that
meets all of our needs and to obtain feedback regarding our efforts often and early. Furthermore, we
should have the courage to make and stick to our decisions, and have the humility to admit that we
may not know everything, that others have value to add to our project efforts.

AM Principles
The principles of AM include the importance of assuming simplicity when we are modeling and embracing
change as we are working because requirements do in fact change over time. We should recognize that
incremental change of our system over time enables agility and that we should strive to obtain rapid
feedback on our work to ensure that it accurately reflects the needs of our project stakeholders. Agile
modelers realize that software is the primary goal, although they balance this with the recognition that
enabling the next effort is the secondary goal. We should model with a purpose, if we don't know why
we are working on something, then we shouldn't be doing so, and that we need multiple models in our
development toolkit in order to be effective. A critical concept is that models are not necessarily
documents, a realization that enables us to travel light by discarding most of our models once they have
fulfilled their purpose. Agile modelers believe that content is more important than representation, that
there are many ways we can model the same concept yet still get it right. To be effective modelers we
need to know our models. To be effective teammates we should realize that everyone can learn from
everyone else, we should work with people's instincts, and that open and honest communication is often
the best policy to follow to ensure effective teamwork. Finally, a focus on quality work is important
because nobody likes to produce sloppy work, and that local adaptation of AM to meet the exact needs
of our environment is important. The following summarizes the principles of AM.

Assume Simplicity
As we develop we should assume that the simplest solution is the best solution.

Content Is More Important Than Representation


Any given model could have several ways to represent it. For example, a UI specification could be
created using sticky notes on a large sheet of paper (an essential or low-fidelity prototype), as a sketch
on paper or a whiteboard, as a "traditional" prototype built using a prototyping tool or programming
language, or as a formal document including both a visual representation as well as a textual description
of the UI.

Embrace Change
Accept the fact that change happens. Revel in it; change is one of the things that make software
development exciting.

Enabling the Next Effort is Your Secondary Goal


Our project can still be considered a failure even when we deliver a working system to our userspart
of fulfilling the needs of our project stakeholders is to ensure that our system is robust enough so that it
can be extended over time. As Alistair Cockburn [13] likes to say, when we are playing the software
development game our secondary goal is to set up to play the next game.

Everyone Can Learn From Everyone Else


Agile modelers have the humility to recognize that they can never truly master something, there is
always opportunity to learn more and to extend your knowledge. They take the opportunity to work with
and learn from others, to try new ways of doing things, to reflect on what seems to work and what
doesn't.

Incremental Change
To embrace change we need to take an incremental approach to our own development efforts, to
change our system a small portion at a time instead of trying to get everything accomplished in one big
release. We can make a big change as a series of small, incremental changes.

Know Your Models


Because we have multiple models that we can apply as agile modelers we need to know their strengths
and weaknesses to be effective in their use.

Local Adaptation
It is doubtful that AM can be applied out of the box; instead, we will need to modify it to reflect the
environment, including the nature of the organization, the team, the project stakeholders, and the
project itself.

Maximize Stakeholder Investment


Our project stakeholders are investing resourcestime, money, facilities, and so onto have software
developed that meets their needs. Stakeholders deserve to invest their resources the best way possible
and not to have them frittered away by our team. Furthermore, stakeholders deserve to have the final

and not to have them frittered away by our team. Furthermore, stakeholders deserve to have the final
say in how those resources are invested or not invested. If it was our money, would we want it any
other way?

Model With a Purpose


If we cannot identify why and for whom we are creating a model, then why are we bothering to work on
it all?

Multiple Models
We have a wide range of modeling artifacts available to us. These artifacts include, but are not limited
to, the diagrams of the Unified Modeling Language (UML), structured development artifacts such as data
models, and low-tech artifacts such as essential user interface models [1].

Open and Honest Communication


People need to be free, and to perceive that they are free, to offer suggestions. Open and honest
communication enables people to make better decisions because the quality of the information that they
are basing them on is more accurate.

Quality Work
Agile developers understand that they should invest the effort to make permanent artifacts, such as
source code, user documentation, and technical system documentation of sufficient quality.

Rapid Feedback
Feedback is one of the five values of AM, and because the time between an action and the feedback on
that action is critical, agile modelers prefer rapid feedback over delayed feedback whenever possible.

Software is Your Primary Goal


The primary goal of software development is to produce high-quality software that meets the needs of
our project stakeholders in an effective manner.

Travel Light
Traveling light means that we create just enough models and documentation to get by.

Work With People's Instincts


As we gain experience at developing software, our instincts become sharper, and what our instincts are
telling us subconsciously can often be an important input into our modeling efforts.

AM Practices
To model in an agile manner we must apply AM's practices appropriately. Fundamental practices include
creating several models in parallel, applying the right artifact(s) for the situation, and iterating to another
artifact to continue moving forward at a steady pace. Modeling in small increments, and not attempting
to create the magical all encompassing model from our ivory tower, is also fundamental to our success
as an agile modeler. Because models are only abstract representations of software, abstractions that may
not be accurate, we should strive to prove it with code to show that our ideas actually work in practice

and not just in theory. Active stakeholder participation is critical to the success of our modeling efforts
because our project stakeholders know what they want and can provide us with the feedback that we
require. There are two fundamental reasons why we create models: either we model to understand an
issue (such as how to design part of the system) or we model to communicate what our team is doing
(or has done).The principle of assume simplicity is supported by the practices of creating simple content
by focusing only on the aspects that we need to model and not attempting to create a highly detailed
modeling, depicting models simply via use of simple notations, and using the simplest tools to create our
models. We travel light by discarding temporary models and updating models only when it hurts.
Communication is enabled by displaying models publicly, either on a wall or internal Web site, through
collective ownership of our project artifacts, through applying modeling standards, and by modeling with
others. Our development efforts are greatly enhanced when we consider testability, apply patterns
gently, and reuse existing artifacts. Because we often need to integrate with other systems, including
legacy databases as well as Web-based services, we will find that we need to formalize contract models
with the owners of those systems. The following summarizes the practices of AM.

Active Stakeholder Participation


Project success often requires a significant level of involvement by project stakeholderssenior
management needs to publicly and privately support our project, operations and support staff must
actively work with our project team towards making the production environment ready to accept our
system, other system teams must work with ours to support integration efforts, and maintenance
developers must work to become adept at the technologies and techniques used by our system.

Apply Modeling Standards


Developers should agree to and follow a common set of modeling standards on a software project. A
good source of modeling standards and guidelines is the book The Elements of UML Style [4] and [URL
64].

Apply Patterns Gently


Effective modelers learn and then appropriately apply common architectural, design, and analysis
patterns in their models. However, both Martin Fowler [17] and Joshua Kerievsky [28] believe that
developers should consider easing into the application of a pattern and apply it gently.

Apply the Right Artifact(s)


This practice is AM's equivalent of the adage use the right tool for the job; in this case we want to
create the right model(s) to get the job done. Each artifactsuch as a UML state chart, an essential use
case, source code, or data flow diagram (DFD)has its own specific strengths and weaknesses, and
therefore is appropriate for some situations but not others.

Collective Ownership
Everyone can work on any model, and ideally any artifact on the project, if they need to.

Consider Testability
When we are modeling we should be constantly asking ourselves "How are we going to test this?"
because if we can't test the software that we are building, we shouldn't be building it.

Create Several Models in Parallel


Because each type of model has its strengths and weaknesses, no single model is sufficient for our
modeling needs. By working on several at once we can easily iterate back and forth between them and
use each model for what it is best suited.

Create Simple Content


We should keep the actual content of our modelsrequirements, analysis, architecture, or designas
simple as we possibly can while still fulfilling the needs of our project stakeholders. The implication is
that we should not add additional aspects to our models unless they are justifiable.

Depict Models Simply


We should use a subset of the modeling notation available to usa simple model that shows the key
features that we are trying to understand, perhaps a class model depicting the primary responsibilities of
classes and the relationships between them, often proves to be sufficient.

Discard Temporary Models


The vast majority of the models that we create are temporary working modelsdesign sketches, low
fidelity prototypes, index cards, potential architecture/design alternatives, and soonmodels that have
fulfilled their purpose but no longer add value now that they have done so.

Display Models Publicly


This supports the principle of open and honest communication on our team because all of the current
models are quickly accessible to them, as well as with our project stakeholders because we aren't hiding
anything from them.

Formalize Contract Models


Contract models are often required when an external group controls an information resource that our
system requires, such as a database, legacy application, or information service. A contract model is
formalized with both parties mutually agreeing to it and ready to mutually change it over time if
required.

Iterate To Another Artifact


Whenever we find that we are having difficulties working on one artifact (perhaps we are working on a
use case and find that we are struggling to describe the business logic), that's a sign that we should
iterate to another artifact. By iterating to another artifact we immediately become unstuck because we
are making progress working on that other artifact.

Model in Small Increments


With incremental development we model a little, code a little, test a little, then deliver a little. No more
big design upfront (BDUF) where we invest weeks or even months creating models and documents.

Model to Communicate
One reason why we model is to communicate with people external to our team or to create a contract
model.

Model To Understand
The most important application of modeling is to explore the problem space, to identify and analyze the

requirements for the system, or to compare and contrast potential design alternatives to identify the
potentially most simple solution that meets the requirements.

Model With Others


Software development is a lot like swimmingit's very dangerous to do alone.

Prove it With Code


A model is an abstraction, one that should accurately reflect an aspect of whatever you are building. To
determine if it will actually work, we should validate that our model works by writing the corresponding
code.

Reuse Existing Resources


There is a wealth of information that agile modelers can take advantage of by reusing them.

Update Only When It Hurts


We should update an artifact such as a model or document only when we absolutely need to (when not
having the model updated is more painful than the effort of updating it).

Use the Simplest Tools


The vast majority of models can be drawn on a whiteboard, on paper, or even on the back of a napkin.
Note that AM has nothing against CASE toolsif investing in a CASE tool is the most effective use of our
resources, then we should do so and then it to the best of its ability.
At its core, AM is simply a collection of techniques that reflect the principles and values shared by many
experienced software developers. If there is such a thing as agile modeling, then are there also agile
models? Yes.

Amazon

Prev

don't be afraid of buying books

Next

WHAT ARE AGILE MODELS?


A model is an abstraction that describes one or more aspects of a problem or a potential solution
addressing a problem. Traditionally, models are thought of as zero or more diagrams plus any
corresponding documentation. However, non-visual artifacts such as collections of CRC cards, a textual
description of one or more business rules, or the structured English description of a business process are
also models. An agile model is a model that is just barely good enough. But how do we know when a
model is good enough?
Agile models are just barely good enough when they exhibit the following traits:

Agile models fulfill their purpose


Sometimes we model to communicate, perhaps we need to communicate the scope of our effort to
senior management, and sometimes we model to understand, perhaps we need to determine a design
strategy to implement a collection of Java classes. If we don't know why we need to create something,
then don't create it; that wouldn't be agile.

Agile models are understandable


Agile models are understandable by their intended audience. A requirements model will be written in the
language of the business that our users comprehend, whereas a technical architecture model will likely
use technical terms that developers are familiar with. For example, the whiteboard sketch in Figure B.1
is straightforward and easy to understandit isn't perfect but it gets the message across. The modeling
notation that we use affects understandabilityUML use case diagrams are of no value to our users if
they don't understand what the notation represents. In this case we would either need to use another
approach or educate them in the modeling technique. Style issues, such as avoiding crossing lines, will
also affect understandabilitymessy diagrams are harder to read than clean ones [4]. The level of detail
in our models and simplicity also affect understandability.

Figure B.1. A white board sketch of a business process.

Agile models are sufficiently accurate


Models often do not need to be 100 percent accurate, they just need to be accurate enough. For
example, if a street map is missing a street, or it shows that a street is open but we discover it's closed
for repairs, do we throw away the map and start driving mayhem through the city? Not likely. We might
decide to update our map; we could pull out a pen and do it ourselves or go to the local store and
purchase the latest version (which still might be out of date), or we could simply accept that the map
isn't perfect but still use it because it is good enough for our purposes. We don't discard our street map
the minute we find an inaccuracy because we don't expect the map to be perfect nor do we need it to
be. Some project teams can tolerate inaccuracies whereas others can't: the nature of the project, the
nature of the individual team members, and the nature of the organization will decide this.

Agile models are sufficiently consistent


An agile model does not need to be perfectly consistent with itself or with other artifacts to be useful. If
a use case clearly invokes another in one of its steps, then the corresponding use case diagram should
indicate that with an association between the two use cases that is tagged with the UML stereotype of
<<I n clude >>.The diagram is clearly inconsistent with the use case specification, yet the world hasn't
come to an end. In an ideal world, all of our artifacts would be perfectly consistent but the world isn't
ideal nor does it need to be. There is clearly an entropy issue to consider regarding accuracy and
consistency. If we have an artifact that we wish to keep as official documentation, then we will need to
invest the resources to update it as time goes on, otherwise it will quickly become out of date and

useless. The data model of Figure B.2 is missing a few recently added columns, yet it still provides very
good insight into your database schema. There is a fine line between spending too much time updating
documents and not enough. As an aside, Figure B.2 follows the proposed UML notation for data modeling
described at [URL 7].

Figure B.2. A physical data model (UML notation).


[View full size image]

Agile models are sufficiently detailed


A road map doesn't indicate each individual house on each street. That would be too much detail and
thus would make the map difficult to work with. However, when a street is being built, I would imagine
the builder has a detailed map of the street that shows each building, the sewers, electrical boxes, and
so on in enough detail that makes the map useful to him. This map doesn't depict the individual patio
stones that make up the walkway to each, once again that would be too much detail. Sufficient detail
depends on the audience and the purpose for which they are using a modeldrivers need maps that
show streets, builders need maps that show civil engineering details. Similarly, Figure B.1 clearly doesn't
provide a detailed description of the XYZ business process, nor is it perfect, but it does get depicted at a
sufficiently detailed level. I've worked on many projects where a couple of diagrams drawn on a
whiteboard that are updated as the project goes along were sufficient to describe the architecture.

Agile models provide positive value


A fundamental aspect of any project artifact is it should add positive value. Does the benefit that an
architecture model brings to our project outweigh the costs of developing and (optionally) maintaining it?
An architecture model helps to solidify the vision to which our project team is working towards, which
clearly has value. But, if the costs of that model outweigh the benefits, then it no longer provides
positive value. Perhaps it was unwise to invest $100,000 developing a detailed and heavily documented
architecture model when a $5,000 investment resulting in whiteboard diagrams recorded via digital
snapshots would have done the job.

Agile models are as simple as possible


We should strive to keep our models as simple as possible while still getting the job done. Simplicity is
clearly affected by the level of detail in our models, but it also can be affected by the extent of the
notation that we apply. For example, Unified Modeling Language (UML) class diagrams can include a
myriad of symbols, yet most diagrams can get by with just a portion of the notation. We often don't
need to apply all the symbols available to us, so we limit ourselves to a subset of the notation that still
allows us to get the job done. Often a CRC model is sufficient to explore the business domain or the
detailed design of our software, an example of which is depicted in Figure B.3, so we don't even need to
create a UML class diagram.

Figure B.3. Several CRC cards.


[View full size image]

Therefore, the definition for an agile model is that it is a model that fulfills its purpose and no more; is
understandable to its intended audience; is simple; sufficiently accurate, consistent, and detailed; and
investment in its creation and maintenance provides positive value to the project. In other words, an
agile model is just barely good enough.

Amazon

Prev

don't be afraid of buying books

Appendix C. ONLINE RESOURCES


This appendix presents a categorized and annotated list of URLs that are referenced in the book.

Next

Amazon

Prev

don't be afraid of buying books

FORUMS
Here you will find a selection of online places to talk with others about TDD and/or XP. This includes
mailing lists, newsgroups, forums, etc.
[URL 1] Yahoo JUnit Group
groups.yahoo.com/group/junint

Discussion of JUnit and its use.

[URL 2] Yahoo XPGroup


groups.yahoo.com/group/extremeprogramming

Discussion of eXtreme Programming practices and principles.

[URL 3] Yahoo TDD Group


groups.yahoo.com/group/testdrivendevelopment

Discussion of the theory and practice of test-driven development.

[URL 4] Yahoo Refactoring Group


groups.yahoo.com/group/refactoring

This is a forum for discussions about refactoring, including tools associated with

Next

refactoring. It is a place to share and discuss new and old refactorings in a variety of
software languages.

Amazon

Prev

don't be afraid of buying books

INFORMATION ON AGILE PROCESSES


[URL 5] The Agile Alliance
www.agilealliance.org

The Agile Alliance is a non-profit organization dedicated to promoting the concepts of


agile software development, and helping organizations adopt those concepts.

[URL 6] The Agile Manifesto


www.agilemanifesto.org

This site contains the text of the manifesto, information about the manifesto and its
authors, and a list of signatories (and a way to become one).

[URL 7] The Agile Data Homepage


www.agiledata.org

A good source of information about how data can be treated with agility. Hosted by Scott
Ambler.

[URL 8] The Agile Modeling Homepage


www.agilemodeling.com

The home of the Agile Modeling "movement." See Appendix B for an introduction. Hosted

Next

by Scott Ambler.

Amazon

Prev

don't be afraid of buying books

INFORMATION ON EXTREME PROGRAMMING


[URL 9] XP123
www.xp123.com

William Wake's site, full of articles and information about XP.

[URL 10] The Refactoring Homepage


www.refactoring.com

Martin Fowler's site dedicated to refactoring.

[URL 11] XProgramming.com: An eXtreme Programming Resource


www.xprogramming.com

The canonical XP site, maintained by Ron Jeffries.

[URL 12] eXtreme Programming: A Gentle Introduction


www.extremeprogramming.org

Don Wells' site, a very nicely done introduction to XP.

Next

[URL 13] eXtreme Programming Roadmap


www.c2.com/cgi/wiki

The granddaddy of XP sites. This is where it all began. This is a wiki (the original) so it's
a forum for discussion as well as an information source.

Amazon

Prev

don't be afraid of buying books

JUNIT-RELATED SOFTWARE
[URL 14] JUnit Resources
www.junit.org

This is the place to get the latest versions of the programmer test frameworks in the
xUnit family. There are also extensions and articles about programmer testing.

[URL 15] JUnitPerf


www.clarkware.com/software/JUnitPerf.html

JUnitPerf is a collection of JUnit test decorators that help you measure the performance
and scalability of functionality contained within existing JUnit tests.

[URL 16] JUnitPP


junitpp.sourceforge.net

JUnitPP is an extension of the JUnit framework. It provides some additional functionality


that makes JUnit more flexible and simple to use. It provides a test data repository,
command line arguments, and an improved TestRunner supporting a built-in repetition
counter and multithreading on the command line.

[URL 17] Daedalos JUnitExtensions


www.daedalos.com/EN/djux

Next

The Daedalos JUnit Extensions (djux) extend the JUnit testing framework in various ways.
They allow specifying TestResources that are available during the whole test cycle. Using
test resources speeds up unit tests, because time consuming initializations are only done
once and remain active over a complete series of test runs. Furthermore, they allow you
to integrate external testing tools as well as to perform specific database tests inside
JUnit tests.

At the site you can download the current version of the Daedalos JUnit Extensions and
find additional information about unit testing, future releases, and examples.

[URL 18] xmlUnit


xmlunit.sourceforge.net

XML can be used for just about anything, so deciding if two documents are equal to each
other isn't as easy as a character for character match. XMLUnit allows you to compare
XML documents and strings.

[URL 19] Gargoyle Software's JUnit Extensions


gsbase.sourceforge.net

A collection of classes that are helpful when writing JUnit test cases.

[URL 20] NoUnit


nounit.sourceforge.net

NoUnit allows you to see how good your JUnit tests are. It generates a report from your
code to graphically show you how many of your project's methods are being tested, and
how well.

This is invaluable if you have code that doesn't have a suite of programmer tests.

If you are practicing TDD, then this shouldn't be required since all the code is there as a
direct result of a test requiring it. But not a bad idea, anyway. . . bad stuff happens.

[URL 21] Jester - the JUnit test tester


jester.sourceforge.net

Jester finds code that is not covered by tests. It does this by making systematic (and
independent) changes to your code, compiling, and running your test suite. If changing
the code results in the tests still passing 100 percent, then a potential problem is
flagged.

[URL 22] The Clover code coverage analyser


www.thecortex.net/clover

Clover instruments your source code, gathers execution counts on a line-by-line basis
when you run your test suite (or run the code in whatever way you want), and generates
reports from the resulting data. This is very handy if you want to find out how
comprehensive your tests are. And you should want to find that out.

[URL 23] The Mock Objects framework


www.mockobjects.com

The MockObjects project is a generic programmer testing framework whose goal is to


facilitate developing programmer tests in the mock object style.

[URL 24] MockMaker


mockmaker.sourceforge.net

MockMaker is a program for creating source code for mock object classes that work with
the MockObjects framework (required parts of the framework are included in the
MockMaker download).

[URL 25] EasyMock


www.easymock.org

EasyMock is a framework for dynamically creating mock object at runtime. A big


advantage of this is that specifications for the mocks are included as part of the test and
not in yet another class.

[URL 26] Mockry


mockry.sourceforge.net

Mockry is another tool to create mockobjects.

Amazon

Prev

don't be afraid of buying books

JUNIT-RELATED INFORMATION
[URL 27] JUnit: A Starter Guide
www.diasparsoftware.com/articles/JUnit/jUnitStarterGuide.html

by J. B. Rainsberger

After reading how critical the JUnit community is of JUnit's documentation, we decided to
create some.

[URL 28] Simple Smalltalk testing: With patterns.


www.xprogramming.com/testfram.htm

The original xUnit article, by Kent Beck.

[URL 29] Test infected: Programmers love writing tests.


junit.sourceforge.net/doc/testinfected/testing.htm

The classic treatise on TDD, by Kent Beck and Erich Gamma.

[URL 30] JUnit primer.


www.clarkware.com/articles/JUnitPrimer.html

Next

Another great introduction to using JUnit, by Mike Clark.

[URL 31] Refactoring test code.


www.cwi.nl/~leon/papers/xp2001/xp2001.pdf

An excellent article on smells in test code and what to do about them. Presented at
XP2001, by Arie van Deursen, Leon Moonen, Alex van den Bergh, and Gerard Kok.

Amazon

Prev

don't be afraid of buying books

TOOLS
[URL 32] The Eclipse Project
www.eclipse.org

You can find the latest versions of the open source Eclipse IDE here, as well as articles,
forums, etc. relating to Eclipse. Eclipse now includes the JUnit plugin as part of the build.

[URL 33] Idea


www.intellij.com/idea/

Full Java IDE with extensive refactoring support.

[URL 34] Together Control Center


www.togethersoft.com

TogetherSoft's IDE and UML modeling tool.

[URL 35] jBuilder


www.borland.com/jbuilder

Borland's Java IDE has some refactoring support.

Next

[URL 36] Jakarta ANT


jakarta.apache.org/ant

The standard way to write build scripts for Java.

[URL 37] CruiseControl


cruisecontrol.sourceforge.net

A continuous integration tool from Martin Fowler and the folks at ThoughtWorks.

[URL 38] The Smalltalk Refactoring Browser


chip.cs.uiuc.edu/users/brant/Refactory

The original refactoring power tool.

[URL 39] jFactor


www.instantiations.com/jfactor

A Java refactoring tool from Instantiations (who have been making tools for the OO world
for a long time). It is a refactoring plugin.

[URL 40] RefactorIT


www.refactorit.com

A Java refactoring tool from Aqris that works with jBuilder, Sun ONE Studio, NetBeans,
and jDeveloper.

[URL 41] CodeMorpher


www.xptools.com

A Java refactoring tool from XP Tools AB that helps find smells, suggests appropriate
refactorings, and performs refactorings. Available as a stand-alone application that
interoperates with most Java IDEs and as a plugin for Borland's JBuilder.

[URL 42] Retool


www.chive.com/products/retool

Retool is a refactoring tool from Chive Software. It is an add-in for Oracle 9i, JDeveloper,
and Borland JBuilder 4/5.

[URL 43] VisualWorks Smalltalk by Cincom


www.cincom.com/smalltalk

VisualWorks is the direct descendant of the original Smalltalk systems from XEROX PARC.
XEROX spun off ParcPlace to commercialize Smalltalk-80. This evolved into VisualWorks
and was subsequently bought by Cincom, who has continued to develop and evolve it.

[URL 44] Squeak


www.squeak.org

Squeak is an open, highly-portable Smalltalk-80 implementation whose virtual machine is


written entirely in Smalltalk, making it easy to debug, analyze, and change. To achieve
practical performance, a translator produces an equivalent C program whose performance
is comparable to commercial Smalltalks.

Squeak is available for free via the Internet at this and other sites. Each release includes
platform-independent support for color, sound, and network access, with complete source
code. Originally developed on the Macintosh, members of its user community have since
ported it to numerous other platforms.

[URL 45] ERWin by Computer Associates


www3.ca.com/Solutions/Product.asp?ID=260

AllFusion ERwin Data Modeler is an industry-leading data modeling solution that can help
you create and maintain databases,data warehouses, and enterprise data models.

Amazon

Prev

don't be afraid of buying books

OTHER XUNIT FAMILY MEMBERS


[URL 46] Testing Framework for Ruby
homepage1.nifty.com/markey/ruby/rubyunit/index_e.html

On this page you can download the latest version of RubyUnit and find documentation
and related tools (e.g., a Ruby implementation of a mock objects framework).

[URL 47] Testing Framework for Smalltalk


www.xprogramming.com/testfram.htm

On this page you can download the latest version of SUnit. SUnit is now included in the
standard distribution of Cincom VisualWorks Smalltalk.

[URL 48] Testing Framework for C++


cppunit.sourceforge.net

On this page you can download the latest version of CppUnit, find documentation, and
get involved. CppUnit was originally a port of JUnit done by Michael Feathers of Object
Mentor.

[URL 49] Testing Framework for C++


unitpp.sourceforge.net

On this page you can download the latest version of Unit++. This is an alternative xUnit
implementation for C++ which takes a different approach. It isn't a port of JUnit, rather it
is a redesign and rewrite in C++. As such, it takes advantage of C++'s capabilities and
idioms.

Next

[URL 50] Testing Framework for Perl


perlunit.sourceforge.net

On this page you can download the latest version of PerlUnit.

[URL 51] Testing Framework for Visual Basic


www.vbunit.com

On this page you can download the latest version of vbUnit.

[URL 52] Testing Framework for C# and .NET


nunit.org

On this page you can download the latest version of NUnit.

[URL 53] Testing Framework for Python


pyunit.sourceforge.net

The home on PyUnit and its ongoing development.

Amazon

Prev

don't be afraid of buying books

COMPANIES
[URL 54] Adaption Software, Inc.
www.adaptionsoft.com

Adaption is the company founded and run by the author of this book.

Adaption Software uses its expertise in Extreme Programming (XP) and Test Driven
Development, as well as its high level of software craftsmanship to provide:
1. Consulting services for companies adopting TDD or XP;
2. Training courses for individuals and organizations who wish to learn TDD or XP;
3. Software development consulting services using XP.

[URL 55] The Pragmatic Programmers


www.pragmaticprogrammer.com

The online home of The Pragmatic Programmers, the company built by Dave Thomas and
Andy Hunt.

[URL 56] Clarkware Consulting, Inc.


www.clarkware.com

Clarkware Consulting, Inc. provides pragmatic software architecture, design, development,


and performance consulting and mentoring services. Mike Clark offers his personal
experience and leadership in tailoring these services for your project to quickly deliver
maximum business value. He has a reputation for delivering high-quality software rapidly
and predictably, and a passion for helping clients build better software faster. He also
offers customized training and mentoring to help you quickly become productive using
agile design and testing tools that any budget can afford.

Next

[URL 57] Daedalos Consulting GmbH


www.daedalos.com

The company that employs Jens Uwe Pibka. Kent Beck worked with this company for a
couple of years as well.

[URL 58] ThoughtWorks, Inc


www.thoughtworks.com

The consulting/contracting company for which Martin Fowler works.

[URL 59] Gargoyle Software, Inc.


www.gargoylesoftware.com

Mike Bowler's company.

[URL 60] TogetherSoft


www.togethersoft.com

Maker of the Together line of products.

Amazon

Prev

don't be afraid of buying books

MISCELLANEOUS
[URL 61] The Coad Letter
bdn.borland.com/coadletter

The Coad Letter focuses on these key areas: competitive strategy, adaptive process,
modeling, design, and test-driven development. This site is a specialized e-newsletter
source and online community dedicated to developing, discussing, and advancing these
topics.

The Coad Letter is a service provided by Peter Coad and colleagues.

I (Dave Astels) am editor of the TDD edition.

[URL 62] Brad Appleton's AVL Tree tutorial


www.enteract.com/~bradapp/ftp/src/libs/C++/AvlTrees.html

A freely available public domain AVL tree library written in C++.

[URL 63] Software Development


www.sdmagazine.com

Software Development magazine.

Next

[URL 64] The Modeling Style Homepage


www.modelingstyle.info

A good source of modeling standards and guidelines. Hosted by Scott Ambler.

[URL 65] The support page for this book.


www.adaptionsoft.com/tddapg.html

A page with supplemental information, additional chapters, discussion forum, links to


online resources, corrections, downloadable code, etc.

[URL 66] The Mono project


www.go-mono.com

An effort to create an open source implementation of the .NET Development Framework.

Mono includes: a compiler for the C# language, a runtime for the Common Language
Infrastructure (also referred to as the CLR),and a set of class libraries.

[URL 67] The Python Language


www.python.org

The online home of Python.

[URL 68] The Puffin Homepage


www.puffinhome.org

The online home of Puffin: an XML configurable Web application/service testing


framework built in Python by Keyton Weissinger.

[URL 69] The WebUnit Homepage


webunit.sourceforge.net

The online home of WebUnit: a unit testing framework for testing Web applications in
PyUnit tests. WebUnit was developed by Steve Purcell, the author of PyUnit.

Amazon

Prev

don't be afraid of buying books

Appendix D. ANSWERS TO EXERCISES


This appendix contains answers to the exercises in the book, with pointers back to where the exercise appeared.

1.
Answer
1:

Refactor T e st M o v i e to use a single Constructor Method that we just added to M o vi e .


p u b li c c l a s s T e st M o v i e e x t en d s T e s t C a s e {
p ri v a t e M o vi e s t a r W a r s = n u l l ;

p ro t e c t e d vo i d s e t U p ( ) {
s t a r W a r s = ne w M o v i e ( " St a r W a r s " , n ul l , - 1 ) ;

p u b li c v o i d te s tM o v i e N a m e ( ) {
a ss e r t E q u a ls ( "s t a r W a r s s ho u l d h a v e n a me \ " S t a r W a r s \ ". " ,
"S t a r W a r s " ,
st a r W a r s . g e tN a m e ( ) ) ;
}
p u b li c v o i d te s tN u l l N a m e ( ) {
S tr i n g n u l lS t ri n g = n u l l ;
t ry {
n e w M o v i e( n ul l S t r i n g , nu l l ,
-1);
f a i l ( " n u ll na m e s h o u l d h a v e t h r o w n Il l e g a l A r g u m e n t Ex c e p t i o n . " ) ;
} c a t c h ( I ll e ga l A r g u m e n t Ex c e p t i o n e x ) {
}
}
p u b li c v o i d te s tE m p t y N a m e ( ) {
t ry {
n e w M o v i e( " ", n u l l , - 1 );
f a i l ( " e m pt y n a m e s h o u l d h a v e t h r o w n I l l e g a l A r g u m e n tE x c e p t i o n . " ) ;
} c a t c h ( I ll e ga l A r g u m e n t Ex c e p t i o n e x ) {
}
}
p u b li c v o i d te s tT o S t r i n g ( ) {
a ss e r t E q u a ls ( "s t a r W a r s s ho u l d h a v e t o St r i n g o f \ " S t a r W a r s \ " . " ,
"S t a r W a r s " ,
st a r W a r s . t o St r i n g ( ) ) ;
}
p u b li c v o i d te s tE q u a l s ( ) {
f in a l M o v i e a = n e w M o v i e( " S t a r
-1);
f in a l M o v i e b = n e w M o v i e( " S t a r
-1);
f in a l M o v i e c = n e w M o v i e( " S t a r
-1);
f in a l M o v i e d = n e w M o v i e( " S t a r
-1) {
};
n ew E q u a l s Te s te r ( a , b , c , d ) ;
}

Wars", null,
Wars", null,
Trek", null,
Wars", null,

p u b li c v o i d te s tR e n a m i n g ( ) {
S tr i n g n e w Na m e = " S t a r T re k " ;
M ov i e a M o v ie = n e w M o v i e (" S t a r W a r s " , n u l l ,
-1);
a Mo v i e . r e n am e (n e w N a m e ) ;
a ss e r t E q u a ls ( "R e n a m i n g s ho u l d c h a n g e th e n a m e . " ,
ne w N a m e ,
aM o v i e . g e t N am e ( ) ) ;
}
p u b li c v o i d te s tN u l l R e n a m e () {
M ov i e a M o v ie = n e w M o v i e (" S t a r W a r s " , n u l l ,
-1);
t ry {
a M o v i e . r en a me ( n u l l ) ;
f a i l ( " n u ll re n a m e s h o u ld h a v e t h r o w n I l l e g a l A r g u m e nt E x c e p t i o n . " );
} c a t c h ( I ll e ga l A r g u m e n t Ex c e p t i o n e x ) {
}
}
p u b li c v o i d te s tE m p t y R e n a m e( ) {
M ov i e a M o v ie = n e w M o v i e (" S t a r W a r s " , n u l l ,
-1);
t ry {
a M o v i e . r en a me ( " " ) ;
f a i l ( " e m pt y r e n a m e s h o ul d h a v e t h r o wn I l l e g a l A r g u m en t E x c e p t i o n . ") ;

Next

} c a t c h ( I ll e ga l A r g u m e n t Ex c e p t i o n e x ) {
}

p u b li c v o i d te s tC o p y C o n s t r uc t o r ( ) {
M ov i e c o p y Of S ta r W a r s = n ew M o v i e ( s t a r Wa r s ) ;
a ss e r t N o t S am e (" A c o p y s h ou l d n o t b e t he s a m e a s t h e or i g i n a l . " ,
st a r W a r s ,
co p y O f S t a r W ar s ) ;
a ss e r t E q u a ls ( "A c o p y s h o ul d b e e q u a l to t h e o r i g i n a l ." ,
s ta r W a r s ,
c op y O f S t a r W a rs ) ;
}
p ub l i c v o i d t es t U n R a t e d ( ) {
a s s e r t F a ls e (" s t a r W a r s sh o u l d b e u n r at e d . " , s t a r W a r s. h a s R a t i n g ( ) );
}
p ub l i c v o i d t es t R a t e d M o v ie ( ) t h r o w s U nr a t e d E x c e p t i o n {
M o v i e f o tr = n e w M o v i e (" F e l l o w s h i p of t h e R i n g " , n ul l , 5 ) ;
a s s e r t T r ue ( "f o t r s h o u l d b e r a t e d " , fo t r . h a s R a t i n g ( )) ;
a s s e r t E q ua l s( " f o t r s h o ul d b e r a t e d at 5 . " , 5 , f o t r .g e t R a t i n g ( ) ) ;
}
p ub l i c v o i d t es t U n r a t e d E xc e p t i o n ( ) {
try {
s t a r W a rs . ge t R a t i n g ( ) ;
f a i l ( " ge t Ra t i n g o n a n u n r a t e d M o v ie s h o u l d t h r o w U n r a t e d E x c e p ti o n . " ) ;
} c a t c h (U n ra t e d E x c e p t io n e x ) {
a s s e r t Eq u al s ( " U n r a t e dE x c e p t i o n s h ou l d i d e n t i f y t he m o v i e . " ,
s t a r W a r s. g e t N a m e ( ) ,
e x . g e t M es s a g e ( ) ) ;
}
}
p ub l i c v o i d t es t U n c a t e g o ri z e d ( ) {
a s s e r t E q ua l s( " s t a r W a r s s h o u l d b e u n ca t e g o r i z e d . " ,
" U n c a t e g o ri z e d " ,
s t a r W a r s . ge t C a t e g o r y ( ) );
}
p ub l i c v o i d t es t S c i e n c e F ic t i o n ( ) {
M o v i e a l ie n = n e w M o v i e( " A l i e n " , " S ci e n c e F i c t i o n " ,

-1);

2.
Answer
2:

a s s e r t E q ua l s( " a l i e n s h ou l d b e S c i e n ce F i c t i o n . " ,
" S c i e n c e Fi c t i o n " ,
a l i e n . g e t Ca t e g o r y ( ) ) ;

p ub l i c s t a ti c v o i d m a i n ( St r i n g [ ] a r g s) {
j u n i t . t e xt u i. T e s t R u n n e r. r u n ( T e s t M o v ie . c l a s s ) ;
}

Refactor the tests again, this time removing the unneeded M o v i e creation.
p u b li c c l a s s T e st M o v i e e x t en d s T e s t C a s e {
p ri v a t e M o vi e s t a r W a r s = n u l l ;
p ro t e c t e d vo i d s e t U p ( ) {
s t a r W a r s = ne w M o v i e ( " St a r W a r s " , n ul l , - 1 ) ;
}
p ub l i c v o i d t es t M o v i e N a m e( ) {
a s s e r t E q ua l s( " s t a r W a r s s h o u l d h a v e na m e \ " S t a r W a r s\ " . " ,
" S t a r W a r s" ,
s t a r W a r s . ge t N a m e ( ) ) ;
}
p ub l i c v o i d t es t N u l l N a m e () {
S t r i n g n ul l St r i n g = n u ll ;
try {
n e w M o vi e (n u l l S t r i n g , n u l l ,
-1);
f a i l ( " nu l l n a m e s h o u ld h a v e t h r o w n I l l e g a l A r g u m e nt E x c e p t i o n . " );
} c a t c h (I l le g a l A r g u m e nt E x c e p t i o n e x) {
}
}
p ub l i c v o i d t es t E m p t y N a m e( ) {
try {
n e w M o vi e (" " , n u l l ,
-1);
f a i l ( " em p ty n a m e s h o ul d h a v e t h r o wn I l l e g a l A r g u m en t E x c e p t i o n . ") ;
} c a t c h (I l le g a l A r g u m e nt E x c e p t i o n e x) {
}
}

p ub l i c v o i d t es t T o S t r i n g () {
a s s e r t E q ua l s( " s t a r W a r s s h o u l d h a v e to S t r i n g o f \ " S ta r W a r s \ " . " ,
" S t a r W a r s" ,
s t a r W a r s . to S t r i n g ( ) ) ;
}
p ub l i c v o i d t es t E q u a l s ( ) {
f i n a l M o vi e a = n e w M o vi e ( " S t a r
-1);
f i n a l M o vi e b = n e w M o vi e ( " S t a r
-1);
f i n a l M o vi e c = n e w M o vi e ( " S t a r
-1);
f i n a l M o vi e d = n e w M o vi e ( " S t a r
-1) {
};
n e w E q u a ls T es t e r ( a , b , c , d ) ;
}

W a r s" , n u l l ,
W a r s" , n u l l ,
T r e k" , n u l l ,
W a r s" , n u l l ,

p ub l i c v o i d t es t R e n a m i n g () {
S t r i n g n ew N am e = " S t a r T r e k " ;
s t a r W a r s .r e na m e ( n e w N a m e) ;
a s s e r t E q ua l s( " R e n a m i n g s h o u l d c h a n g e t h e n a m e . " ,
newName,
s t a r W a r s . ge t N a m e ( ) ) ;
}
p ub l i c v o i d t es t N u l l R e n a me ( ) {
try {
s t a r W a rs . re n a m e ( n u l l );
f a i l ( " nu l l r e n a m e s h ou l d h a v e t h r ow n I l l e g a l A r g u me n t E x c e p t i o n ." ) ;
} c a t c h (I l le g a l A r g u m e nt E x c e p t i o n e x) {
}
}
p ub l i c v o i d t es t E m p t y R e n am e ( ) {
try {
s t a r W a rs . re n a m e ( " " ) ;
f a i l ( " em p ty r e n a m e s ho u l d h a v e t h ro w n I l l e g a l A r g um e n t E x c e p t i o n. " ) ;
} c a t c h (I l le g a l A r g u m e nt E x c e p t i o n e x) {
}
}
p ub l i c v o i d t es t C o p y C o n s tr u c t o r ( ) {
M o v i e c o py O fS t a r W a r s = n e w M o v i e ( s t ar W a r s ) ;
a s s e r t N o tS a me ( " A c o p y sh o u l d n o t b e t h e s a m e a s t h e o r i g i n a l . " ,
starWars,
c o p y O f S t a rW a r s ) ;
a s s e r t E q ua l s( " A c o p y s ho u l d b e e q u a l t o t h e o r i g i n al . " ,
starWars,
c o p y O f S t a r Wa r s ) ;
}
p ub l i c v o i d t es t U n R a t e d ( ) {
a s s e r t F a ls e (" s t a r W a r s sh o u l d b e u n r at e d . " , s t a r W a r s. h a s R a t i n g ( ) );
}
p ub l i c v o i d t es t R a t e d M o v ie ( ) t h r o w s U nr a t e d E x c e p t i o n {
M o v i e f o tr = n e w M o v i e (" F e l l o w s h i p of t h e R i n g " , n ul l , 5 ) ;
a s s e r t T r ue ( "f o t r s h o u l d b e r a t e d " , fo t r . h a s R a t i n g ( )) ;
a s s e r t E q ua l s( " f o t r s h o ul d b e r a t e d at 5 . " , 5 , f o t r .g e t R a t i n g ( ) ) ;
}
p ub l i c v o i d t es t U n r a t e d E xc e p t i o n ( ) {
try {
s t a r W a rs . ge t R a t i n g ( ) ;
f a i l ( " ge t Ra t i n g o n a n u n r a t e d M o v ie s h o u l d t h r o w U n r a t e d E x c e p ti o n . " ) ;
} c a t c h (U n ra t e d E x c e p t io n e x ) {
a s s e r t Eq u al s ( " U n r a t e dE x c e p t i o n s h ou l d i d e n t i f y t he m o v i e . " ,
s t a r W a r s .g e t N a m e ( ) ,
e x . g e t M e ss a g e ( ) ) ;
}
}
p ub l i c v o i d t es t U n c a t e g o ri z e d ( ) {
a s s e r t E q ua l s( " s t a r W a r s s h o u l d b e u n ca t e g o r i z e d . " ,
" U n c a t e g o ri z e d " ,
s t a r W a r s . ge t C a t e g o r y ( ) );
}
p ub l i c v o i d t es t S c i e n c e F ic t i o n ( ) {
M o v i e a l ie n = n e w M o v i e( " A l i e n " , " S ci e n c e F i c t i o n " ,

-1);

a s s e r t E q ua l s( " a l i e n s h ou l d b e S c i e n ce F i c t i o n . " ,
" S c i e n c e Fi c t i o n " ,
a l i e n . g e t Ca t e g o r y ( ) ) ;

}
p ub l i c s t a ti c v o i d m a i n ( St r i n g [ ] a r g s) {
j u n i t . t e xt u i. T e s t R u n n e r. r u n ( T e s t M o v ie . c l a s s ) ;

3.

Answer
3:

The customer identified these categories: Science Fiction, Horror, Comedy, Western, Drama, Fantasy, Kids,
Adult, Mystery, Thriller. Add these to C a t e g o r y .
p u b li c c l a s s C a te g o r y {
p ri v a t e S t ri n g n a m e = n u l l ;

4.
Answer
4:

Answer
5:

p ri v a t e C a te g or y ( S t r i n g ca t e g o r y N a m e ) {
n a m e = c at e go r y N a m e ;
}
p ub l i c s t a ti c f i n a l C a t e go r y U N C A T E G O RI Z E D = n e w C a t eg o r y ( " U n c a t e go r i z e d " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y S C I F I = ne w C a t e g o r y ( " S ci e n c e F i c t i o n" ) ;
p ub l i c s t a ti c f i n a l C a t e go r y H O R R O R = n e w C a t e g o r y ( " Ho r r o r " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y C O M E D Y = n e w C a t e g o r y ( " Co m e d y " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y W E S T E R N = n e w C a t e g o r y ( "W e s t e r n " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y D R A M A = ne w C a t e g o r y ( " D ra m a " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y F A N T A S Y = n e w C a t e g o r y ( "F a n t a s y " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y K I D S = n ew C a t e g o r y ( " K i ds " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y A D U L T = ne w C a t e g o r y ( " A du l t " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y M Y S T E R Y = n e w C a t e g o r y ( "M y s t e r y " ) ;
p ub l i c s t a ti c f i n a l C a t e go r y T H R I L L E R = n e w C a t e g o r y (" T h r i l l e r " ) ;

Extend s e t Ca t e g o r y F i el d ( ) to make t e s t S e l e c t in g ( ) pass.


p u b li c v o i d se l ec t ( i n t i ) {
i f ( i = = - 1) {
s e l e c t e d Mo v ie = n u l l ;
} else {
s e l e c t e d Mo v ie = m o v i e s .g e t M o v i e ( i ) ;
v i e w . s e t Na m eF i e l d ( s e l e ct e d M o v i e . g e t Na m e ( ) ) ;
v i e w . s e t Ca t eg o r y F i e l d ( se l e c t e d M o v i e .g e t C a t e g o r y ( ) ) ;

5.

try {
v i e w . s et R at i n g F i e l d ( se l e c t e d M o v i e .g e t R a t i n g ( ) + 1) ;
} c a t c h (U n ra t e d E x c e p t io n e ) {
v i e w . s e tR a ti n g F i e l d ( 0 );
}

What problem does this cause? Why? Fix it.


It breaks t es t Up d a t i n g ( ) , t e s t U p d a t i n gW i t h S a m e N a m e( ) ,and t e s t D u p l i c a te C a u s i n g U p d at e
because they don't expect the calls to s e t C a t e g o r y F ie l d ( ) .
Here are the updated methods. We just need to add expectations for the calls to s e t C a t e g o r yF i e l d ( ) :
p ub l i c v o i d t es t U p d a t i n g () {
V e c t o r n ew M ov i e s = n e w V e c t o r ( ) ;
n e w M o v i e s. a dd ( s t a r W a r s );
n e w M o v i e s. a dd ( n e w M o v i e( " S t a r T r e k I" , C a t e g o r y . S C IF I , 5 ) ) ;
n e w M o v i e s. a dd ( s t a r g a t e );
n e w M o v i e s. a dd ( t h e S h i n i ng ) ;
m o c k V i e w .s e tM o v i e s ( m o v ie s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
m o c k V i e w .s e tN a m e F i e l d ( "S t a r T r e k " ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
m o c k V i e w .s e tR a t i n g F i e l d( 4 ) ;
c o n t r o l . se t Vo i d C a l l a b l e( ) ;
m o c k V i e w .s e tC a t e g o r y F i el d ( C a t e g o r y . SC I F I ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
m o c k V i e w .g e tN a m e F i e l d ( );

c o n t r o l . se t Re t u r n V a l u e (" S t a r T r e k I ", 1 ) ;
m o c k V i e w .g e tR a t i n g F i e l d( ) ;
c o n t r o l . se t Re t u r n V a l u e (6 , 1 ) ;
m o c k V i e w .s e tM o v i e s ( n e w Mo v i e s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
c o n t r o l . ac t iv a t e ( ) ;
M o v i e L i s tE d it o r e d i t o r = n e w M o v i e L is t E d i t o r ( m o v i e Li s t , m o c k V i e w );
e d i t o r . s el e ct ( 1 ) ;
e d i t o r . u pd a te ( ) ;
}

c o n t r o l . ve r if y ( ) ;

p u b li c v o i d te s tU p d a t i n g W i th S a m e N a m e ( ) {
V ec t o r n e w Mo v ie s = n e w V ec t o r ( ) ;
n ew M o v i e s . ad d (s t a r W a r s ) ;
n ew M o v i e s . ad d (n e w M o v i e ( "S t a r T r e k " , C at e g o r y . S C I F I , 5) ) ;
n ew M o v i e s . ad d (s t a r g a t e ) ;
n ew M o v i e s . ad d (t h e S h i n i n g );
m oc k V i e w . s et M ov i e s ( m o v i e s) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . s et N am e F i e l d ( " S ta r T r e k " ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . s et R at i n g F i e l d ( 4) ;
c on t r o l . s e tV o id C a l l a b l e ( );
m oc k V i e w . s et C at e g o r y F i e l d( C a t e g o r y . S C IF I ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . g et N am e F i e l d ( ) ;
c on t r o l . s e tR e tu r n V a l u e ( " St a r T r e k " , 1 );
m oc k V i e w . g et R at i n g F i e l d ( );
c on t r o l . s e tR e tu r n V a l u e ( 6 , 1 ) ;
m oc k V i e w . s et M ov i e s ( n e w M o vi e s ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
c on t r o l . a c ti v at e ( ) ;
M ov i e L i s t E di t or e d i t o r = n e w M o v i e L i s tE d i t o r ( m o v i e L i st , m o c k V i e w ) ;
e di t o r . s e l ec t (1 ) ;
e di t o r . u p d at e () ;
c on t r o l . v e ri f y( ) ;
}
p u b li c v o i d te s tD u p l i c a t e C au s i n g U p d a t e ( ) {
m oc k V i e w . s et M ov i e s ( m o v i e s) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . s et N am e F i e l d ( " S ta r T r e k " ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . s et R at i n g F i e l d ( 0) ;
c on t r o l . s e tD e fa u l t V o i d C a ll a b l e ( ) ;
m oc k V i e w . s et C at e g o r y F i e l d( C a t e g o r y . S C IF I ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . g et N am e F i e l d ( ) ;
c on t r o l . s e tR e tu r n V a l u e ( " St a r W a r s " , 1 );
m oc k V i e w . d up l ic a t e E x c e p t io n ( " S t a r W a r s" ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
c on t r o l . a c ti v at e ( ) ;
M ov i e L i s t E di t or e d i t o r = n e w M o v i e L i s tE d i t o r ( m o v i e L i st , m o c k V i e w ) ;
e di t o r . s e l ec t (1 ) ;
e di t o r . u p d at e () ;
}

6.

Answer
6:

c on t r o l . v e ri f y( ) ;

We used the t o S t ri n g( ) method to get the value for the category field, as well as the value from the
expected C a t eg o ry to compare against the field contents. What's the problem that we have with the system
in its current state? (Hint: look at C a t e go r y .) Fix it.
We haven't defined C at eg o r y . t o S t r i ng ( ) so it just uses the default from Object. This gives a consistent
result, and so our test passes. However, it's not human readable. To fix it we need to add t o S t r i n g ( ) to

C a t eg o r y :
p u b li c S t r i n g t oS t r i n g ( ) {
r et u r n n a m e;
}

7.
Answer
7:

Make the required changes to M o v i e and Te s t M o v i e L i s tE d i to r .


Add a mutator for c at e g o r y tp M o v i e :
p u b li c v o i d se t Ca t e g o r y ( C a te g o r y a C a t e g or y ) {
c at e g o r y = a C at e g o r y ;
}

In Te s t M o v i eL i st E d i t o r , add an expectation for the call to g e t C a t e g o r yF i e l d ( ) in


t e s tU p d a t i n g Wi t hS a m e N a m e ( ) :
p u b li c v o i d te s tU p d a t i n g W i th S a m e N a m e ( ) {
V ec t o r n e w Mo v ie s = n e w V ec t o r ( ) ;
n ew M o v i e s . ad d (s t a r W a r s ) ;
n ew M o v i e s . ad d (n e w M o v i e ( "S t a r T r e k " , Ca t e g o r y . S C I F I , 5 ) ) ;
n ew M o v i e s . ad d (s t a r g a t e ) ;
n ew M o v i e s . ad d (t h e S h i n i n g );
m oc k V i e w . s et M ov i e s ( m o v i e s) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . s et N am e F i e l d ( " S ta r T r e k " ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . s et R at i n g F i e l d ( 4) ;
c on t r o l . s e tV o id C a l l a b l e ( );
m oc k V i e w . s et C at e g o r y F i e l d( C a t e g o r y . S C IF I ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . g et N am e F i e l d ( ) ;
c on t r o l . s e tR e tu r n V a l u e ( " St a r T r e k " , 1 );
m oc k V i e w . g et R at i n g F i e l d ( );
c on t r o l . s e tR e tu r n V a l u e ( 6 , 1 ) ;
m oc k V i e w . g et C at e g o r y F i e l d( ) ;
/ / t h i s li n e a d d e d
c on t r o l . s e tR e tu r n V a l u e ( C at e g o r y . S C I F I , 1 ) ; / / t h i s l in e a d d e d
m oc k V i e w . s et M ov i e s ( n e w M o vi e s ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
c on t r o l . a c ti v at e ( ) ;
M ov i e L i s t E di t or e d i t o r = n e w M o v i e L i s tE d i t o r ( m o v i e L i st , m o c k V i e w ) ;
e di t o r . s e l ec t (1 ) ;
e di t o r . u p d at e () ;
}

8.
Answer
8:

c on t r o l . v e ri f y( ) ;

Write a t o S t r i ng ( ) method for M o v i e L i s t .


p u b li c S t r i n g t oS t r i n g ( ) {
S tr i n g B u f f er bu f = n e w S tr i n g B u f f e r ( " [" ) ;
I te r a t o r m ov i eI t e r a t o r = m o v i e s . i t e r a to r ( ) ;
b oo l e a n f i rs t = t r u e ;
w hi l e ( m o v ie I te r a t o r . h a s Ne x t ( ) ) {
M o v i e a M ov i e = ( M o v i e ) m o v i e I t e r a t o r. n e x t ( ) ;
i f ( ! f i r st ) {
b u f . a p pe n d( ' ' ) ;
}
f i r s t = fa l se ;
b u f . a p p e nd ( '" ' ) ;
b u f . a p p e nd ( aM o v i e . g e t N am e ( ) ) ;
b u f . a p p e nd ( '" ' ) ;
}

9.

Answer
9:

b uf . a p p e n d (' ] ') ;
r et u r n b u f .t o St r i n g ( ) ;

Write an e q u a l s ( ) method for Mo v i e L i s t . Start by adding a test to T e s t M o v i eL i s t W i t hP o p u l a t e dL i st .


First, the test:
p u b li c v o i d te s tE q u a l s ( ) t hr o w s D u p l i c a te M o v i e E x c e p t i o n {
M ov i e L i s t e qu a lL i s t = n e w M o v i e L i s t ( ) ;
e qu a l L i s t . ad d (s t a r W a r s ) ;
e qu a l L i s t . ad d (s t a r T r e k ) ;
e qu a l L i s t . ad d (s t a r g a t e ) ;

M ov i e L i s t un e qu a l L i s t = ne w M o v i e L i s t () ;
u ne q u a l L i s t. a dd ( s t a r W a r s );
u ne q u a l L i s t. a dd ( s t a r g a t e );
n ew E q u a l s Te s te r ( m o v i e L i st , e q u a l L i s t , u n e q u a l L i s t , nu l l ) ;

Then the e q u al s () method (be sure you didn't forget h a s h Co d e( ) ):


p u b li c b o o l e an eq u a l s ( O b j e ct o ) {
i f ( o = = t hi s ) r e t u r n t r ue ;
i f ( o = = n ul l ) r e t u r n f a ls e ;
i f ( o . g e t C la s s( ) ! = t h i s .g e t C l a s s ( ) ) re t u r n f a l s e ;
M ov i e L i s t aM o vi e L i s t = ( M o vi e L i s t ) o ;
r et u r n m o v ie s .e q u a l s ( a M o vi e L i s t . m o v i e s) ;
}
p u b li c i n t h as h Co d e ( ) {
r et u r n m o v ie s .h a s h C o d e ( ) ;
}

10.
Answer
10:

Extend the fixture to support this test.


/ / . ..
p r i va t e M o v i eL i st s c i f i L i s t = n u l l ;
p r i va t e M o v i eL i st t h r i l l e r Li s t = n u l l ;
p r i va t e M o v i eL i st h o r r o r L i st = n u l l ;
p r o te c t e d v o id se t U p ( ) t h r ow s E x c e p t i o n {
/ /. . .
s ci f i L i s t = n ew M o v i e L i s t( ) ;
s ci f i L i s t . ad d (s t a r W a r s ) ;
s ci f i L i s t . ad d (s t a r T r e k ) ;
s ci f i L i s t . ad d (s t a r g a t e ) ;
t hr i l l e r L i st = n e w M o v i e Li s t ( ) ;
t hr i l l e r L i st . ad d ( r e d O c t o be r ) ;
t hr i l l e r L i st . ad d ( c o n g o ) ;

11.

h or r o r L i s t = ne w M o v i e L i st ( ) ;
h or r o r L i s t .a d d( t h e S h i n i n g) ;
h or r o r L i s t .a d d( c a r r i e ) ;

Make the required updates to add f i l t er e d M o v i e s .

Answer
11:

p u b li c c l a s s M o vi e L i s t E d i t or {
p ri v a t e M o vi e Li s t f i l t e r ed M o v i e s ;
p ub l i c M o v ie L is t E d i t o r ( M ov i e L i s t m o v i eL i s t , M o v i e L i s tE d i t o r V i e w a Vi e w ) {
m o v i e s = m o vi e L i s t ;
f i l t e r e d Mo v ie s = m o v i e L i st ;
v i e w = a Vi e w;
v i e w . s e t Ed i to r ( t h i s ) ;
u p d a t e M o vi e Li s t ( ) ;
}

12.
Answer
12:

p ri v a t e v o id up d a t e M o v i e Li s t ( ) {
v i e w . s e t Mo v ie s ( n e w V e c to r ( f i l t e r e d M ov i e s . g e t M o v i e s () ) ) ;
}

Extract the fixture code from the two tests into s e t U p () .


p u b li c c l a s s T e st M o v i e L i s t Wr i t e r e x t e n d s T e s t C a s e {
S tr i n g W r i t er de s t i n a t i o n = n u l l ;
M ov i e L i s t mo v ie L i s t = n u ll ;
p ro t e c t e d vo i d s e t U p ( ) {
d e s t i n a t io n = n e w S t r i ng W r i t e r ( ) ;
m o v i e L i s t= n ew M o v i e L i s t () ;
}
p ub l i c v o i d t es t W r i t i n g E mp t y L i s t ( ) t h ro w s E x c e p t i o n {
m o v i e L i s t. w ri t e T o ( d e s t in a t i o n ) ;
a s s e r t E q ua l s( " W r i t i n g an e m p t y l i s t s h o u l d p r o d u c e n o t h i n g . " ,
"",
d e s t i n a t i on . t o S t r i n g ( ) );
}
p ub l i c v o i d t es t W r i t i n g O ne M o v i e ( ) t h r ow s E x c e p t i o n {
S t r i n g s ta r Wa r s O u t p u t = " S t a r W a r s | Sc i e n c e F i c t i o n |4 \ n " ;
M o v i e s t ar W ar s = n e w M ov i e ( " S t a r W a rs " , C a t e g o r y . S CI F I , 4 ) ;
m o v i e L i s t. a dd ( s t a r W a r s );
m o v i e L i s t. w ri t e T o ( d e s t in a t i o n ) ;
a s s e r t E q ua l s( " W r o n g o u tp u t f r o m w r i ti n g a s i n g l e m ov i e l i s t . " ,
s t a r W a r s O ut p u t ,
d e s t i n a t i on . t o S t r i n g ( ) );
}

13.
Answer
13:

p ub l i c s t a ti c v o i d m a i n ( St r i n g [ ] a r g s) {
j u n i t . t e xt u i. T e s t R u n n e r. r u n ( T e s t M o v ie L i s t W r i t e r . c l as s ) ;
}

Add the method stubs and declarations we need in order to get the test compiling.
In Mo v i e L i s tE d it o r :
p u b li c v o i d sa v eA s ( ) {
}

In Mo v i e L i s tE d it o r V i e w :
F i l e g e t F i l e (S t ri n g s t r i n g );

And in S wi n gM o v i e L i s t E di t o r V i e w :
p u b li c F i l e ge t Fi l e ( S t r i n g p a t t e r n ) {
r et u r n n u l l;
}

14.
Answer
14:

Make the required change to T e s t Mo v i e L i s t E d i t or F i l e O p e r a t i o n s. t e s t S a v i n g ( ).


p u b li c v o i d te s tS a v i n g ( ) t hr o w s E x c e p t i on {
m oc k V i e w . s et M ov i e s ( m o v i e s) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . g et F il e ( ) ;
c on t r o l . s e tR e tu r n V a l u e ( o ut p u t F i l e , 1 ) ;
c on t r o l . a c ti v at e ( ) ;

15.
Answer
15:

M ov i e L i s t E di t or e d i t o r = n e w M o v i e L i s tE d i t o r ( m o v i e L i st , m o c k V i e w ) ;
a ss e r t T r u e (" E di t o r s h o u l d h a v e s a v e d " , e d i t o r . s a v e A s () ) ;
F il e A s s e r t .a s se r t S i z e ( " S av e A s - e d f i l e h a s w r o n g s i z e. " ,
e xp e c t e d . l e n g t h( ) ,
o ut p u t F i l e ) ;
F il e A s s e r t .a s se r t E q u a l s ( "S a v e A s - e d f il e h a s w r o n g c on t e n t s " ,
ex p e c t e d ,
ou t p u t F i l e ) ;
c on t r o l . v e ri f y( ) ;

Fix the a dd ( ) related tests in T es t M o v i e L i s t E di t o r .


p u b li c v o i d te s tA d d i n g ( ) t hr o w s D u p l i c a te M o v i e E x c e p t i o n {
S tr i n g L O S T I N S P A C E = " Lo s t I n S p a c e ";
M ov i e l o s t In S pa c e = n e w Mo v i e ( L O S T I N S P A C E , C a t e g o r y. S C I F I , 3 ) ;
V ec t o r m o v ie s Wi t h A d d i t i o n = n e w V e c t o r( m o v i e s ) ;
m ov i e s W i t h Ad d it i o n . a d d ( l os t I n S p a c e ) ;
m oc k V i e w . s et M ov i e s ( m o v i e s) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . g et N am e F i e l d ( ) ;
c on t r o l . s e tR e tu r n V a l u e ( L OS T I N S P A C E , 1 ) ;
m oc k V i e w . g et C at e g o r y F i e l d( ) ;
c on t r o l . s e tR e tu r n V a l u e ( C at e g o r y . S C I F I , 1 ) ;
m oc k V i e w . g et R at i n g F i e l d ( );
c on t r o l . s e tR e tu r n V a l u e ( 3 , 1 ) ;
m oc k V i e w . s et M ov i e s ( m o v i e sW i t h A d d i t i o n );
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
c on t r o l . a c ti v at e ( ) ;
M ov i e L i s t E di t or e d i t o r = n e w M o v i e L i s tE d i t o r ( m o v i e L i st , m o c k V i e w ) ;
e di t o r . a d d () ;
c on t r o l . v e ri f y( ) ;
}
p u b li c v o i d te s tD u p l i c a t e C au s i n g A d d ( ) {
m oc k V i e w . s et M ov i e s ( m o v i e s) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
m oc k V i e w . g et N am e F i e l d ( ) ;
c on t r o l . s e tR e tu r n V a l u e ( " St a r W a r s " , 1 );
m oc k V i e w . g et C at e g o r y F i e l d( ) ;
c on t r o l . s e tR e tu r n V a l u e ( C at e g o r y . S C I F I , 1 ) ;
m oc k V i e w . g et R at i n g F i e l d ( );
c on t r o l . s e tR e tu r n V a l u e ( 5 , 1 ) ;
m oc k V i e w . d up l ic a t e E x c e p t io n ( " S t a r W a r s" ) ;
c on t r o l . s e tV o id C a l l a b l e ( 1) ;
c on t r o l . a c ti v at e ( ) ;
M ov i e L i s t E di t or e d i t o r = n e w M o v i e L i s tE d i t o r ( m o v i e L i st , m o c k V i e w ) ;
e di t o r . a d d () ;
}

c on t r o l . v e ri f y( ) ;

16.
Answer
16:

Fix up the add related tests in T e s t S w i n g M o v ie L i s t E d i t o r V i e w.


p u b li c v o i d te s tA d d i n g ( ) {
S tr i n g L O S T I N S P A C E = " Lo s t I n S p a c e ";
M ov i e l o s t In S pa c e = n e w Mo v i e ( L O S T I N S P A C E , C a t e g o r y. S C I F I , 3 ) ;
m ov i e s . a d d (l o st I n S p a c e ) ;
J Te x t F i e l d Op e ra t o r n e w M o vi e F i e l d =
ne w J T e x t F i e l d Op e r a t o r ( m a i n Wi n d o w ,
n e w N a m e B a s e d C h o o se r ( " n a m e " ) ) ;
n ew M o v i e F i el d .e n t e r T e x t ( LO S T I N S P A C E );
J Co m b o B o x O pe r at o r r a t i n g Co m b o =
n e w J C om b o B o x O p e r at o r ( m a i n W i n d ow ,
n e w N a m eB a s e d C h o o s e r ( " ra t i n g " ) ) ;
r at i n g C o m b o. s et S e l e c t e d I nd e x ( 4 ) ;
J Co m b o B o x O pe r at o r c a t e g o ry C o m b o =
n e w J C om b o B o x O p e r at o r ( m a i n W i n d ow ,
n e w N a m eB a s e d C h o o s e r ( " ca t e g o r y " ) ) ;
c at e g o r y C o mb o .s e t S e l e c t e dI n d e x ( 1 ) ;
J Bu t t o n O p e ra t or a d d B u t t o n =
n e w JB u tt o n O p e r a t o r( m a i n W i n d o w ,
n e w N a m e B a se d C h o o s e r ( " a d d ") ) ;
a dd B u t t o n . do C li c k ( ) ;
J Li s t O p e r a to r m o v i e L i s t =
n e w J Li s tO p e r a t o r ( m ai n W i n d o w ,
ne w N a m e B a s e d Ch o o s e r ( " m o v i e L is t " ) ) ;
L is t M o d e l li s tM o d e l = m o vi e L i s t . g e t M o de l ( ) ;
a ss e r t E q u a ls ( "M o v i e l i s t i s t h e w r o n g s i z e " , m o v i e s . si z e ( ) ,
l i s t M o d e l . ge t S i z e ( ) ) ;
f or ( i n t i = 0; i
< m ov i e s . s i z e( ) ; i + + ) {
a s s e r t E q ua l s( " M o v i e l i st c o n t a i n s b ad m o v i e a t i n d ex " + i , m o v ie s . g e t ( i ) ,
l i s t M o d e l .g e t E l e m e n t A t (i ) ) ;
}
}
p u b li c v o i d te s tD u p l i c a t e C au s i n g A d d ( ) {
J Te x t F i e l d Op e ra t o r n e w M o vi e F i e l d =
ne w J T e x t F i e l d Op e r a t o r ( m a i n Wi n d o w ,
n e w N a m e B a s e d C h o o se r ( " n a m e " ) ) ;
n ew M o v i e F i el d .e n t e r T e x t ( st a r W a r s . g e t N am e ( ) ) ;
J Co m b o B o x O pe r at o r r a t i n g Co m b o =
n e w J C om b o B o x O p e r at o r ( m a i n W i n d ow ,
n e w N a me B a s e d C h o o s e r ( "r a t i n g " ) ) ;
r at i n g C o m b o. s et S e l e c t e d I nd e x ( 4 ) ;
J Co m b o B o x O pe r at o r c a t e g o ry C o m b o =
n e w J C om b o B o x O p e r at o r ( m a i n W i n d ow ,
n e w N a me B a s e d C h o o s e r ( "c a t e g o r y " ) ) ;
c at e g o r y C o mb o .s e t S e l e c t e dI n d e x ( 1 ) ;

17.

Answer
17:

J Bu t t o n O p e ra t or a d d B u t t o n =
n e w JB u tt o n O p e r a t o r( m a i n W i n d o w ,
n e w N a m e B as e d C h o o s e r ( " a d d" ) ) ;
a dd B u t t o n . pu s hN o B l o c k ( ) ;
c he c k D u p l i ca t eE x c e p t i o n D ia l o g ( ) ;
J Li s t O p e r a to r m o v i e L i s t =
n e w J Li s tO p e r a t o r ( m ai n W i n d o w ,
n e w N a m e B a s e dC h o o s e r ( " m o v i e Li s t " ) ) ;
c he c k L i s t I sU n ch a n g e d ( m o v ie L i s t ) ;

Do the split: T e s t Mo v ie L i s t E d i t o rF i l e O p e r a t i o ns into T e s t M o v i e Li s t E d i t o r S a v in g and


T e s tM o v i e L i s tE d it o r L o a d i n g .
1. Rename T es t M o v i e L i s tE d i t o r F i l e O p er a t i o n s to T e s t M o v i eL i s t E d i t o r S a vi n g .
2. Create T e st M o v i e L i s t Ed i t o r L o a d i n g , copying loading related parts of
T es t M o v i e L is tE d i t o r S a v i n g:
p u b li c c l a s s T e st M o v i e L i s t Ed i t o r L o a d i n g
e x t e n ds Co m m o n T e s t M ov i e L i s t E d i t o r {

p ri v a t e V e ct o r e m p t y M o v i es ;
p ri v a t e F i l e i np u t F i l e ;
p ro t e c t e d vo i d s e t U p ( ) t hr o w s E x c e p t i on {
s u p e r . s e tU p () ;
i n p u t F i l e = F i l e . c r e a t eT e m p F i l e ( " t e st S a v i n g " , " d a t ") ;
i n p u t F i l e. d el e t e O n E x i t () ;
e m p t y M o v ie s = n e w V e c t or ( ) ;
}
p ub l i c v o i d t es t L o a d i n g ( ) t h r o w s E x c e pt i o n {
m o c k V i e w .s e tM o v i e s ( e m p ty M o v i e s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
m o c k V i e w .g e tF i l e T o L o a d () ;
c o n t r o l . se t Re t u r n V a l u e (i n p u t F i l e , 1 );
m o c k V i e w .s e tM o v i e s ( m o v ie s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
c o n t r o l . ac t iv a t e ( ) ;

M o v i e L i s tE d it o r e d i t o r = n e w M o v i e L is t E d i t o r ( n e w M ov i e L i s t ( ) ,
mockView);
a s s e r t T r ue ( "E d i t o r s h o ul d h a v e l o a d ed . " , e d i t o r . l o ad ( ) ) ;
c o n t r o l . ve r if y ( ) ;

p ub l i c s t a ti c v o i d m a i n ( St r i n g [ ] a r g s) {
j u n i t . t e xt u i. T e s t R u n n e r. r u n ( T e s t M o v ie L i s t E d i t o r L o a di n g . c l a s s ) ;
}

18.

In looking at adding another test to T e s t S w i n g M ov i e L i s t E d i t o r F il e O p e r a t i o n s , we notice that we


have some code debt. The Rule of Three has kicked in. This is the third test, and as we think about it we see
that there are a few lines of code duplicated in each test. Time to refactor. What do we need to do?

Answer
18:

Each test begins with these three lines that need to be moved to the end of s e t U p( ) and removed from the
two existing tests:
m e n ub a r = n e w J Me n u B a r O p e r at o r ( m a i n W i n d ow ) ;
f i l eM e n u = n e w J Me n u O p e r a t o r( m e n u b a r , " F il e " ) ;
f i l eM e n u . p u s h( ) ;

19.
Answer
19:

What does the fixture look like?


p u b li c c l a s s T e st M o v i e L i s t So r t i n g B y N a m e e x t e n d s T e s t C a se {
p ri v a t e M o vi e Li s t s o r t e d Li s t = n u l l ;
p ri v a t e M o vi e Na m e C o m p a r a to r n a m e C o m p a ra t o r = n u l l ;
p ri v a t e V e ct o r s o r t e d M o v ie s = n u l l ;
p ri v a t e M o vi e Li s t e m p t y L is t = n u l l ;
p ro t e c t e d vo i d s e t U p ( ) t hr o w s E x c e p t i on {
e m p t y L i s t = n e w M o v i e L is t ( ) ;
s o r t e d M o vi e s = n e w V e c to r ( ) ;
s o r t e d M o vi e s. a d d ( n e w
s o r t e d M o vi e s. a d d ( n e w
s o r t e d M o vi e s. a d d ( n e w
s o r t e d M o vi e s. a d d ( n e w
s o r t e d M o vi e s. a d d ( n e w
s o r t e d M o vi e s. a d d ( n e w
s o r t e d M o vi e s. a d d ( n e w
- 1 ) );

M ov i e ( " A " ,
M ov i e ( " B " ,
M ov i e ( " C " ,
M ov i e ( " D " ,
M ov i e ( " E " ,
M ov i e ( " F " ,
M ov i e ( " G " ,

C a t eg o r y . S C I F I ,
C a t eg o r y . S C I F I ,
C a t eg o r y . S C I F I ,
C a t eg o r y . S C I F I ,
C a t eg o r y . S C I F I ,
C a t eg o r y . S C I F I ,
C a t eg o r y . S C I F I ,

s o r t e d L i st = ne w M o v i e L i s t( ) ;
I t e r a t o r i = s o r t e d M o v ie s . i t e r a t o r ( );
w h i l e ( i .h a sN e x t ( ) ) {
s o r t e d Li s t. a d d ( ( M o v i e) i . n e x t ( ) ) ;
}
n a m e C o m p ar a to r = n e w M ov i e N a m e C o m p a ra t o r ( ) ;

}
/ /. . .

5 ) );
4 ) );
3 ) );
2 ) );
1 ) );
0 ) );

20.
Answer
20:

Do the extraction of the fixture.


pu b l ic c l a s s Te s tM o v i e R a t i n gs e x t e n d s T e st C a s e {
p r iv a t e M o v ie st a r T r e k = nu l l ;

21.
Answer
21:

p r ot e c t e d v oi d s e t U p ( ) t h ro w s E x c e p t i o n {
st a r T r e k = n ew M o v i e ( " S ta r T r e k " , C a te g o r y . S C I F I ) ;
}
p u bl i c v o i d t e st U n r a t e d C o ns t r u c t o r ( ) {
tr y {
s t a r T r e k. g et R a t i n g ( ) ;
f a i l ( " G et t in g r a t i n g of a n u n r a t e d M o v i e s h o u l d t hr o w U n r a t e d M ov i e E x c e p t i o n" ) ;
} c a t c h ( Un r at e d E x c e p t i on e x ) {
}
}
p u bl i c v o i d t e st A d d i n g O n e Ra t i n g ( ) t h r o ws E x c e p t i o n {
st a r T r e k . ad d Ra t i n g ( 3 ) ;
as s e r t E q u al s (" B a d a v e r a ge r a t i n g o f 1. " , 3 , s t a r T r e k. g e t R a t i n g ( ) );
}
p u bl i c s t a t ic vo i d m a i n ( S tr i n g [ ] a r g s ) {
ju n i t . t e x tu i .T e s t R u n n e r .r u n ( T e s t M o v i eR a t i n g s . c l a s s ) ;
}

What is the problem, and how do we fix it?


The expected saved data strings don't include a rating count value.
They need to be updated to:
p u b li c c l a s s T e st M o v i e L i s t Ed i t o r S a v i n g ex t e n d s C o m m o n T es t M o v i e L i s t E di t o r {
p ro t e c t e d vo i d s e t U p ( ) t hr o w s E x c e p t i on {
s u p e r . s e tU p () ;
e x p e c t e d = "S t a r W a r s | Sc i e n c e F i c t i on | 5 | 1 \ n " +
"Star Trek|Science Fiction|3|1\n" +
" S t a r ga t e | S c i e n c e Fi c t i o n | 0 | 0 \ n " +
" T h e Sh i n i n g | H o r r o r| 2 | 1 \ n " ;
e x t e n d e d Ex p ec t e d = e x p ec t e d + " T h e Fe l l o w s h i p o f T he R i n g | F a n t a sy | 5 | 1 \ n " ;
}
/ /. . .
}
p u b li c c l a s s T e st S w i n g M o v i eL i s t E d i t o r F i le O p e r a t i o n s e x te n d s T e s t C a s e {
p ro t e c t e d vo i d s e t U p ( ) t hr o w s E x c e p t i on {
//. . .
s a v e d T e x t = " S t a r W a r s |S c i e n c e F i c t io n | 5 | 1 \ n " +
"Star Trek|Science Fiction|3|1\n" +
" S t a rg a t e | S c i e n c e F i c t i o n | 0 | 0 \ n " ;
e x t e n d e d Sa v ed T e x t = s a ve d T e x t + " T h e S h i n i n g | H o r r o r| 2 | 1 \ n " ;
}
}

22.
Answer
22:

Convert T e st M ov i e L i s t E d i to r S a v i n g as required.
p u b li c c l a s s T e st M o v i e L i s t Ed i t o r S a v i n g ex t e n d s X M L T e s t Ca s e {
p ro t e c t e d Mo c kC o n t r o l c o nt r o l = n u l l ;
p ro t e c t e d Mo v ie L i s t E d i t o rV i e w m o c k V i e w = n u l l ;
p ro t e c t e d Ve c to r m o v i e s = n u l l ;
p ro t e c t e d Mo v ie s t a r W a r s = n u l l ;
p ro t e c t e d Mo v ie s t a r T r e k = n u l l ;
p ro t e c t e d Mo v ie s t a r g a t e = n u l l ;
p ro t e c t e d Mo v ie t h e S h i n i ng = n u l l ;
p ro t e c t e d Mo v ie L i s t m o v i eL i s t = n u l l ;

p ri v a t e
p ri v a t e
p ri v a t e
p ri v a t e
p ri v a t e
p ri v a t e

S t ri n g e x p e c t e d P re f i x ;
S t ri n g e x p e c t e d ;
F i le ou t p u t F i l e ;
M o vi e f o t r ;
S t ri n g e x t e n d e d E xp e c t e d ;
V e ct o r e x t e n d e d M ov i e s ;

p ub l i c T e s tM o vi e L i s t E d i t or S a v i n g ( S t r i ng n a m e ) {
s u p e r ( n a me ) ;
}
p ro t e c t e d vo i d s e t U p ( ) t hr o w s E x c e p t i on {
S t r i n g p ar s er = " o r g . a pa c h e . x e r c e s . ja x p . D o c u m e n t B u il d e r F a c t o r y I mp l " ;
X M L U n i t . se t Co n t r o l P a r s er ( p a r s e r ) ;
X M L U n i t . se t Te s t P a r s e r ( pa r s e r ) ;

-1);

s t a r W a r s = ne w M o v i e ( " St a r W a r s " , C at e g o r y . S C I F I , 5) ;
s t a r T r e k = ne w M o v i e ( " St a r T r e k " , C at e g o r y . S C I F I , 3) ;
s t a r g a t e = ne w M o v i e ( " St a r g a t e " , C a te g o r y . S C I F I ,
t h e S h i n i ng = n e w M o v i e (" T h e S h i n i n g ", C a t e g o r y . H O R RO R , 2 ) ;
m o v i e s = ne w Ve c t o r ( ) ;
m o v i e s . a dd ( st a r W a r s ) ;
m o v i e s . a dd ( st a r T r e k ) ;
m o v i e s . a dd ( st a r g a t e ) ;
m o v i e s . a dd ( th e S h i n i n g ) ;
m o v i e L i s t = n e w M o v i e L i st ( ) ;
m o v i e L i s t. a dd ( s t a r W a r s );
m o v i e L i s t. a dd ( s t a r T r e k );
m o v i e L i s t. a dd ( s t a r g a t e );
m o v i e L i s t. a dd ( t h e S h i n i ng ) ;
e x p e c t e d Pr e fi x = " < m o v ie l i s t > " +
" < m o v ie n a m e = \ " S t a r W a r s \ " " +
" c a t e g o r y = \ "S c i e n c e F i c t i o n\ " > " +
" < r at i n g s > " +
" <r a t i n g v a l u e =\ " 5 \ " " +
" s o u r c e= \ " A n o n y m o u s \ " / > " +
" < /r a t i n g s > " +
" < / m ov i e > " +
" < m o v ie n a m e = \ " S t a r T r e k \ " " +
" c a t e g o r y= \ " S c i e n c e F i c ti o n \ " > " +
" < ra t i n g s > " +
"< r a t i n g v a l u e= \ " 3 \ " " +
" s o u r ce = \ " A n o n y m o u s \ " / > " +
" < /r a t i n g s > " +
" < / m o vi e > " +
" < m o v ie n a m e = \ " S t a rg a t e \ " " +
" c a t e g o r y = \ "S c i e n c e F i c t i o n\ " > " +
" < r at i n g s > < / r a t i ng s > " +
" < / m o vi e > " +
" < m o v ie n a m e = \ " T h e S h i n i n g \ " " +
" c a t e g o r y = \ "H o r r o r \ " > " +
" < r at i n g s > " +
" <r a t i n g v a l u e =\ " 2 \ " " +
" s o u r c e= \ " A n o n y m o u s \ " / > " +
" < / ra t i n g s > " +
" < / m o vi e > " ;
e x p e c t e d = ex p e c t e d P r e fi x + " < / m o v i el i s t > " ;
f o t r = n ew Mo v i e ( " T h e Fe l l o w s h i p o f T h e R i n g " , C a t eg o r y . F A N T A S Y , 5 ) ;
e x t e n d e d Ex p ec t e d = e x p ec t e d P r e f i x +
" < m o v ie n a m e = \ " T h e F e l l o w s h i p o f Th e R i n g \ " " +
" c a t e g o r y = \ "F a n t a s y \ " > " +
" < r at i n g s > " =
" <r a t i n g v a l u e =\ " 5 \ " " +
" s o u r c e= \ " A n o n y m o u s \ " / > " +
" < / ra t i n g s > " +
" < / m o vi e > < / m o v i e l i st > " ;
e x t e n d e d Mo v ie s = n e w V ec t o r ( m o v i e s ) ;
e x t e n d e d Mo v ie s . a d d ( f o t r) ;
o u t p u t F i le = F i l e . c r e a te T e m p F i l e ( " t es t S a v i n g " , " . d at " ) ;
o u t p u t F i le . de l e t e O n E x i t( ) ;

c o n t r o l = E as y M o c k . c o n tr o l F o r ( M o v i e Li s t E d i t o r V i e w . cl a s s ) ;
m o c k V i e w = (M o v i e L i s t E di t o r V i e w ) c o n tr o l . g e t M o c k ( ) ;
m o c k V i e w .s e tE d i t o r ( n u l l) ;
c o n t r o l . se t De f a u l t V o i d Ca l l a b l e ( ) ;

p ub l i c v o i d t es t S a v i n g ( ) t h r o w s E x c e p ti o n {
m o c k V i e w .s e tM o v i e s ( m o v ie s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
m o c k V i e w .g e tF i l e T o S a v e () ;
c o n t r o l . se t Re t u r n V a l u e (o u t p u t F i l e , 1) ;

m o c k V i e w .g e tN a m e F i e l d ( );
c o n t r o l . se t Re t u r n V a l u e (f o t r . g e t N a m e () , 1 ) ;
m o c k V i e w .g e tC a t e g o r y F i el d ( ) ;
c o n t r o l . se t Re t u r n V a l u e (C a t e g o r y . F A N TA S Y , 1 ) ;
m o c k V i e w .g e tR a t i n g F i e l d( ) ;
c o n t r o l . se t Re t u r n V a l u e (f o t r . g e t R a t i ng ( ) + 1 , 1 ) ;
m o c k V i e w .s e tM o v i e s ( e x t en d e d M o v i e s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
c o n t r o l . ac t iv a t e ( ) ;
M o v i e L i s tE d it o r e d i t o r = n e w M o v i e L is t E d i t o r ( m o v i e Li s t , m o c k V i e w) ;
a s s e r t T r ue ( "E d i t o r s h o ul d h a v e s a v e d" , e d i t o r . s a v e As ( ) ) ;
a s s e r t X M LE q ua l ( " S a v e A s- e d f i l e h a s w r o n g c o n t e n t s " ,
expected,
c o n t e n t s O f( o u t p u t F i l e ) );

e d i t o r . a dd ( );
a s s e r t T r ue ( "E d i t o r s h o ul d h a v e r e s a ve d " , e d i t o r . s a ve ( ) ) ;
a s s e r t X M LE q ua l ( " S a v e d fi l e h a s b a d co n t e n t s . " ,
e x t e n d e d Ex p e c t e d ,
c o n t e n t s Of ( o u t p u t F i l e )) ;
c o n t r o l . ve r if y ( ) ;

p ri v a t e S t ri n g c o n t e n t s O f( F i l e a F i l e ) t h r o w s I O E x c e p ti o n {
F i l e I n p u tS t re a m f s t r e a m = n e w F i l e I np u t S t r e a m ( a F i l e) ;
i n t s i z e = fs t r e a m . a v a il a b l e ( ) ;
b y t e [ ] bu f fe r = n e w b yt e [ s i z e ] ;
f s t r e a m . re a d( b u f f e r ) ;
re t u r n n e w S tr i n g ( b u f f e r) ;
}
p ub l i c v o i d t es t C a n c e l l e dS a v i n g ( ) t h r ow s E x c e p t i o n {
m o c k V i e w .s e tM o v i e s ( m o v ie s ) ;
c o n t r o l . se t Vo i d C a l l a b l e( 1 ) ;
m o c k V i e w .g e tF i l e T o S a v e () ;
c o n t r o l . se t Re t u r n V a l u e (n u l l , 1 ) ;
c o n t r o l . ac t iv a t e ( ) ;

23.
Answer
23:

M o v i e L i s tE d it o r e d i t o r = n e w M o v i e L is t E d i t o r ( m o v i e Li s t , m o c k V i e w) ;
a s s e r t F a ls e (" E d i t o r s h ou l d n o t h a v e s a v e d . " , e d i t o r. s a v e A s ( ) ) ;
c o n t r o l . ve r if y ( ) ;

p ub l i c s t a ti c v o i d m a i n ( St r i n g [ ] a r g s) {
j u n i t . t e xt u i. T e s t R u n n e r. r u n ( T e s t M o v ie L i s t E d i t o r S a v in g . c l a s s ) ;
}

Update M ov i e L i s t E di to r . s a v e ( ) to make it work.


p u b li c b o o l e an sa v e ( ) t h r o ws I O E x c e p t i o n {
i f ( o u t p u t Fi l e = = n u l l ) {
r e t u r n f al s e;
}
F il e W r i t e r w r it e r = n e w Fi l e W r i t e r ( o u tp u t F i l e ) ;
n ew X M L M o v ie L is t W r i t e r ( w ri t e r ) . w r i t e ( mo v i e s ) ;
w ri t e r . c l o se ( );
r et u r n t r u e;
}

Amazon

Prev

don't be afraid of buying books

Next

BIBLIOGRAPHY
[1] Scott W. Ambler. The Object Primer: The Application Developer's Guide to Object Orientation.
Cambridge University Press, New York, 2nd edition, 2001. www.ambysoft.com/theObjectPrimer.html.
[2] Scott W. Ambler. Agile Modeling: Effective Practices for Extreme Programming and the Unified
Process. John Wiley & Sons Publishing, New York, 2002.
[3] Scott W. Ambler. Enterprise unified process white paper. Technical report, Ronin International, 2002.
www.ronin-intl.com/publications/unifiedProcess.htm.
[4] Scott W. Ambler. The Elements of UML Style. Cambridge University Press, New York, 2003.
www.ambysoft.com/elementsUMLStyle.html.
[5] Dave Astels. Refactoring with UML. In Proceedings of XP2002: 3rd International Conference on
eXtreme Programming and Flexible Processes in Software Engineering, pages 6770, 2002. Available at
c ic l am in o.d i be.u n ige. i t/xp 2002 / a t t i .
[6] David Astels, Granville Miller, and Miroslav Novak. A Practical Guide to eXtreme Programming. The
Coad Series. Prentice Hall, 2002. ISBN 0-13-067482-6.
[7] Kent Beck. Simple smalltalk testing: With patterns. Available at
www.xprogramming.com/testfram.htm.
[8] Kent Beck. Extreme Programming Explained: Embrace Change. The XP Series. Addison Wesley
Longman, 2000. ISBN 0-201-61641-6.
[9] Kent Beck. Test-Driven Development: By Example. Addison Wesley Longman, 2002. ISBN 0-32114653-0.
[10] Joshua Bloch. Effective Java Programming Language Guide. Addison Wesley Longman, 2001. ISBN
0-201-31005-8.
[11] Marko Boger, Thorsten Strum, and Per Fragemann. Refactoring browser for UML. In Proceedings of
XP2002: 3rd International Conference on eXtreme Programming and Flexible Processes in Software
Engineering, pages 7781, 2002. Available at c i c l a m i n o . d i b e . u n i g e . i t / x p 2 0 0 2 / a t t i .
[12] Peter Coad, Mark Mayfield, and Jonathan Kern. Java Design: Building Better Apps and Applets.
Yourdon Press Computing Series. Prentice Hall, 2nd edition, 1999. ISBN 0-139-11181-6.
[13] Alistair Cockburn. Agile Software Development. Addison Wesley Longman, October 2001.
[14] Michael Feathers. The humble dialog box. Published online, 2002. Available at
www.objectmentor.com/resources/articles.
[15] Martin Fowler. The refactoring home page. www.refactoring.com.
[16] Martin Fowler. Refactoring: Improving the Design of Existing Code. Addison Wesley Longman, 1999.
ISBN 0-201-48567-2.
[17] Martin Fowler. Is design dead? In Extreme Programming Examined, chapter 1, pages 3-17. Addison
Wesley Longman, 2001. Also available at www.martinfowler/articles/designDead.htm.
[18] Tammo Freese. Easymock: Dynamic Objects for jUnit. In Proceedings of XP2002: 3rd International
Conference on eXtreme Programming and Flexible Processes in Software Engineering, pages 2-5, 2002.
Available at cic la min o .dib e .uni ge. i t / x p 2 0 0 2 / a t t i .
[19] E. Gamma, R. Helm, R. Johnson, and J. Vlissides. Design Patterns: Abstraction and Reuse in ObjectOriented Designs. In O. Nierstrasz, editor, Proceedings of ECOOP'93, Berlin, 1993. Springer-Verlag.
[20] Erich Gamma and Kent Beck. jUnit: A cook's tour. Jave Report, May 1999. Available at
junit.sourceforge.net/doc/cookstour/cookstour.htm.
[21] The Standish Group. The CHAOS report, 1994.
[22] The Standish Group. CHAOS: A recipe for success, 1999.
[23] Peter Haggar. Practical Java Programming Language Guide. Addison Wesley Longman, February

2000.
[24] Jim Highsmith. Agile Software Development Ecosystems. Addison Wesley Longman, March 2002.
ISBN: 0-201-76043-6.
[25] Andrew Hunt and David Thomas. The Pragmatic Programmer. Addison Wesley Longman, 2000. ISBN
0-201-61622-X.
[26] Ron Jeffries. XProgramming.com: An extreme programming resource. www.xprogramming.com.
[27] Ron Jeffries, Ann Anderson, and Chet Hendrickson. Extreme Programming Installed. The XP Series.
Addison Wesley Longman, 2001. ISBN 0-201-70842-6.
[28] Joshua Kerievsky. Patterns and XP. In Extreme Programming Examined, chapter 13, pages 207-220.
Addison Wesley Longman, 2001.
[29] Joshua Kerievsky. Refactoring to Patterns. Not yet published, 2002. Drafts available online at
www.industriallogic.com/xp/refactoring.
[30] P. Kruchten. The Rational Unified Process: An Introduction. Addison Wesley Longman, 2nd edition,
2000.
[31] Tim Mackinnon. xUnit testinga plea for assertEquals. In Proceedings of XP2002: 3rd International
Conference on eXtreme Programming and Flexible Processes in Software Engineering, pages 170-171,
2002. Available at ci c lami n o.di be.u n i g e . i t / x p 2 0 0 2 / a t t i .
[32] Tim Mackinnon, Steve Freeman, and Philip Craig. Endo-testing: Unit testing with mock objects. In
Extreme Programming Examined, chapter 17, pages 287-301. Addison Wesley Longman, 2001. Available
at www.mockobjects.com/endotesting.html.
[33] Robert C. Martin. Agile Software Development: Principles, Patterns, and Practices. Prentice Hall,
2003. ISBN 0-13-597444-5.
[34] Pete McBreen. Software Craftsmanship. Addison Wesley Longman, 2002. ISBN 0-201-73386-2.
[35] Ivan Moore. JesterA junit test tester. In Proceedings of XP2001: 2nd International Conference on
eXtreme Programming and Flexible Processes in Software Engineering, pages 8487, 2001.
[36] Stephen R. Palmer and John M. Felsing. A Practical Guide to Feature-Driven Development. Prentice
Hall, 2002.
[37] Giancarlo Succi and Michele Marchesi, editors. Extreme Programming Examined. The XP Series.
Addison Wesley Longman, 2001. This is a collection of papers from the XP2000 proceedings. ISBN 0201-71040-4.
[38] Giancarlo Succi, Michele Marchesi, Witold Pedrycz, and Laurie Williams. Preliminary analysis of the
effects of pair programming on job satisfaction. In Proyceedings of XP2002: 3rd International Conference
on eXtreme Programming and Flexible Processes in Software Engineering, pages 212-215, 2002.
Available at cic la min o .dib e .uni ge. i t / x p 2 0 0 2 / a t t i .
[39] Arie van Deursen and Leon Moonen. The video store revisitedthoughts on refactoring and testing.
In Proceedings of XP2002: 3rd International Conference on eXtreme Programming and Flexible Processes
in Software Engineering, pages 7176, 2002. Available at
c ic l am in o.d i be.u n ige. i t/xp 2002 / a t t i .
[40] Arie van Deursen, Leon Moonen, Alex van den Bergh, and Gerard Kok. Refactoring test code. In
Proceedings of XP2001: 2nd International Conference on eXtreme Programming and Flexible Processesin
Software Engineering, pages 9295, 2001. Available at www.cwi.nl/~leon/papers/xp2001/xp2001.pdf.
[41] William C. Wake. Refactoring Workbook. Addison Wesley Longman, 2003. ISBN 0-321-10929-5.

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL ] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
"Don't Repeat Yourself" (Thomas/Hunt)
"Save As" functionality in GUI 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
"Save" functionality in GUI 2nd 3rd 4th 5th 6th
"Say Everything Once and Only Once" (Jeffries et al.)

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
AbstractExpectation class
AbstractExpectationCollection class
ActiveTestSuite
add() 2nd 3rd 4th
Adding a movie
adding a movie 2nd
Adding a movie
adding a movie
Adding a movie
adding a movie
Adding a movie
adding a movie
Adding a movie
adding a movie
Adding a movie 2nd
adding a movie
Adding a movie
logical layer request for required data and updating of movie list
adding a movie
logical layer request for required data and updating of movie list 2nd
Adding a movie
logical layer request for required data and updating of movie list
adding a movie
logical layer request for required data and updating of movie list
Adding a movie
logical layer request for required data and updating of movie list
adding a movie
logical layer request for required data and updating of movie list
Adding a movie
logical layer request for required data and updating of movie list
adding a movie
text field for movie name and add button
Adding a movie
text field for movie name and add button
adding a movie
text field for movie name and add button
Adding a movie
text field for movie name and add button
adding a movie
text field for movie name and add button
Adding a movie
text field for movie name and add button 2nd

adding a movie
text field for movie name and add button
addRating() 2nd 3rd 4th 5th
Agile Modeling (AM), and test-driven development
ALL category 2nd
Ant integration, Clover
ANT task, running tasks as 2nd
ArrayList
Assert superclass 2nd
assertEquals() 2nd
assertFalse()
assertNotNull()
assertNotSame() 2nd
assertNull()
assertSame() 2nd
assertTrue()
fail()
Assert, starting with
assertCollectionsEquals() 2nd
assertEquals for Object()
assertEquals()
assertExcludes
assertFileEquals() 2nd 3rd
assertIncludes
assertInstanceOf()
AssertionFailedError 2nd
Assertions 2nd 3rd 4th
AssertMo
assertNotXPathExists()
assertStartsWith
assertXMLEqual
assertXMLEquals()
assertXMLIdentical()
assertXPathExists()
assertXPathsEqual()
assertXPathsNotEqual()
Associated tests, eXtreme Programming
awtSleep(), JFCTestCase
awtSleep(long sleepTime), JFCTestCase

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Bacon, Tim
BaseTestCase 2nd
Beck, Kent 2nd
Big Design Up Front (BDUF)
Block assertions, SUnit
Boilerplate markup
BookList class 2nd 3rd 4th
Boolean assertions, SUnit
Bootstrap technique
Boundary conditions, testing early
Bowler, Mike
Brute force, and GUI test-first 2nd 3rd 4th 5th 6th

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
calculate()
calculateAverageRating()
calculateTotalRating()
categories
Categories 2nd
categories 2nd
Categories
categories
adding a category
Categories
adding a category 2nd
categories
adding a category 2nd
Categories
adding a category
categories
adding a selection of category
Categories
adding a selection of category 2nd
categories
adding a selection of category
Categories
adding a selection of category
categories
adding a selection of category 2nd
Categories
adding a selection of category
categories
adding a selection of category
Categories
adding a selection of category
categories
adding support for a category to Movie
Categories
adding support for a category to Movie
constraining a fixed set of alternatives
categories
constraining a fixed set of alternatives
Categories
extending testSelecting
categories
extending testSelecting
Categories
setUp(), updating
categories
setUp(), updating
Categories

setUp(), updating
categories
setUp(), updating
Categories
showing a category in GUI
categories
showing a category in GUI
Categories
showing a category in GUI
categories
showing a category in GUI
Categories
showing a category in GUI
categories
showing a category in GUI 2nd
Categories
showing a category in GUI
categories
testing for other categories 2nd
Categories
testing for other categories 2nd
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories

testing for other categories


categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories 2nd
categories
testing for other categories 2nd
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories
categories
testing for other categories
Categories
testing for other categories 2nd
categories
testing for other categories 2nd
Categories
testing for other categories
Category class
categorySublist()
Clark, Mike
Class names
for test class
Class-level report, Clover 2nd
cleanUp(), JFCTestHelper
Clover 2nd
Ant integration
class level report 2nd
class-level report 2nd

defined
package-level report 2nd
project level report 2nd
project-level report 2nd
user guide
Code
and test-driven development 2nd 3rd
and testing 2nd
duplication 2nd 3rd
example of
removing 2nd
Code smells 2nd 3rd 4th 5th 6th 7th 8th
and refactoring
comments
concept
data classes 2nd 3rd
defined
duplicated code
examples of 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th
explaining variable, introducing
Extract Class 2nd
Extract Interface 2nd 3rd 4th
Extract Method 2nd 3rd
form template method 2nd 3rd
replace conditional with polymorphism
replace constructor with factory method 2nd
replace inheritance with delegation 2nd 3rd
replace magic number with symbolic constant 2nd
replace nested conditional with guard clauses 2nd
replace type code with subclasses
in source code
inappropriate intimacy 2nd
large classes
lazy classes
long method
origin of term
Shotgun Surgery 2nd
switch statements 2nd
to patterns
Coding hat
CommonTestMovieListEditor
Conditional, replacing with polymorphism
Constructor, replacing with factory method 2nd
contains()
CountingNodeTester class
CruiseControl
Customer Tests
CustomMovieListRenderer

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
DaeDalos JUnit extensions 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th
DatabaseChecker 2nd 3rd 4th
defined
Extensible TestCase
programmer test implementation using test resources 2nd 3rd 4th 5th
test resources, managing 2nd
TRTestRunner
Data classes 2nd 3rd
DatabaseChecker 2nd 3rd 4th
Databases/network resources, avoiding testing against
Debugging 2nd
Delegation, replacing inheritance with 2nd 3rd
delete()
Design, project 2nd 3rd
DetailedIllegalArgumentException
DetailedNullPointerException 2nd
DifferenceConstants class
DifferenceEngine class
DifferenceListener
DisposeWindow(), JFCTestHelper
DocumentTraversal interface
Dollery, Bryan
Duplicate movie message
DuplicateMovieException 2nd 3rd 4th 5th
Duplication of code 2nd 3rd 4th
example of
removing 2nd

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E ] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
EasyMock 2nd 3rd 4th 5th 6th 7th
advanced features 2nd 3rd
advantage of
basic features 2nd
defined
disadvantages of 2nd
Eclipse 2nd 3rd
JUnit integration
JUnit plugin for 2nd 3rd
JUnit view in
problem indication
refactoring support
enterClickAndLeave(), JFCTestHelper
enterDragAndLeave(), JFCTestHelper
enterDropDownClickAndLeave(), JFCTestHelper
enterJListClickAndLeave(), JFCTestHelper
equals()
EqualsTester 2nd 3rd 4th 5th
Errors
EventCatcher 2nd
ExceptionTest Case 2nd
Expectation class
ExpectationCollection class
ExpectationCounter class
ExpectationDoubleValue class
ExpectationList class
ExpectationMap class
ExpectationSegment class
ExpectationSet class
ExpectationValue class 2nd
Explaining variable, introducing
ExtensibleTestCase
Extract Class 2nd
refactoring 2nd
Extract Interface
Extract interface
Extract Interface
Extract interface 2nd
Extract Interface 2nd
Extract Method refactoring 2nd 3rd
eXtreme Programming
and Java project
associated tests
eXtreme Programming Explained (Beck)

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Factory method, replacing constructor with 2nd
Failures
Fakes
Fast view
Feathers, Mike
filter on category
Filter on category
filter on category
Filter on category 2nd
filter on category 2nd
Filter on category
adding a category selector to the GUI
filter on category
adding a category selector to the GUI
getting a sublist based on category
Filter on category
getting a sublist based on category
filter on category
getting a sublist based on category
Filter on category
getting a sublist based on category
filter on category
getting a sublist based on category
Filter on category
getting a sublist based on category
filter on category
supporting an ALL category
Filter on category
supporting an ALL category 2nd
filter on category
supporting an ALL category
filterOnCategory()
findComponent(), JFCTestHelper
findNamedComponent(), JFCTestHelper
Fine-grained interfaces, using liberally
Form Template method 2nd 3rd
Framework
RubyUnit
Freese, Tammo

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Gamma, Erich
Gargoyle Software JUnit extensions 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th
BaseTestCase 2nd
detailed exceptions 2nd 3rd
EqualsTester 2nd 3rd 4th 5th
EventCatcher 2nd
OrderedTestSuite 2nd
RecursiveTestSuite 2nd
getAverageRating() 2nd 3rd
getCategory() 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21st
getCategoryField()
getDescription()
getMessageFromJDialog(), JFCTestHelper
getName() 2nd
getNameField()
getNewName() 2nd 3rd 4th 5th 6th
getRating()
getRating(int)
getRatingField()
getRatingReviewField()
getRawRating() 2nd
getResultDocument()
getResultString()
getShowingDialogs(), JFCTestHelper
getShowingJFileChooser(), JFCTestHelper
getShowingJFileChoosers(), JFCTestHelper
getStringColumn(tableName, columnName,lwhereClause)
getTestResource()
getWindow(), JFCTestHelper
getWindows(), JFCTestHelper
Graphical User Interface (GUI) 2nd
Guard clauses, replacing nested conditional with 2nd
GUI test-first
brute force 2nd 3rd 4th 5th 6th
defined
developing 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21st 22nd 23rd 24th 25th 26th
example
tackling 2nd 3rd 4th 5th 6th
Jemmy 2nd 3rd 4th 5th
example 2nd 3rd

how it works 2nd 3rd


JButtonOperator
JFCUnit 2nd 3rd 4th 5th 6th 7th 8th
API 2nd 3rd 4th 5th 6th 7th 8th
junit.extensions.jfcunit.JFCTestCase
junit.extensions.jfcunit.JFCTestHelper,
ultra-thin GUI 2nd 3rd 4th 5th 6th 7th 8th

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Haggar, Peter
hashCode()
HashSet
hasRating() 2nd
HTMLDocumentBuilder class
Hunt, Andrew

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
IDEA 2nd 3rd
Change Signature feature
defined
features
integration with JUnit
refactorings
IDEs, refactoring support
IgnoreTextAndAttributeValuesDifferenceListener class
IllegalArgumentException 2nd 3rd
IMovieList interface 2nd
Inheritance, replacing with delegation 2nd 3rd
Introduce Explaining Variable refactoring
isEmpty()

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Java project 2nd 3rd 4th
Java, refactoring tools
javax.xml.transform.Transformer instance
JBuilder
JButtonOperator
Jeffries, Ron
Jemmy 2nd 3rd 4th 5th
example 2nd 3rd
how it works 2nd 3rd
JButtonOperator
Jester 2nd 3rd 4th 5th 6th 7th 8th
console output 2nd 3rd
defined
index for movie example
python scripts
quality testing results
shortcoming
standard changes 2nd 3rd 4th 5th 6th
Web site
JFactor
JFCTestHelper 2nd 3rd
JFCUnit 2nd 3rd 4th 5th 6th 7th 8th
API 2nd 3rd 4th 5th 6th 7th 8th
GUI test-first 2nd 3rd 4th 5th 6th 7th 8th
API 2nd 3rd 4th 5th 6th 7th 8th
junit.extensions.jfcunit.JFCTestCase
junit.extensions.jfcunit.JFCTestHelper
JUnit 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th
architectural overview 2nd
Assert superclass 2nd
assertEquals() 2nd
assertFalse()
assertNotNull()
assertNotSame() 2nd
assertNull()
assertSame() 2nd
assertTrue()
fail()
piecing it all together 2nd 3rd 4th 5th
placing tests/test code
plugin for Eclipse 2nd 3rd
running tests 2nd 3rd 4th

as an ANT task 2nd


errors/failures
setUp() 2nd 3rd
Swing GUI
tearDown() 2nd 3rd 4th
Test interface
TestCase class 2nd 3rd 4th 5th 6th 7th 8th
TestListener interface
TestResult class
TestRunner classes 2nd 3rd
TestSuite class 2nd 3rd
text UI
tips 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
assert, starting with
assertEquals()
fine-grained interfaces, using liberally
keeping test methods small 2nd 3rd
keeping tests independent of each other
main(), adding to test cases
message argument
System.out and System.errr, avoiding in tests
test simple stuff first
testing against databases/network resources, avoiding
testing boundary conditions early
toString(), writing
JUnit extensions 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th
20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st 32nd 33rd 34th 35th 36th 37th 38th 39th
40th
assertEquals for Object()
assertExcludes
assertIncludes
assertStartsWith
DaeDalos JUnit extensions 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th
DatabaseChecker 2nd 3rd 4th
defined
Extensible TestCase
programmer test implementation using test resources 2nd 3rd 4th 5th
test resources, managing 2nd
TRTestRunner
Gargoyle Software JUnit extensions 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th
BaseTestCase 2nd
detailed exceptions 2nd 3rd
EqualsTester 2nd 3rd 4th 5th
EventCatcher 2nd
OrderedTestSuite 2nd
RecursiveTestSuite 2nd
JUnitPerf 2nd 3rd 4th 5th 6th
applications

confident tuning
defined
LoadTest 2nd 3rd
new customer stories
scaling up
TimedTest
MockObjects
standard extensions 2nd 3rd 4th 5th 6th
ActiveTestSuite
ExceptionTest Case 2nd
Repeated Test 2nd
TestDecorator 2nd
TestSetup 2nd 3rd
XMLUnit 2nd 3rd 4th 5th 6th 7th 8th 9th
configuring
defined
glossary
quick tour of 2nd
validation tests
walking the DOM tree 2nd
XML comparison tests, writing 2nd 3rd
XML transformations, comparing
Xpath tests 2nd 3rd
JUnit plugin, for Eclipse 2nd 3rd
JUnit Test, running a class as
JUnit-related tools 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th
Clover 2nd
Ant integration
class level report 2nd
defined
package-level report 2nd
project level report 2nd
user guide
Eclipse 2nd
JUnit integration
problem indication
refactoring support
IDEA 2nd
Change Signature feature
defined
features
integration with JUnit
refactorings
Jester 2nd 3rd 4th 5th 6th 7th 8th
console output 2nd 3rd
defined
python scripts

shortcoming
standard changes 2nd 3rd 4th 5th 6th
Web site
NoUnit 2nd 3rd
analysis by 2nd
defined
report 2nd
result of running
junit.runner.BaseTestRunner
junit.swingui.TestRunner 2nd
JUnitPerf 2nd 3rd 4th 5th 6th
applications
confident tuning
defined
LoadTest 2nd 3rd
new customer stories
scaling up
TimedTest
junitreport task 2nd

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Kerievsky, Joshua

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Lack of cohesion of methods 2 (LOCOM2) 2nd
Large classes
and code smells
finding
Lazy classes, and code smells
LoadTest
using for scalability testing 2nd 3rd
Long method, code smells

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Magic number, replacing with symbolic constants 2nd
main(), adding to test cases
Making a humble dialog, use of term
Message argument
Mock objects 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21st 22nd 23rd
capabilities of
defined 2nd
EasyMock 2nd 3rd

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Nested conditionals, replacing with guard clauses 2nd
NodeTester class 2nd
notImplemented()
NoUnit 2nd 3rd
analysis by 2nd
defined
report 2nd
report for movie example
result of running
NullPointerException 2nd 3rd 4th
Number of operations (NOO) 2nd
numberOfMovies 2nd

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
oneItemList instance
OrderedTestSuite 2nd

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Package-level report, Clover 2nd
Patterns, code smells to
Patterns, refactoring to
Pentecost, Kay
Persistence
persistence
Persistence
persistence
Persistence
persistence
Persistence
"Save As" functionality in GUI
persistence
"Save As" functionality in GUI 2nd
Persistence
"Save As" functionality in GUI
persistence
"Save As" functionality in GUI
Persistence
"Save As" functionality in GUI 2nd
persistence
"Save As" functionality in GUI 2nd
Persistence
"Save As" functionality in GUI
persistence
"Save As" functionality in GUI
Persistence
"Save As" functionality in GUI
persistence
"Save As" functionality in GUI
Persistence
"Save As" functionality in GUI 2nd
persistence
"Save As" functionality in GUI 2nd
Persistence
"Save As" functionality in GUI
persistence
"Save As" functionality in GUI
Persistence
"Save As" functionality in GUI
persistence
"Save" functionality in GUI

Persistence
"Save" functionality in GUI
persistence
"Save" functionality in GUI
Persistence
"Save" functionality in GUI
persistence
"Save" functionality in GUI
Persistence
"Save" functionality in GUI 2nd
persistence
"Save" functionality in GUI
Persistence
"Save" functionality in GUI
persistence
"Save" functionality in GUI
Persistence
"Save" functionality in GUI
persistence
"Save" functionality in GUI
Persistence
loading in GUI
persistence
loading in GUI
Persistence
loading in GUI
persistence
loading in GUI 2nd
Persistence
loading in GUI
persistence
loading in GUI
Persistence
loading in GUI 2nd
persistence
loading in GUI
Persistence
reading from a flat file
persistence
reading from a flat file
Persistence
reading from a flat file 2nd
persistence
reading from a flat file 2nd
Persistence
reading from a flat file
persistence
reading from a flat file

Persistence
reading from a flat file
persistence
reading from a flat file
Persistence
reading from a flat file
persistence
reading from a flat file
Persistence
writing movie collection to a flat file
persistence
writing movie collection to a flat file
Persistence
writing movie collection to a flat file
persistence
writing movie collection to a flat file
Persistence
writing movie collection to a flat file
persistence
writing movie collection to a flat file
Pipka, Jens Uwe
Polymorphism, replacing conditional with
postProcessTest()
Practical Java (Haggar)
Programmer Tests, maintaining a suite of
Project retrospective 2nd 3rd
design 2nd 3rd
lack of cohesion of methods 2 (LOCOM2) 2nd
number of operations (NOO) 2nd
test vs. application 2nd
testing quality 2nd 3rd 4th
weighted methods per class 2 (WMPC2)
Project stakeholders, and test-driven development
Project-level report, Clover 2nd
Python scripts
PYUnit 2nd 3rd 4th 5th
example 2nd 3rd 4th 5th

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Quality
Clover results
comments on results
debugging 2nd
general notes 2nd
Jester results
NoUnit results 2nd
RatingRenderer
testing for 2nd 3rd 4th
tests, list of 2nd 3rd 4th 5th 6th 7th 8th
XMLMovieListWriter 2nd

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Rating.equals()
RatingRenderer 2nd
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings 2nd
Ratings 2nd
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
ratings
Ratings
editing 2nd 3rd 4th 5th 6th
editing the rating
ratings
editing the rating 2nd
Ratings
editing the rating 2nd
ratings
editing the rating
Ratings
editing the rating
ratings
editing the rating
Ratings
editing the rating
ratings
editing the rating

Ratings
editing the rating
ratings
editing the rating
Ratings
showing in GUI 2nd 3rd 4th 5th 6th
showing rating in GUI
ratings
showing rating in GUI
Ratings
showing rating in GUI
ratings
showing rating in GUI
Ratings
showing rating in GUI
ratings
showing rating in GUI
Ratings
showing rating in GUI
ratings
showing rating in GUI
Ratings
showing rating in GUI
ratings
showing rating in GUI
Ratings
showing rating in GUI
ratings
showing rating in GUI
Ratings
single rating, adding
ratings
single rating, adding
Ratings
single rating, adding
ratings
single rating, adding
Ratings
single rating, adding
ratings
single rating, adding
RecursiveTestSuite 2nd
Refactoring 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th
21st 22nd 23rd 24th 25th 26th 27th 28th
and Eclipse
and IDEA
and Test-Driven Development
code smells 2nd 3rd 4th 5th 6th 7th 8th
defined
defined
duplication 2nd 3rd
example of

redundantly maintained code found at run time


removing 2nd
unclear intent
IDEA
need for 2nd
procedure 2nd
situations requiring 2nd
test code
Refactoring hat
RefactorIt
RegisterTestResource
Repeated Test 2nd
Replace Conditional with Polymorphism refactoring
Replace Constructor with Factory Method refactoring 2nd
Replace Inheritance with Delegation refactoring 2nd 3rd
Replace Magic Number with Symbolic Constant refactoring 2nd
Replace Nested Conditional with Guard Clauses refactoring 2nd
Replace Type Code with Subclasses refactoring
resetSleepTime(), JFCTestCase
retrospective 2nd 3rd
design 2nd 3rd
lack of cohesion of methods 2 (LOCOM2) 2nd
number of operations (NOO) 2nd
test vs. application 2nd
testing quality 2nd 3rd 4th
weighted methods per class 2 (WMPC2)
ReturnObjectList
ReturnObjectList class
Reviews
reviews
Reviews
reviews 2nd
Reviews 2nd
reviews 2nd
Reviews
reviews
Reviews
reviews
Reviews
reviews
Reviews
reviews
Reviews 2nd
reviews
Reviews
reviews
Reviews
reviews
Reviews
reviews
Reviews
reviews

Reviews
reviews 2nd
Reviews 2nd
reviews
Reviews
adding
reviews
adding 2nd
Reviews
adding
adding to ratings
reviews
adding to ratings
Reviews
adding to ratings
reviews
adding to ratings
Reviews
adding to ratings
reviews
adding to ratings
displaying
Reviews
displaying
reviews
displaying
Reviews
displaying
reviews
displaying
Reviews
displaying
reviews
displaying
Reviews
displaying 2nd
reviews
displaying
Reviews
displaying
reviews
displaying
Reviews
displaying
reviews
displaying
Reviews
displaying
reviews
displaying
Reviews
displaying

reviews
displaying
Reviews
displaying
reviews
displaying
Reviews
loading
reviews
loading 2nd
Reviews
loading
reviews
saving
Reviews
saving 2nd
reviews
saving
rowCount(tableName)
rowCount(tableName, whereClause)
rowExists(tableName, where-Clause) 2nd
RubyUnit 2nd 3rd 4th 5th 6th
example 2nd 3rd 4th 5th
framework
Running tests
JUnit 2nd 3rd 4th
as an ANT task 2nd
errors/failures
runTestSilently()
Runtime structure of tests, sample of

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S] [ T] [ U ] [ V ]
[ W] [ X ]
sendKeyAction(), JFCTestHelper
sendString(), JFCTestHelper
setCategoryField()
setDefaultVoidCallable()
setExpected()
setExpectedResult()
setMovieNames() 2nd 3rd 4th
setMovies() 2nd 3rd
setNewName() 2nd 3rd
setNewRating()
setRating() 2nd 3rd 4th
setRatingField()
setReturnValue()
setSleepTime()
setSleepTime(long time), JFCTestCase
setUp() 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
updating 2nd
SetupExample 2nd
setVoidCallable()
Shotgun Surgery 2nd
Simplicity
testing simple stuff first
Single rating, adding 2nd 3rd
Single-rating field, removing
size()
sleep(long delay), JFCTestCase
Smalltalk, and refactoring
sorting
Sorting 2nd
sorting 2nd
Sorting 2nd
sorting
Sorting
sorting 2nd
Sorting
sorting
Sorting
sorting
Sorting 2nd
sorting
Sorting
sorting 2nd

Sorting 2nd
sorting 2nd
Sorting 2nd
sorting
Sorting
sorting
Sorting 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st
22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st 32nd 33rd 34th 35th 36th 37th 38th 39th 40th 41st
adding a rating in the GUI 2nd 3rd 4th 5th 6th
adding support for 2nd 3rd 4th 5th 6th 7th 8th
sorting
adding way to sort to GUI
Sorting
adding way to sort to GUI 2nd
sorting
adding way to sort to GUI 2nd
Sorting
adding way to sort to GUI 2nd
sorting
adding way to sort to GUI
Sorting
asking MovieListEditor for sorted lists
sorting
asking MovieListEditor for sorted lists 2nd
Sorting
asking MovieListEditor for sorted lists
sorting
comparing movies
Sorting
comparing movies 2nd
sorting
comparing movies 2nd
Sorting
comparing movies 2nd
sorting
comparing movies
Sorting
comparing movies
sorting
comparing movies
Sorting
of a MovieList
sorting
of a MovieList
Sorting
of a MovieList
sorting
of a MovieList 2nd

Sorting
of a MovieList
sorting
of a MovieList
Sorting
of a MovieList
sorting
of a MovieList
Sorting
of a MovieList 2nd
sorting
of a MovieList
Sorting
rating source 2nd 3rd 4th 5th 6th 7th 8th 9th
removing the single-rating field
revised persistence 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th
showing in GUI 2nd 3rd 4th 5th 6th
sortUsing() 2nd
Source code, code smells in
Standard extensions 2nd 3rd 4th 5th 6th
ActiveTestSuite
ExceptionTest Case 2nd
Repeated Test 2nd
TestDecorator 2nd
TestSetup 2nd 3rd
start()
stop()
Stubs
Subclasses, replacing type code with
SUnit 2nd 3rd 4th 5th 6th
block assertions
boolean assertions
example 2nd 3rd 4th 5th
failure
framework
Support movie name editing
support movie name editing
Support movie name editing
support movie name editing
Support movie name editing
support movie name editing
Support movie name editing
support movie name editing
Support movie name editing
support movie name editing
Support movie name editing
constructing a Movie with a null name

support movie name editing


constructing a Movie with a null name
Support movie name editing
constructing a Movie with an empty name
support movie name editing
constructing a Movie with an empty name
Support movie name editing
constructing a Movie with an empty name
support movie name editing
constructing a Movie with an empty name
Support movie name editing
movie name change
support movie name editing
movie name change
Support movie name editing
movie name change
support movie name editing
movie name change
Support movie name editing
renaming a movie to a null name
support movie name editing
renaming a movie to a null name
Support movie name editing
renaming a movie to a null name
support movie name editing
renaming a movie to a null name
Support movie name editing
renaming a movie to an empty name
support movie name editing
renaming a movie to an empty name
Support movie name editing
renaming a movie to an empty name
support movie name editing
renaming a movie to an empty name
Swing GUI
SwingMovieListEditorView 2nd 3rd
Switch statements, and code smells 2nd
Symbolic constant, replacing magic number with 2nd
System.out and System.errr, avoiding in tests

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T ] [ U ] [ V ]
[ W] [ X ]
TDD Traffic Light, The (Wake)
tearDown() 2nd 3rd 4th 5th
Test code
refactoring
Test interface
Test methods, keeping small 2nd 3rd
Test Resource Browser
Test vs. application 2nd
Test-Driven Development 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th
and Agile Modeling (AM)
and code 2nd 3rd
and code compilation 2nd
and programmers 2nd
compared to traditional testing approaches
defined 2nd 3rd
Movie Class example 2nd 3rd 4th
Programmer Tests, maintaining suite of
tests
writing the tests
Testability
testAdding() 2nd 3rd 4th
testAddingOneRating()
testAddingRating() 2nd 3rd
testAddingRatings() 2nd
testAddingThreeOtherRatings()
testAddingThreeRatings()
testAllSubset()
testAnonymousRating()
testBadCategory()
testCalegoryFiltering()
testCancelledLoading() 2nd
testCancelledSaving()
TestCase class 2nd 3rd 4th 5th 6th 7th 8th
TestCaseMo
testCompareAfter() 2nd
testCompareBefore() 2nd
testCompareSame() 2nd
testComparingWithUnrated()
testContents() 2nd 3rd
testCopyConstructor()
TestCustomListRenderer
TestDecorator 2nd

testDuplicateCausingAdd() 2nd 3rd


testDuplicateCausingUpdate() 2nd
testEmptyListSize() 2nd 3rd 4th
testEmptyName()
Tester's block technique
Testing 2nd
Testing quality
testing quality
Testing quality
testing quality
Testing quality
testing quality
Testing quality
testing quality
Testing quality
Clover results
testing quality
Clover results
Testing quality
comments on results
testing quality
comments on results
debugging
Testing quality
debugging 2nd
testing quality
debugging
Testing quality
general notes
testing quality
general notes 2nd
Testing quality
general notes
Jester results
testing quality
Jester results
Testing quality
NoUnit results
testing quality
NoUnit results
Testing quality
NoUnit results
testing quality
NoUnit results
Testing quality
RatingRenderer
testing quality
RatingRenderer

Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
tests, list of
testing quality
tests, list of
Testing quality
XMLMovieListWriter
testing quality
XMLMovieListWriter
Testing quality
XMLMovieListWriter
testing quality
XMLMovieListWriter
testList()
TestListener interface
testLoading() 2nd
TestMovie.testNullName()
TestMovieComparingByRating
TestMovieList.testEmptyListSize()
TestMovieListEditor 2nd 3rd

TestMovieListEditor.testUpdating() 2nd
TestMovieListEditorFileOperations 2nd 3rd
TestMovieListEditorFileOperations.testSaving()
TestMovieListEditorLoading
TestMovieListEditorSaving
TestMovieListEditorSorting
TestMovieListReading 2nd 3rd 4th 5th 6th 7th 8th
TestMovieListWithEmptyList
TestMovieListWithPopulatedList
testMovieName() 2nd
TestMovieRatings
testNoReview()
testNullName()
testNullRename()
testOneItemListIsEmpty()
testOneItemListSize()
TestOperation class
testRatedMovie() 2nd 3rd
TestRating
TestRatingRenderer
testRatingSelectionWithoutReview() 2nd 3rd
testRatingSelectionWithReview() 2nd
testRatingWithSource()
testReadingEmptyList()
testReadingMultipleMovie() 2nd
testReadingOneMovie() 2nd 3rd 4th
testRenaming() 2nd
testRenamingDuplicate()
TestResource
TestResourceFactory 2nd
TestResourceFactory.reset()
TestResourceTestCase
TestResult class
testReturnsSelf() 2nd
TestRunner classes 2nd 3rd 4th 5th
tests
Tests
keeping independent of each other
writing

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U] [ V ]
[ W] [ X ]
UML tools 2nd
uniqueness of movies
Uniqueness of movies 2nd
uniqueness of movies 2nd
Uniqueness of movies
uniqueness of movies
Uniqueness of movies 2nd
uniqueness of movies
Uniqueness of movies
uniqueness of movies 2nd
Uniqueness of movies 2nd
uniqueness of movies
Uniqueness of movies
uniqueness of movies
attempting to add a duplicate movie
Uniqueness of movies
attempting to add a duplicate movie 2nd
uniqueness of movies
attempting to add a duplicate movie
movie renaming
Uniqueness of movies
movie renaming 2nd
uniqueness of movies
movie renaming 2nd
Uniqueness of movies
movie renaming
uniqueness of movies
movie renaming
Uniqueness of movies
movie renaming
uniqueness of movies
non-uniqueness error message
Uniqueness of movies
non-uniqueness error message 2nd
uniqueness of movies
non-uniqueness error message
Uniqueness of movies
non-uniqueness error message
uniqueness of movies
non-uniqueness error message 2nd
Uniqueness of movies
non-uniqueness error message 2nd
uniqueness of movies
non-uniqueness error message
Uniqueness of movies
test for bad add
uniqueness of movies

test for bad add


Uniqueness of movies
test for bad update
uniqueness of movies
test for bad update
Uniqueness of movies
test for bad update
uniqueness of movies
test for bad update
Uniqueness of movies
test for bad update
uniqueness of movies
test for bad update
test for duplicate addition
Uniqueness of movies
test for duplicate addition 2nd
uniqueness of movies
test for duplicate addition
Uniqueness of movies
test for duplicate addition
uniqueness of movies
test for duplicate addition
Uniqueness of movies
test for updating that causes duplicate
uniqueness of movies
test for updating that causes duplicate 2nd
Uniqueness of movies
test for updating that causes duplicate 2nd
uniqueness of movies
test for updating that causes duplicate
Uniqueness of movies
tests
uniqueness of movies
tests
UnratedException
updateMovie()
updateMovieList()
updateNamesInView()
user stories/tasks
User stories/tasks 2nd
user stories/tasks
User stories/tasks
user stories/tasks

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Validator class
Van Deursen, Arie
VBUnit 2nd 3rd 4th 5th 6th 7th 8th 9th
example 2nd 3rd 4th 5th 6th 7th 8th 9th
framework
getting started with 2nd
MovieList functionality, creating 2nd 3rd 4th 5th 6th 7th 8th
Verifiable class
verify()
Vision statement
vision statement

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[W] [X]
Wake, William
Weighted methods per class 2 (WMPC2)
writeRating()

Amazon

Prev

don't be afraid of buying books

Next

[ SYMBOL] [ A ] [ B ] [ C ] [ D ] [ E] [ F ] [ G ] [ H ] [ I ] [ J ] [ K ] [ L ] [ M ] [ N ] [ O ] [ P ] [ Q ] [ R ] [ S ] [ T] [ U ] [ V ]
[ W] [ X ]
Xalan
XmlCalculator 2nd 3rd 4th
XMLMovieListReader:
XMLMovieListWriter 2nd
XMLTestCase class 2nd 3rd
XMLUnit 2nd 3rd 4th 5th 6th 7th 8th 9th
configuring
defined
glossary
quick tour of 2nd
validation tests
walking the DOM tree 2nd
XML comparison tests, writing 2nd 3rd
XML transformations, comparing
Xpath tests 2nd 3rd

Amazon

Vous aimerez peut-être aussi