Agile Development Tools for Test-Driven Development with Java

machinebrainySoftware and s/w Development

Jun 8, 2012 (5 years and 3 months ago)

339 views

AgileDevelopmentToolsforTest-Driven
DevelopmentwithJava
Dr.ArminR
¨
ohrl
StefanSchmiedl

a

s

@approximity.com
http://www.approximity.com
November2002
(C)2002byApproximityGmbH1
Contents

Test-DrivenDevelopment(TDD)

Tools

Conclusion
(C)2002byApproximityGmbH2
TDD:Develophigh-qualitycodeinlesstime

IfExtremeProgrammingistooextreme,startwith
test-drivendevelopment.

TDD

BUF(bigupfrontdesign).

TDDimprovesdevelopmentspeedandefciency,
becausendingmistakesearlyischeapandfast.

Shortiterationsinvitefrequentfeedback(lean
manufacturing).

TDD-testsuitesmakeexcellentdocumenationand
specication:complete,uptodate,executable.Nomore
nevertoucharunningprogram.
(C)2002byApproximityGmbH3
GainingSimplicity

VillainsinJamesBondmoviesneverdothesimplestthing
thatcouldpossiblywork.JohnBrewer

Twowaysofconstructingasoftwaredesign:Onewayisto
makeitsosimplethatthereareobviouslynode?ciencies,
andtheotherwayistomakeitsocomplicatedthat
therearenoobviousde?ciencies.C.A.R.Hoare
(C)2002byApproximityGmbH4
TDD:Therules

Writetestsbeforeyouwritethemethods.

Refactortohighqualitycode:Avoidallduplication.

Beminimalistic:YAGNI,DTSTTCPW;

Makenoexceptions.
(C)2002byApproximityGmbH5
TDD:Theresults

Organicprogramdesign

Individuallytestable,losely-coupledcomponents

Qualitycode
(C)2002byApproximityGmbH6
Traf?c-lightcoding

Red:Writeatestthatdoesnotwork,maybenoteven
compiles!

Yellow:Makeitwork!Ifnecessary,feelfreetosin!

Green:Refactor!Removethesins!
(C)2002byApproximityGmbH7
RulesofThumb

Testallthatcouldgowrong;

Proceedinbabysteps.

60%test-codeand40%production-codeisnotunusual.

Allunittestsmustrunat100%,allthetime.
(C)2002byApproximityGmbH8
Babysteps

BecomeTest-Infected!(ErichGamma)

Getaddictedtothegreenbar.

Testslikeaplomb...beforesomethinggoesbadlywrong.

Produceonlywhatisreallyneeded.

Don'tbloatthecode!
(C)2002byApproximityGmbH9
Whenisatestgood/bad?

Agoodtestislikeacanaryinamine:
Itdiesbeforesomeonegetshurt.

Dependenciesbetweentestsarelikeusednappies:
Theysmellbad.
(C)2002byApproximityGmbH10
JUnit

Junit:Unit-TestingframeworkforJava








 

(C)2002byApproximityGmbH11
JUnit?Buggysamplecode





























 









 





































(C)2002byApproximityGmbH12
JUnit?Testcode





















































 



























































 


















































































































 














 








 






 





































(C)2002byApproximityGmbH13
JUnit?ConsoleOutput













 





























































 






 





























 






















 

























 



















 
























 
























 





























 






























 














 













 













(C)2002byApproximityGmbH14
JUnit?SwingGUI











 

 




















(C)2002byApproximityGmbH15
Refactoring

Donotalterexternalbehavior,butimprovetheinternal
structureofthecode.

Improveitsquality,reliability,andmaintainability.

Howgoodthedesignisdoesn’tmatternearasmuchas
whetherthedesignisgettingbetterorworse.Ifitis
gettingbetter,daybyday,Icanlivewithitforever.Ifitis
gettingworse,Iwilldie.KentBeck
(C)2002byApproximityGmbH16
RefactoringTools

RefactorIT(esp.ifyouuseNetbeans/Forte)











 


 




 

IntelliJIDEAoffersabout25refactorings






 





 






Eclipse(builtintoEclipseR1.0;essentiallyrenamesand
extractmethod,...)










 


Emacs(Xrefactory):quiteadvanced





 






 



 


 


Nothingseemstobeatrefactoringtoolsintegratedin
SmalltalkClassBrowser.
(C)2002byApproximityGmbH17
RefactoringBiblebyMartinFowler




 


 





 
(C)2002byApproximityGmbH18
Mockobjects(MO)
www.mockobjects.com

