Professional Documents
Culture Documents
Chris Richardson Author of POJOs in Action Chris Richar son Consu!tin"# $nc %%%&chrisrichar son&net
TS-4439
2007 JavaOneSM Conference | Session TS-4439 |
'ast( thin"s can ha))en to (ou %hen (ou on*t %rite tests
#$T
About Chris
+ ,re% u) in -n"!an + .ive in Oa/!an # CA + 20 (ears of soft%are eve!o)1ent e2)erience
+ OO eve!o)1ent since 0934 + Java5 )!atfor1 since 0994 + Java P!atfor1# -nter)rise - ition 6Java --7 since 0999
+ Author of POJOs in Action + S)ea/er at JavaOneSM conference# JavaPo!is# '8JS# S9 :est# J;,s#< + Chair of the e=$, Java S$, in Oa/!an 6%%%&e>i"&or"7 + Run a consu!tin" an trainin" co1)an( that he!)s or"ani?ations >ui! >etter soft%are faster
2007 JavaOneSM Conference | Session TS-4439| 3
Agenda
When %evelopers Write Tests 8ast 8ee >ac/ $s -ssentia! =usiness Tier Tests Persistence Tier Tests :e> Tier Tests ,ettin" Starte
Agenda
:hen 9eve!o)ers :rite Tests *ast *eedbac) +s Essential =usiness Tier Tests Persistence Tier Tests :e> Tier Tests ,ettin" Starte
10
Test Test
Business Tier
Test
Test
Test Test
Persistence Tier
Test
Test
Test Test
11
+ ConseGuences
+ Chec/-in %as a >i" ea! rare!( one + 9eve!o)ers i n*t run tests >ro/en >ui! s
2007 JavaOneSM Conference | Session TS-4439| 12
'ne Reason for &lo, Tests - .oing 'utside of the /irtual Machine for the Java (latform 0J/M1 +nterface2
+ Tests that cross JJM interface >oun aries are "enera!!( s!o% + 9ata>ases are s!o%
+ Testin" at ever( !a(er hit the ata>ase over an over a"ain
WebTests
JVM
Web
ServiceTests
Service
DAOTests DAO
9ata>ase
The ter1s CJava Jirtua! MachineD an CJJMD 1ean a Jirtua! Machine for the Java5 )!atfor1
2007 JavaOneSM Conference | Session TS-4439| 13
Agenda
:hen 9eve!o)ers :rite Tests 8ast 8ee >ac/ $s -ssentia! #usiness Tier Tests Persistence Tier Tests :e> Tier Tests ,ettin" Starte
16
from to
<<interface>> OverdraftPolicy
oOverdraft Policy
17
18
9ata>ase access
19
MoneyTransfer Service
+ ;sin" 1oc/s
+ Si1)!ifies tests Mock AccountRepository + S)ee s u) tests + -na>!es an o>Fect to >e findAccount() teste in iso!ation + -na>!es to)- o%n eve!o)1ent
2007 JavaOneSM Conference | Session TS-4439| 21
Creating Moc)s
+ :rite (our o%n 1oc/s
+ Si1)!e for interfaces >ut it >eco1es te ious + Ko% to 1oc/ concrete c!assesE
23
24
%o,nsides of Moc)s
+ Testin" the co!!a>oration of o>Fects N %hite >o2 testin" + Tests can >e >ritt!e
+ Chan"e esi"n %ithout chan"in" %hat it oes fai!in" tests + 9iscoura"es eve!o)ers fro1 %ritin" tests
+ Moc/ se!ective!(H
25
Agenda
:hen 9eve!o)ers :rite Tests 8ast 8ee >ac/ $s -ssentia! =usiness Tier Tests (ersistence Tier Tests :e> Tier Tests ,ettin" Starte
26
Can 1( a))!ication create# fin # u) ate an e!ete )ersistent o>FectsE 9oes the ata en u) in the correct ta>!eOco!u1nE
#ibernate AccountRepository
!ibernate"ma##ing #ackage$%net&c!risric!ardson&banking'(am#le&domain% default"access$%field%) class name$%Account% table$%BA*+,A--./*T% ) id name$%id% column$%A--./*T,I0%) generator class$%nati1e% 2) 2id) #ro#ert3 name$%balance% column$%BA4A*-'% 2) 2class) & 2!ibernate"ma##ing)
#ibernate
Session
$uery
27
+ 9ra%>ac/s
+ .ots of ata>ase access s!o% + 'ee to initia!i?e the ata>ase ifficu!t to %rite
+ So!ution
+ Moc/ the ORM fra1e%or/ AP$s
Moc) 6ibernate
29
+ So!ution
class Ban)ing&apping!ests e"tends 6ibernate&apping!ests { public void testAccount() { assert#lass&apping(Account(class/ 7BA58,A##O$5!7); assertAll9ields&apped(); }
2007 JavaOneSM Conference | Session TS-4439| 30
+ 'o insta!!Bit*s Fust a Java Archive 6JAR7 fi!e + OORM N 9= )orta>i!it( 1a/es this eas( + $ssues
+ 9ifficu!t to o if usin" han -co e SA. + So1e inco1)ati>i!itiesM e&"& ti1e )recision
31
%EM'
:a!/throu"h Persistence Tests Co e
33
Agenda
:hen 9eve!o)ers :rite Tests 8ast 8ee >ac/ $s -ssentia! =usiness Tier Tests Persistence Tier Tests Web Tier Tests ,ettin" Starte
34
Web%class%'
J5P 7
(((
&
37
+ Three co1)onents
+ Core N JavaScri)t5 techno!o"( !i>rar( that runs in the >ro%ser + $9- N 8irefo2 )!u"-in for recor in" an e2ecutin" tests + Re1ote Contro! 6RC7 N fra1e%or/ for %ritin" auto1ate tests in JavaO&'-TORu>(O<
38
39
e %e> container
+ Testin" %ith a %e> container in a se)arate JJM interface + ;se the Car"o o)en-source fra1e%or/
+ T()ica!!( s!o%er + ReGuires a :AR to >e >ui!t
+ $nsta!!sOstartsOsto)s %e> containers + 9e)!o(sOun- e)!o(s %e> a))!ications + Java AP$# Maven P!u"-in# Ant tas/s# $9- )!u"-ins
2007 JavaOneSM Conference | Session TS-4439| 40
Cargo E4ample
public :eb!est e"tends { public void set$p() t'ro2s -"ception { Aip$1@Installer installer % ne2 Aip$1@Installer( ne2 $1@(7'ttp<==apac'e(tradebit(com=(((=?a)artaBtomcatB0(+(4>(Cip7)/ ne2 9ile(temp3ir/ 7tomcatBinstall7)); installer(install(); !omcat0"Standalone@ocal#onfiguration config % ne2 !omcat0"Standalone@ocal#onfiguration( ne2 9ile(temp3ir/ 7tomcatBdeploy7)); config(setProperty(ServletPropertySet(PO1!/ 7>+>+7)); :A1 2ar % ne2 :A1(locate:A1(72ebapp=target=ptrac)(2ar7)); config(add3eployable(2ar); container % ne2 !omcat0"Installed@ocal#ontainer(config); 9ile 'ome % installer(get6ome(); container(set6ome('ome); container(start(); }
41
%EM'
Revie% an Run Se!eniu1OCar"o Test Co e
42
43
Agenda
:hen 9eve!o)ers :rite Tests 8ast 8ee >ac/ $s -ssentia! =usiness Tier Tests Persistence Tier Tests :e> Tier Tests .etting &tarted
44
45
46
+ Run
+ =efore chec/-in + :ith CruiseContro!
47
%EM'
Recor in" a :e> Test :ith Se!eniu1 $9-
48
+ :or/in" on a co1)onent
+ :rite characteri?ation tests for e2istin" >ehavior + :rite so1e tests for the ne% >ehavior + Ma/e the tests )ass
49
50
&ummary
+ =a thin"s can ha))en %ithout tests
+ 9eve!o)1ent s!o%s o%n + The a))!ication eca(s + $t can >e a o%n%ar s)ira!
+ Just o itH
2007 JavaOneSM Conference | Session TS-4439| 51
+ ProFectTrac/ Sa1)!e Co e
+ TS-7032B=ui! in" JavaServer 8aces A))!ications %ith S)rin" an Ki>ernate + =O8-7344BThe .on"-Tai! Treasure Trove + =O8-432IBTestin" :e> 2&0 8eatures# ;sin" Rea!-:or! A))!ications + TS-4I33BA vance -nter)rise 9e>u""in" TechniGues
2007 JavaOneSM Conference | Session TS-4439| 52
?@A
53