使用Maven自动化集成测试
参考文章:http://stackoverflow.com/questions/16935290/maven-deploy-webapp-to-tomcat-before-junit-test
ThereareanumberofschoolsofthoughtastohowtohandlethistypeofintegrationtestwithMaven.
Ishouldpointoutthatwhenyouaredeployinganapplicationtoanapplicationserver,youarenotintherealmofunittestinganymore.Becausetheentireapplicationisbeingdeployedwithinacontainer,youaretestingtheintegrationofthosetwocomponents.
NowthereisnothingwrongwithusingJUnitforrunningintegrationtests(thoughtherearesomelimitationsthatyoumayhit,forexampleunittestsshouldnotcareaboutthesequencingofindividualtests-assumingyouarewritingthemcorrectly-soJUnitenforcesthisbynotguaranteeinganysequenceofexecution...priortoJava1.7theexecutionorderwasaccidentallyimpliedbytheorderoftestmethodswithinaclass,butitwasnotpartoftheJUnitcontract...Somepeopleswitchtoothertestingframeworksfortheirintegrationtests,e.g.TestNG,iftheyfindtheunittestfocusofJUnitisgettinginthewayoftheirtestdevelopment)
ThekeypointtokeepinmindisthattheMavenlifecycleusesthetestphasefortheexecutionofunittests.
Whenitcomestointegrationteststherearetwo(andahalf)schoolsofthoughtastotherightwaytohandlethetestswithMaven.
School1-Failsafeandintegration-test/verify
Thisschoolofthoughtusesthephasesafterpackagetostartupacontainer,runtheintegrationtests,teardownthecontainer,andfinallycheckthetestresultsandfailthebuildintheeventoftestfailures.
NEVEREVERRUNmvnintegration-testasthatwillnotteardownthecontainercorrectly,anytimeyouthinkyouwanttotypemvnintegration-testyouactuallywanttotypemvnverify(ohlook,it'sshorterandeasiertotypealso...bonus)
Sowiththisyoudothefollowing:
Bindtomcat7:runtothepre-integration-testphasewithfork=true
Bindfailsafe:integration-testtotheintegration-testphase
Bindtomcat7:shutdowntothepost-integration-testphase
Bindfailsafe:verifytotheverifyphase.
Forextrabrowniepointsyouwouldusebuild-helper-maven-plugin:reserve-network-portboundtothevalidatephasetoensurethatthetestserverisstartedonanunusednetworkportandtheneitheruseresourcefilteringagainstthetestresourcestopasstheportthroughtothetestsoruseasystempropertypassedthroughsystemPropertyVariablestomaketheportnumberavailabletothetests.
Advantages
CleanMavenbuild
Ifthetestsfail,youcannotreleasetheproject
Canmovetheintegrationtestsintoaseparateprofile(byconventioncalledrun-its)ifthetestsaretooslowtoruneverybuild.
Disadvantages
HardtorunthetestsfromanIDE.Alltheintegrationtestsstart/endinITandwhileMavenknowstoruntestsstarting/endinginTestwithSurefireandruntestsstarting/endinginITwithFailsafe,yourIDEprobablydoesn't.Additionally,yourIDEisnotgoingtostartthecontainerforyou,soyouhavetodoalotofworkbyhandtoactuallyrunthetestsmanually.
Debuggingthetestspotentiallyrequiresattachingtwodebuggers,e.g.onetodebugtheapplicationrunningincontainerandtheothertodebugthetestcases.
mvnDebug-Dmaven.failsafe.debug=trueverify
CouplesyourteststotheMavenbuildprocess.
School2-Separatemodule
Thisschoolofthoughtmovestheintegrationtestsintoaseparatemodulethatdependsonthewarmoduleandcopiesthewarintothetestresourcesusing,e.g.dependency:copy-dependenciesboundtothegenerate-test-resourcesphasecoupledwithaTomcat7dependencytotestagainst.
ThetestcasesthemselvesstartuptheTomcat7containerusingembeddedmode
Advantages
TestscanruninIDE
IntegrationtestsareseparatedfromUnittests,soaskingtheIDEtorunalltestswillnotkickofftheslowertests
Disadvantages
Thewarartifactisonlyrebuiltifyougopastthepackagephase,consequently,youneedtorunatleastmvncleanpackageperiodicallytorefreshthecodeundertestwhenusingtheIDE.
Thefailureoftheintegrationtestsdoesnotbreakthebuildofthewarmodule,soyoucanendupreleasingabrokenwarartifactandthenhavethereactorbuildfailfortheintegrationtestmodule.Somepeoplecounteractthisissuebyhavingtheintegrationtestmodulewithinsrc/itandusingMavenInvokerPlugintorunthetests...thoughthatprovidesapoorerIDEintegration,soIdonotrecommendthatline.
HardtogetaconsolidatedtestcoveragereportfromMaven.
Havetocodethecontainerstart/stopyourselffromwithinyourtestcases.
School2.5-FailsafewiththetestcasesstartingtheirownTomcat7server
Thisisakindofhybridofthetwoapproaches.
YouuseFailsafetoexecutethetests,buttheteststhemselvesareresponsibleforstartingandstoppingtheTomcat7containerthatyouwanttotestin.
Advantages
Don'thavetoconfiguretheserverstart/stopinMavenpom
IDEcansafelyrunalltests(thoughtheintegrationtestsmaybeslowerandyoumaywanttonotrunthem,butit'snotliketheywillallfailunlessthereisatestfailure)
EasiertodebugthetestsfromyourIDE(onlyoneprocesstoattachagainst,andtheIDEusuallymakesiteasytodebugtestsbyprovidingaspecialtestrunner)
Disadvantages
Havetocodethecontainerstart/stopyourselffromwithinyourtestcases
Ihopetheabovehelpsyouunderstandtheoptionsyouhave.Theremaybeothertweaksbutingeneraltheaboveareconsideredthebestpractice(s)forintegrationtestingwithMavenatpresent.