Tải bản đầy đủ
D.2 The basics: compiling all source code

D.2 The basics: compiling all source code

Tải bản đầy đủ



Running tests with Ant


Target for compiling
all source code

debug="on" classpathref="classpath.main" />

debug="on" classpathref="classpath.test" />

class paths for
and test code

This is familiar to those who have seen Ant build scripts. The script in listing D.2
provides the developer with two targets to execute: B clean and C compile. With
Ant installed, typing ant clean on the command prompt deletes all compiled
class files. Typing ant compile compiles all .java source files into the bin directory.
Typing ant clean compile first deletes existing class files and then compiles everything from scratch. And, because the project’s default target is configured as compile, just typing ant is the same as typing ant compile.
But what about running those tests we’re now compiling?


Adding a target for running tests
JUnit is integrated into Ant through the task. In order to make running
our unit tests part of our build, we need to add a new target to build.xml, which
uses the task. Listing D.3 shows an example of what this new target
might look like.
Listing D.3 build.xml with a test target using Ant’s standard task

Use test target as
default from now on


Use property for
output path to
reduce duplication


Need everything

in class path

including compiled

test code


Running tests with Ant

includes="**/Test*.java" />



Use property for output
path to reduce duplication


Collect all classes that
match pattern for execution

Without going to too much detail, the new target in listing D.3 uses the
task to C collect all unit tests from src/test for execution and generates a bunch
of XML files under B the configured reports/junit/data directory. These XML
files describe the test execution environment, which tests were run, and with what
kind of results. Those XML files aren’t user-friendly, though. We need a pretty
report to look at.


Generating a human-readable report
We can generate a nice, compact HTML report from the output of the
task by using another built-in task, . All it needs to know is where
to find the test results, where we want the report to be generated, and in what format—there are a couple of options.
Listing D.4 presents the necessary modifications to our existing build script for
both running the tests as well as for generating the HTML report.
Listing D.4

build.xml with a target for running tests and generating the report


"test" target
coordinates test
and reporting



Running tests with Ant

“test” target

coordinates test

execution and

report generation

generates aggregated XML in data directory…
includes="TEST-*.xml" />

…and produces HTML report in frames

format under reports/junit/html

That’s it. We now have an Ant build script for compiling our code, running our
unit tests, and producing a pretty report of the results!

Works cited
Alur, Deepak, Dan Malks, and John Crupi. Core J2EE Patterns: Best Practices and Design Strategies.
Prentice-Hall, 2003.
Ambler, Scott W. and Pramodkumar J. Sadalage. Refactoring Databases: Evolutionary Database
Design. Addison-Wesley, 2006.
Cohn, Mike. User Stories Applied: For Agile Software Development. Addison-Wesley, 2004.
Cooper, Alan. The Inmates Are Running the Asylum: Why High Tech Products Drive Us Crazy and How
to Restore the Sanity. Sams, 1999.
Crane, Dave, Bear Bibeault, Jord Sonneveld, Ted Goddard, Chris Gray, Ram Venkataraman, and
Joe Walker. Ajax in Practice. Manning Publications, 2007.
Crane, Dave, Eric Pascarello, and Darren James. Ajax in Action. Manning Publications, 2005.
Cunningham, Ward and Rick Mugridge. Fit for Developing Software: Framework for Integrated Tests.
Addison-Wesley, 2005.
DeMarco, Tom. Slack: Getting Past Burnout, Busywork, and the Myth of Total Efficiency. Broadway,
DeMarco, Tom and Timothy Lister. Peopleware: Productive Projects and Teams. Dorset House Publishing Company, 1999.
Feathers, Michael. Working Effectively with Legacy Code. Addison-Wesley, 2004.
Fowler, Martin, Kent Beck, John Brant, and William Opdyke. Refactoring: Improving the Design of
Existing Code. Addison-Wesley, 1999.
Gamma, Erich, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of
Reusable Object-Oriented Software. Addison-Wesley, 1995.
Hillenius, Eelco and Martijn Dashorst. Wicket in Action. Manning Publications, 2007.
Kawasaki, Guy. Rules for Revolutionaries: The Capitalist Manifesto for Creating and Marketing New
Products and Services. Collins, 2000.
Kerievsky, Joshua. Refactoring to Patterns. Addison-Wesley, 2004.
Kuchana, Partha. Software Architecture Design Patterns in Java. Auerbach, 2004.