Replacedomaincodewithdummyimplementations
thatbothemulaterealfunctionalityandenforce
assertionsaboutthebehaviourofourcode.
(C)2002byApproximityGmbH19
AboutMockobjects

Nontrivialcodeisdifculttotestinisolation.

MOallowunittestsforeverything,simplifyteststructure,
andavoidpollutingdomaincodewithtesting
infrastructure.

Testonefeatureatatime,andbenotiedassoonasany
problemoccurs.Normalunittestingcanbehardasyou
testthecodefromoutside.

Decidewhatatestwouldneedtoverifytoshowthatit
hadpassed,andaddmockimplementationsofobjects
thatrepresentthoseconcepts.

DonotattempttoreproducerealbehaviourinaMO;
(C)2002byApproximityGmbH20
HowtoUnit-TestwithMOs

CreateinstancesofMOs

SetstateintheMOs

SetexpectationsintheMOs

InvokedomaincodewithMOsasparameters

VerifyconsistencyintheMOs
(C)2002byApproximityGmbH21
WhyuseMOs?

Youcandeferinfrastructurechoices:justuseMOs.

Yourcodewillscale:Thelightweightemulationis
independentoftherestofyourcode.

Youwon'tleavestonesunturned:MOscanbe
programmedtoemulateanykindofexternalerror
condition.

Domainobjectsoftenfailonlysometimeafteranerror
occurs,MOswillfailnow.

DevelopingwithMockObjectshasbenecialeffectson
thecodingstyle.
(C)2002byApproximityGmbH22
MOSystemFailure














 



 





















 


















 










 


































 
















 








 
















 












 








 



 




































(C)2002byApproximityGmbH23
MOWidgetPolling









 















 

 







 




 



 













 

 




 


























 









 




 










 

 








 

 










(C)2002byApproximityGmbH24
HTTPUnit:blackboxwebtesting












 
 







Bypassthebrowserandaccessawebsitefroma
program.

automatedwebsitetesting

usingaweb-siteaspartofadistributedapplication

HttpUnitemulatesbrowserbehavior:

formsubmission,

basichttpauthentication,

cookies,

automaticpageredirection,and

allowsJavatestcodetoexaminereturnedpagesastext,
anXMLDOM,orcontainersofforms,tables,andlinks.
(C)2002byApproximityGmbH25
HttpUnitExample









 


















 







 























 






























 
























 











(C)2002byApproximityGmbH26
Latka:functional(end-to-end)testingtool

 
 









 

 
 



 

 


implementedinJava

XMLsyntaxtodeneaseriesofHTTP(S)requests

validationstoverifythattherequestwasprocessed
correctly

runnableviaJUnitorasaTomcat(notTomcat3.2;4.0B7
orbetter)servlet
(C)2002byApproximityGmbH27
LatkaviaJUnit







 























































































 
















































(C)2002byApproximityGmbH28
LatkaviaTomCat









 







































































 


 


















 
























































 






 













 






 





















 






 













































 






 






 


















 

 
















 







 



























 
















 







(C)2002byApproximityGmbH29
Cactus:server-sideunittesting

 
 









 

 





asimpletestframeworkforunittestingserver-sidejava
code(Servlets,EJBs,TagLibs,Filters,...)

lowerthecostofwritingtestsforserver-sidecode

itusesJUnitandextendsit

developedwiththeideaofautomatictestinginmind
anditprovidesapackagedandsimplemechanism
basedonanttoautomateserver-sidetesting

implementsanin-containerstrategy
(C)2002byApproximityGmbH30
Cactus:Squence
(C)2002byApproximityGmbH31
Cactus?Scope
(C)2002byApproximityGmbH32
Cactus?TypesofUnit-Tests

Type1:codelogicunittesting.Probablythebesttousea
MOstypeframework.

Type2:integrationunittesting.Cactusistypicallyinthis
category.Thesetestswillexercisetheinteractionswiththe
container.

Type3:functionalunittesting.Theseunittestswillletyou
testthereturnedvaluesfromyourservercode.Thisisfor
exampleHttpUnit(NotethatHttpUnitalsoperforms
standardfunctionaltesting-asopposedtofunctional
unittesting-,whichletyoutestfullusecases-aloginuse
caseforexample,whichiscomprisedofseveral
requests/responses).
(C)2002byApproximityGmbH33
GUItesting:AbbotundJemmy









 
 






Abbot:

Scriptedcontrolofactionsandinspection

Loosecomponentbindingsbydynamiclookup

Specifyhigh-levelsemanticactions,implementedby
low-levelOSevents

Supportsrecordingandplaybackofuserevents