Larman, Craig. Agile & Iterative Development: A Manager’s Guide. Addison-Wesley, 2003.
Loughran, Steve and Erik Hatcher. Ant in Action: Java Development with Ant, Second Edition. Manning
Publications, 2007.
Mann, Kito. JavaServer Faces in Action. Manning Publications, 2005.
Manns, Mary Lynn and Linda Rising. Fearless Change: Patterns for Introducing New Ideas. AddisonWesley, 2004.
Rainsberger, J. B. JUnit Recipes: Practical Methods for Programmer Testing. Manning Publications, 2005.
Ship, Howard M. Lewis. Tapestry in Action. Manning Publications, 2004.

Additional resources
Ambler, Scott W. Agile Database Techniques: Effective Strategies for the Agile Software Developer. Wiley,
Beck, Kent. Test Driven Development: By Example. Addison-Wesley, 2002.
Cohn, Mike. Agile Estimating and Planning. Prentice Hall PTR, 2005.
Duvall, Paul, Steve Matyas, and Andrew Glover. Continuous Integration: Improving Software Quality and
Reducing Risk. Addison-Wesley, 2007.
Goetz, Brian, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. Java Concurrency in Practice. Addison-Wesley, 2006.
Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code. Addison-Wesley, 2007.
Newkirk, James W. and Alexei A. Vorontsov. Test-Driven Development in Microsoft .NET. Microsoft
Press, 2004.
Schwaber, Ken and Mike Beedle. Agile Software Development with SCRUM. Prentice Hall, 2001.

Online resources
All URLs listed here were valid at the time of publishing. No doubt some of these will
change over time.
Agical AB. RMock: Java Mock Object Framework to Use with JUnit. http://rmock.sf.net.
Agile Finland Community. Agile Finland Community Wiki. http://wiki.agilefinland.com.
Apache Community. Apache Ant Community Wiki. http://wiki.apache.org/ant.
———. Apache Ant: A Java-Based Build Tool. http://ant.apache.org.
———. Apache Ant Manual. http://ant.apache.org/manual.
———. Apache Maven: A Java-Based Build Tool. http://maven.apache.org.
———. Continuum: A Continuous Integration Server. http://maven.apache.org/continuum.
———. Jakarta Commons Lang: A Utility Library for the java.util API. http://jakarta.apache.org/



———. Jakarta Commons VFS: A Virtual File System API for Java. http://jakarta.apache.org/
———. Wicket: A Component-Based Web Development Framework for Java. http://wicket.sf.net.
Ashley, Nick, Graham Tackley, and Sam Newman. dbdeploy: A Database Change Management
Tool. http://dbdeploy.com/.
Atlassian Software Systems Pty Ltd. Bamboo: A Continuous Integration Server. http://www.
Atomic Object LLC. System Testing in Ruby. http://atomicobject.com/pages/System+
Bossicard, Vladimir R. PrivateAccessor: A Utility Class for Overriding Access Modifiers in Java.
Bowler, Mike. GSBase: A JUnit Extension of Test Utilities. http://gsbase.sf.net.
Cenqua. Clover: Code Coverage Analysis Tool for Java. http://www.cenqua.com/clover.
Cunningham, Ward. Fit: Framework for Integrated Test. http://fit.c2.com.
Diehl and Associates. Mckoi Database: A Lightweight, Java-Based Relational Database. http://
Feathers, Michael. “The ‘Self’-Shunt Unit Testing Pattern.” January 2001. http://www.
———. “The Humble Dialog Box.” August 2002. http://www.objectmentor.com/resources/
———. “Working Effectively With Legacy Code.” February 2002. http://www.objectmentor.com/
Fowler, Amy. “A Swing Architecture Overview.” http://java.sun.com/products/jfc/tsc/articles/
Fowler, Martin. “Continuous Integration.” May 1, 2006. http://www.martinfowler.com/articles/
———. “Mocks Aren’t Stubs.” January 2, 2007. http://www.martinfowler.com/articles/
———. Passive View. July 18, 2006. http://www.martinfowler.com/eaaDev/PassiveScreen.html.
———. Supervising Controller. June 19, 2006. http://www.martinfowler.com/eaaDev/
Freeman, Steve, Tim Mackinnon, Nat Pryce, Mauro Talevi, and Joe Walnes. JMock: A Dynamic
Mock Object Library for Java. http://www.jmock.org.
hsql Development Group. HSQLDB: A Lightweight, Java-Based Relational Database. http://



Hubbard, Charlie and Prashant Dhokte. PrivilegedAccessor: A Utility Class for Overriding Access
Modifiers in Java. http://groups.yahoo.com/group/junit/files/src/PrivilegedAccessor.java
(requires a Yahoo! account).
Jeffries, Ron. “Essential XP: Card, Conversation, Confirmation.” August 30, 2001. http://
———. List of testing-related software. http://www.xprogramming.com/software.htm.
Koskela, Lasse. Bean Inject: Dependency Injection Framework for Java. http://www.
———. Introduction to Code Coverage. January 2004. http://www.javaranch.com/newsletter/
———. JspTest: A JUnit Extension for Unit Testing JavaServer Pages. http://sourceforge.net/
Martin, Robert C. “Design Principles and Design Patterns.” January 2000. http://www.
———. “SRP: The Single Responsibility Principle.” February 2002. http://www.
Meszaros, Gerard. xUnit Test Patterns. http://www.xunitpatterns.com.
Mort Bay Consulting. Jetty: An Embeddable Java EE Web Container. http://jetty.mortbay.com.
Mugridge, Rick. FitLibrary: An Extension Package for Fit and Fitnesse. http://sourceforge.net/
NetBeans Community. Jellytools Module: Jemmy Extension for Creating UI Tests for NetBeans
IDE. http://jellytools.netbeans.org.
———. Jemmy: UI Testing Library for Java. http://jemmy.netbeans.org.
Payne, Bob. Agile Toolkit Podcast. Interview with Arlo Belshee at Agile 2005: “Promiscuous Pairing
and the Least Qualified Implementer.” http://media.libsyn.com/media/agiletoolkit/
Perrotta, Paolo. The Virtual Clock Test Pattern. March 1, 2003. http://www.nusco.org/docs/
Potel, Mike. MVP: Model-View-Presenter, The Taligent Programming Model for C++ and Java.
1996. http://www.arsip.or.id/free_doc/m/mvp/mvp.pdf.
Pragmatic Programmers, LLC. “Tell, Don’t Ask.” http://www.pragmaticprogrammer.com/ppllc/
Rothman, Johanna. “What Does It Cost You To Fix A Defect? And Why Should You Care?” 2000.
Ruby on Rails Community wiki. Understanding Migrations. August 1, 2007. http://wiki.



Saff, David, Erich Gamma, Kent Beck, and Erik Meade. JUnit: A Simple Framework for Writing and
Running Automated Tests. http://www.junit.org.
Schuh, Peter and Stephanie Punke. “ObjectMother: Easing Test Object Creation in XP.” 2001.
SourceForge.net. Cobertura: Code Coverage Analysis Tool for Java. http://cobertura.sf.net.
———. CruiseControl: A continuous integration server. http://cruisecontrol.sf.net.
———. DbUnit: A JUnit Extension for Testing Database Code. December 28, 2006. http://
———. EMMA: Code Coverage Analysis Tool for Java. http://emma.sf.net.
———. Joda-Time: A Time and Date API to Replace the Standard java.util Classes. http://
———. PMD: Static Analysis Tool for Java. http://pmd.sf.net.
———. SUnit: The Mother of All Unit Testing Frameworks. http://sunit.sf.net.
Splint Community. Splint: Static Analysis Tool for C. http://splint.org.
Tammo Freese. EasyMock: A Dynamic Mock Object Library for Java. http://www.easymock.org.
Urban{code}, Inc. AnthillPro: A Continuous Integration Server. http://www.urbancode.com.
World Wide Web Consortium. Logging Control in W3C httpd. July 1995. http://www.w3.org/
———. XML Path Language (XPath). November 16, 1999. http://www.w3.org/TR/xpath.
Yahoo! Inc. JUnit Group and Online Mailing List. http://groups.yahoo.com/group/junit.

Abbot 295, 297, 300, 418
ComponentTester utility 297
class 297
abstract base class 174, 181,
206, 237
for DbUnit-based integration
tests 239
Abstract Factory pattern 127
abstract user interface 356
abstraction 35, 212, 275
between MVC controller and
view 169
keeping uniform level of
level of 419
level, consistency in
methods 69–70
acceptance 450
exercising the internals 414
acceptance criteria 353
acceptance TDD See acceptance
test-driven development
acceptance test 4, 33, 35, 324,
335, 341, 404, 440
accelerating execution
adding detail 337
adding new 353
as communication tool 421
as indicator of completion 31
as shared language 33
as specification See tests as