Usefulforunitandfuncationaltesting
(C)2002byApproximityGmbH34
Abbotsamplecode1/2





 































































 



















 



















 



















































































 





























 





















































 






 


















(C)2002byApproximityGmbH35
Abbotsamplecode2/2







 








 







 




 










 

































 














 





















 





 




 




 

 

























 































(C)2002byApproximityGmbH36
Jemmy
Jemmy

 











 


lessoverheadthanJFCUnit;NetBeansIDEintegration

morepowerful;fullSwingSupport;

Codesnippet:





 
















 






 














































 



 










































 






 









































 













 






 
















 





(C)2002byApproximityGmbH37

xmlunit












 
 










 































 
 























 












































 


 























(C)2002byApproximityGmbH38

xmlunit

?Comparingtrees

Decidingiftwodocumentsareequaltoeachotherisn't
aseasyasacharacterforcharactermatch.

XMLUnitallowsyoutocompareXMLinles,orinstrings.




 







 





























 































 

























ExtractspeciccontentusingXPathexpressionsor
performXSLtransformations.

EvendigestbadlyformedHTMLasXMLsoitcanbe
tested,too.
(C)2002byApproximityGmbH39
Anteater:testingframeworkdesignedaroundAnt
 






 
 







testingframeworkdesignedaroundAnt.

writetestscheckingthefunctionalityofaWeb
applicationoranXMLWebservice.

SendaHTTPrequestandanalyzetheresponse

CheckincomingHTTPrequestsandrespond
accordingly

UniqueabilitytowaitforincomingHTTPrequestsmakes
Anteaterespeciallyusefulfortestinghighlevel
SOAP-basedapplicationslikeebXMLorBizTalk.
(C)2002byApproximityGmbH40
Anteater?Codesample




 





















 








 

 

































 


 
















 








 












 
 
















 





























 

 








 
 










 





























 















 


(C)2002byApproximityGmbH41
Subversion




 





 





 


replacementforCVS

Bettercontroloverdirectories(deleteandrename)

Ofinemode.

Atomiccommits

WebDAVinterfacetotherepository.
(C)2002byApproximityGmbH42
ant

 
 









 






Java/XML-basedbuildtool,notshell-basedlikemake.

platform-andOS-independent

Makelesareinherentlyevil:-)
(C)2002byApproximityGmbH43
WhenTDDmightbeharder...

Security

Parallelcomputing

Legacycode(addteststogaincondence)
(C)2002byApproximityGmbH44
Bibliography1/2

TestDrivenDevelopmentByExample,KentBeck,
Addison-Wesley,2002.







































 











 












 

UnitTestsmitJava.DerTest-First-Ansatz,JohannesLink,
PeterFr
¨
ohlich(Mitarbeiter),dpunkt2002.

Refactoring,MartinFowler,AddisonWesley,1999.
(C)2002byApproximityGmbH45
Bibliography2/2

ExtremeProgrammingExplained:EmbraceChange,Kent
Beck,AddisonWesley,1999.

ExtremeProgrammingInstalled,RonJeffries,Chet
Hendrickson,AddisonWesley,2000.

PlanningExtremeProgramming,KentBeck,MartinFowler,
AddisonWesley,2000.

F.P.Brooks,TheMythicalMan-Month.Reading,
Addison-Wesley,1975.
(C)2002byApproximityGmbH46
Mailinglists

extremeprogramming@yahoogroups.com

xp-forum@yahoogroups.de

testdrivendevelopment@yahoogroups.com

mailinglistsofspecicopensourceprojects.
(C)2002byApproximityGmbH47
Conclusion1/2

UnitTestscanoverlookorcontainerrors.

UnitTestswillnotcatchfailuresthatarisefrominteractions
betweencomponents.

Softwaredevelopmentisanongoingghtagainst
complexity!

Steeringismoreimportantthanspeed,indrivingandin
softwaredevelopment.RonJeffries
(C)2002byApproximityGmbH48
Conclusion2/2

Myruleofthumb:IfIamworriedaboutsomething,Iwrite
atest.ThenthecomputerdoesmyworryingformeandI
cangoanddosomethingusefulwithmytime.William
Pietri

TDDisacompetitiveadvantage!

Nextlogicalstep:acceptancetestsandpair
programming.

Shipbeforetheycanstopit!RonJeffries

Softwaredevelopmentshouldbefun.
(C)2002byApproximityGmbH49
Questions/Comments
Dr.ArminR
¨
ohrl
StefanSchmiedl

a

a

@approximity.com
http://www.approximity.com