automating 337, 340, 345, 391
automating through UI 417
avoiding turbulent
interfaces 399
brittleness 403
categorization 426
challenge of automating 417
common fixtures
between 431
complexity 425, 431
connecting with system under
test 401
crawling under the skin 404
customer-friendly format 359
dependency between
tests 429
design 403
detail in 332
difference between unit test
and internals-exercising
acceptance test 409
duplication 431
embedding into system under
test 419
end-to-end strategy 401
example of 327, 330
executable, syntax of 339, 341
expressing with concrete
examples 354
focus of 398
for FTP client 416
for programming library 413
for web application 431
generating data set for 433
getting access to
transaction 430


heavyweight 409
hooking into service layer 422
identifying 342
approaches 401–411
implementing 333, 337, 406
implementing against
programming API 416
implementing for complex
systems 425
introduction 327–333
list of 336
maintainability 429
mapping to user-accessible
functionality 403
merging with unit test 413
organizing into test suites 428
owned by customer 329
passing 31
performance 356
properties 328–333
reducing complexity 431
refactoring 404, 425
running against stubbed
system 410
running on simulator 358
side effects 411, 426
structuring 431
stubbing 357–358
subsets of 426
suite 403
targeted at system
internals 408
implications 411–425
timing 337



acceptance test (continued)
tips 425–433
tools for implementing 338
translating to executable
test 338–339
updating 399
validation of 331
what to test 397–401
when to write 345–346
writing 329, 337,
345, 350, 360
writing with jWebUnit 401
writing with wiki syntax 360
acceptance test-driven
development 4, 7, 10, 31,
98, 324, 327, 341, 401
as team activity 348–351
automating tests 337–341
benefits 351
benefits of 351–355
continuous planning 344–346
cooperative work 353
customer properties 349–350
cycle 334–343
decreasing load 346
defining customer role
definition of “done” 351–353
frameworks for 37
homegrown tools 362
functionality 341–343
inside an iteration 343–348
mid-iteration sanity test 346
number of testers 350
picking user story 335–336
process 324
process, understanding
promoting 446, 463
properties of 324
scripting language–based
frameworks 361
specification by example 354
splitting stories 347
table-based tools 359–361
text-based frameworks 361
tools 359–362
trust and commitment 354
what we’re testing 355–359

whether to stub parts of
system 357
whether to test against
UI 355–357
whether to test business
logic 358
who writes tests with
customer 350
with Fit 365–369
acceptance testing 37, 361
approach to 400
backdoor 411
crawling under the skin
412, 414–415, 419
end-to-end 406, 416–418,
421, 425
exercising the internals
407, 412, 414, 421
framework 37, 359, 365
approaches 401
strategy 412, 414, 416
strategy, choosing 411
strategy, cost 414
stubbing out the
irrelevant 409
considerations 411
tools 359
access point 212
accessor method 211, 291
accomplishment 438, 442
account manager 12
372, 379–382, 384
header row 379
ActionListener 304
actor 379
Adobe Photoshop 417
adoption 4, 10, 436
assigning roles 463–464
coaching and
facilitation 461–463
destabilization 464
evangelizing for change
facilitating 454–465
lowering bar to change
of Java 200
rewards, delayed 465

sharing information 459–461
training and education 458
Agile 2005 336
Agile Finland 451
agile methods 19, 454, 458
agile software development
7, 31
Agile Toolkit Podcast 336
key ingredient of 7
Ajax 423
alarm system 8
Alexander, Christopher
364, 396
algorithm 58, 77, 85, 101,
116, 223, 378
using to find connecting
line 313
verbosity 313
Alleman, Glen B. 323
alternative implementation 90,
111, 116, 120, 124, 140, 160
alternative implementation
See test double
Alur, Deepak 198
ambiguity 21, 33–34
Ambler, Scott 244
analytic thinking 452
annotation 50, 67, 186, 211
anonymous class 291
implementation 139, 316
anonymous inner class 141
Ant See Apache Ant
AntHill 39
antidote 7
anti-pattern 109
Apache Ant 365, 392
build script 391
build script, adding target for
running tests 478–479
build script, compiling source
code 477–478
build script, directory
structure of 476–477
build script, generating
report 479–480
build script, running Fit
with 392
FTP task 393