API  T E S T     A U T OM A TION DEE P  D I V E   I N T O   P R O T O C O L S T A C K API  TEST     AUT OMA TION DEEP  DIVE  INT O  PR O T OC OL ST A CK
About  An t on  Se me nche nk o Co - f ounder  \ a ct i vis t  of  C OM A QA .B Y ,   Co r eHa r d.B Y an d  In t erIT c omm uni ti es,   c o - f ounder  of  DP I.S ol uti ons ,   manager at  EP AM S ys t em s.  Mor e than  16  year s of  e xper i enc e in   IT .  Speci al i z es  i n  l o w - l e vel  de velopme n t,  Q A aut omati on,   managem en t,  sal es.
A g e n d a 3 1 OSI   Mod el T oo ls 3 W e b  se rvice   te stin g 2
7  A P P LI C A T I ON 6  P R E S EN T A T I ON O S I m o d e l 4 5  S E S S I ON 4 T R A N S P OR T 3 N E T W OR K 2  D A T A LINK 1 P H Y S I C A L D AT A S E G ME N T     P A C K E T     FRAME   B I T S PO R T    I P    M A C
7   la y er  OSI  model : 1. I t   i s not  s t r i ct r epr ese n t a t i on  of  act ua l   ar chite ctur e,   but  r a t her   c oncep tion th a t  help s t o unde r s t and i n t er acti on  o v er   th e  ne tw or k .   I  beli e v e al mos t  al l   of  y ou  al r eady  sa w  t hi s  mode l ,  or   a t   l eas t   hear d  ab out  it .   OSI  s t an ds  f or   Open  S y s t em   I n t er c onnecti on
7   la y er  OSI  model : 2. The fir s t  la y er is  th e  p h y sical  la y er .   Th i s  i s  h o w  d o   d e vic es  c o n n ec t .  Ar e  th e y  c onnec ti ng a cr oss  wir es ?  Or   m a ybe  wit h fi ber ? 3. The sec o n d   la y er  up h er e  i s r e f er r ed  t o as t he  Da t aLink La y er .   Th e  Da t aL i nk l a y er   i den t i f i es  h o w  th e el ec tr o n ic s  signal should  b e  tr ansla t ed .  I s it   E t her ne t   or   wir eless.   I t   c oul d be 8 0 2 . 1 1   f or   wir eless  or   8 0 2 . 3   f or   E t her ne t   s t an da r ds.   Da t aL i nk l a y er   i s  sa ying   h o w th e  b its of  d a t a ar e p ac k ag ed . 4. The th ir d   le v el  up h er e  i s  t he  n e tw o rk  la y er .   T he n e t w ork   l a y er  i s  wher e  w e  i n t r oduce  t he c once p t   of  ad dr essing .   Fr o m  add r ess,  and   t o  add r ess .  5. Th ese fir s t  th r ee  la y er s  ar e  de fi ned unde r   th e  OSI  m ode l  t o be   c o n tr o ll ed  b y  th e  n e tw o rk .  The ne x t  f our  ar e  t he s y s t em  or   soft w ar e  c on t r oll ed  c omponen t s.
7   la y er  OSI  model : 6. La y er n u mb er  f our  i s  c al l ed  t he  T r ansp o rt  La y er .  T r an sport  i s t ell i ng   us ho w  w e  ar e  g oing   t o  p ac k ag e th e  d a t a  b ec aus e  w e d on't   sen d   al l  o f  th e  c o n t en t  a t one time .   W e  send   it  in p ac k e ts .  The y  ar e  smal l   pi ec es  of  da t a  t ha t   ar e  t ak en  f r om  a  l ar g e bund l e of da t a  on  t he s y s t em  her e  … a nd  chopped  i n t o  smal l er   pi ec es. … .   an d  t hen  sen t  acr oss  t he in t er ne t   an d  r eassem bl ed  a t  th e f ar  si de.   So  tr anspor t  m ec hani sm s disassem bl e  th es e  pi ec es ,  se nd  it   acr oss  th e  i n t er ne t  an d r eassem bl e  i t   a t  th e other   si de.   T r an sport  La y er   i s  v ery  i mpor t an t  f or   r eli ab i l i t y  of t he  da t a.   I t   ensur e  t ha t   I   act ua l l y  g e t  al l   of  t he me ssage .  I f   I  g e t  pa r t   of t he  me ssage   f r om  one  sour ce   an d  part f r om   anot her ,  if   I  c an  pr ope r ly  put  them  back  t og e th er ,  I  ha v e  t he c omple t e  me ssage .  The t r an sport  t ell s  me ,  ho w  I   br eak   do wn  an d  put  ba ck  t og e t her   t he  da t a.  
7   la y er  OSI  model : 7. Ne x t   l e v el up  her e  i s Session.   This  is  wh er e  sessio n s  ar e han d led .   I t   open s,   cl ose, an d   able t o r ec ov er  a session .   And  it   al lo w s  t o  s ynchr oni z e  i n f or ma t i on  f r om  di f f er en t  s t r eams.  8. Al l   r i gh t ,  so  one  l a y er   up  i s  la y er  6   is th e pr esen t a tio n   la y er .  I t  h and les  th e la n gu ag e  o r  f o rma t  th a t am  I  u sing ?   And  so ,  when  I 'm   t al k i ng   ab out  th e c omm uni c a t i on t ha t 's  g oin g  acr oss  a p arti cula r   se ssion,  acr oss  a parti cular  pr ot oc ol,  I  c ould use m ul ti pl e  la ngu ag es .   If  I  use HT TP  an d  P or t   8 0   t he la ng ua g e  mig h t   be  HTML .  Or   I   c oul d  use  oth er   f or ma t s.  I  c oul d use  XML .   Th er e' s  a l ot of   oth er   l an gu age s  t ha t   mig h t   be  used acr oss  HT TP .   Al so som e  sour ce s  clai m s  th a t  SSL  lea v es  her e,   in   pr es en t a ti on  la y er .
7   la y er  OSI  model : 9. Whe n  w e  mo v e  up h er e  t o th e  se v en th   la y er  her e,   w e' r e  g onna t al k  about   th e  App li c a tion L a y er .  The A ppl ic a ti on  La y er   is  th e  actua l  ap pl i c a t i on  t ha t 's  g oin g  t o  mana g e  t he da t a  an d c on v er t s  i n f or ma t i on t o  da t a  an d  ba ck w ar ds.   Whe n  I  sa y A pp li ca tio n  La y er ,  o u r  e x ample her e  w o u ld  b e br o w ser s,  a t lea s t  f o r  th e  c ase w e'r e  t al ki n g  about  HT TP .   Or  HT TP   c li en t  f o r  A PI .
C LI E N T SE R V I C E 7 A P P L I C A T I O N 6 P R E S E N T A T I O N 5 S E S S I ON 4 T R A N S P OR T 3 N E T W OR K 2  D A T A LINK 1 P H Y S I C A L A P I   t e s t a u t o m a t i o n GET http://w ww . exa m pl e. com /c ust om ers /33 2 45 200  O k  +  P a y l o a d R e q u e s t d a t a     D a t a f o r ma t :80/ :443 UD P / T C P    IP M A C 10
Th e  OSI  model  descr i bes  ho w  t o   encap sulat e  the pie c es  o f  da t a  a nd   send   tr o u gh   th e  n e tw o rk .  W ell ,  her e' s  a  pa ck e t   of  dat a .   T he b l ue  c olu mn. Ag ai n,  t hi s  i s  b y  e x ampl e,  I   am not   t r ying   t o  descr i be  pr ec i sely  s t anda r ds  of an I P  pack e ts  c on t en t, bec ause  th is  if   f ar  fr om   th is  w or k shop  sc ope.  I  am  g onna s t art h er e  a t   l a y er   se v en,  t he  Appl i c a t i on  La y er .   Her e  i n  t he  pa ck e t ,  t he fi r s t   t hi ng   w e' r e  g onna do  i n  l a y er   se v en' s  g onna put   da t a  i n  her e,   som e  pa yload.   Th is d a t a c ould   b e en cr yp t ed .   I   c ould put  a fl ag   t ha t   sa y s  t ha t   encryp t i on  i s used.   A s  an  e x ample V PN .  Th e ne x t  la y er   of  t he mo del  i s six   an d  t hi s  i s wher e  I   w oul d  put   t he  f or ma t .  I  need  a w a y t o id en t i f y  wha t   t he  f or ma t   of  t he da t a  i s . I  ha v e la y er  fi v e and l a y er   f i v e  i s ab out  m y por t   or   m y pr ot oc ol.   I n her e,   I 'm  id en t i f ying w ha t   me chani sm I'm  usi ng .   I 'v e  g ot P or t   8 0 ,  i f   i t 's  HT TP   f or   e x ampl e. 
My la y er   f our  her e  i s th e T r an sport  La y er .   Th e  t w o  mos t  c omm on  f or ms  of  t r an sport ar e  UDP ,  User  Da t agr am Pr o t o c o l  a nd   T CP ,  T r ansmissio n   Con tr o l  Pr o t o c o l . I n  UDP  sequence   of  deli v ery , se quence  of  t r an smissi on doesn't  me an   as  much. Th e other   op t i on  her e  i s T CP , T r an smissi on Con t r ol  Pr ot oc ol. Whe n  I   send  c on t en t  via   T CP sequ ence   ma t t er s. T CP  i s mo r e  i mpor t an t  th a t   i t   be  deli v er ed an d r eassem bl ed  sequen t i al l y .   Le t's  jus t sa y  I   ha v e  a  2 0 - w or d  message. And  I  r ece iv e  pa rt  one,  pa rt  t w o ,  pa r t   t hr ee , pa r t  fi v e. W ai t   I  need  pa r t  f our , R e t r a ns mit   pa rt  f ou r  so  t ha t   I   c an  r eassem bl e t he  me ssage . I f   I 'm  doi ng  U DP ,  i t   mig h t   not  bee  needed. So  i n l a y er   f our  I  ha v e in f or ma t i on t ha t   al l o w s me   t o  r ec o v er   t he  c on t en t of  mult i pl e  pa ck e t s.  I  don't send  al l   of m y  da t a  i n  one  pa ck e t . I  chop  i t   i n t o  pi ec es . 
Th e bot t om  t hr ee   ne t w or k   l a y er s.  La y er   t w o  i s  wher e  t he  MA C ad dr ess  e x i s t s.   Th i s  i s th e ph y si c al   I D of  a ne t w or k   c ar d  i n  a  P cso I  wil l   ha v e the  FR OM  an d T O MA C ad dr esse s . And f i na l l y  l a y er   3   i s  f or   I P  ad dr ess.  Th i s  i s  t he  v ery  l as t   pi ec e  of  da t a  ad ded t o pack e t   an d  no w ,  I  c an  send t hi s  pa ck e t   usi ng   t he  OSI model  ba ck  an d f or t h  be t w ee n  t he  t w o  de vices.  Th e  OSI   mod el is an  idealiz ed   ab s tr ac t  mod el,  and   th er e ar e n o  p r o t o c o ls  in u se  t o d a y th a t f o ll o w  it .   HT TP   w as cr ea t ed  wit hout  r eg ar d  t o  t he OSI  mode l ,  so  t her e' s  no  poi n t  t o tr ying  t o m ak e  i t   ab solu t ely fi t .   I t   jus t  shoul d hel p  t o  i magi ne  wha t 's  g oin g on wh en  y ou  send  r eques t   som e whe r e.
R e q u e s t   - r e sp o n se   s t r uc t ur e  and typ e s 14 P O S T   /maps/ api/pla c e/ a dd /j s o n H T T P / 1 . 1 H o s t :   map s. g oog l ea pis . c om     C o n t e n t - T y p e :   appli c a ti on/ j s on    Acc ep t: app l i c a ti on /j s on { " l o c a t i o n " : { "l a t" : - 33.86 697 10, "l ng ": 151.1958750 }, "name":  "Andr e w  w a s her e", "phon e_ nu mbe r" : “+ 375( 29) 37 048 58", "types ":  ["bar"], } R e q u e s t   l i ne : ME TH OD |UR I |H T T P     v e r s i o n H E A D E R S M E S S A G E BO D Y
So  I  wil l   g o ahead  an d  mo v e  clot he  t o  mai n  t heme   of  t hi s  w or k shop.   I n  mos t   c ases when  y ou  ar e  w or k i ng   wit h  API ,  it  means  th a t y o u   wil l  send   some r equ es t  and   c h ec k  some r espons e .   Th i s  i s ho w  i t   usua l l y  w or k s. So  th is  is  r eque s t  e x ample.  I t c onsis ts f r om   r eque s t  li ne,   header s  and  me ssage   body .  Message   body  i s  op t i onal .   F or   Ge t   i t   op t i onal , and H E AD  r eques t   shoul d  not   ha v e thi s  pa r t .   Ok ,  r equ es t  li n e  c on t ai ns  R equ es t me th o d ,  URI  o f r eso u r c e ,  an d  v er sio n   o f p r o t o c o l .   Header  sec t i on  c on t ai ns a l l   i n f or ma t i on ab out   r eque s t  it se lf .   The o nl y  one   header   th a t  should  be  pr es en t ed  in   an y  c ase  i s  hos t .  Thi s  i s  ad dr ess  of  serv er   t ha t   w e  ar e  t al k i ng   t o. C ook i es,   c on t en t  types,  session  i n f or ma t i on wil l   be  s t or ed  i n  t hi s  sec t i on.   And r eques t   body  i t self .   Th i s  pa r t  c on t ai ns  t he  da t a,  t ha t   shoul d be sen t   t o  th e  se r v er .
R e q u e s t   - r e sp o n se   s t r uc t ur e  and typ e s 16 H T T P / 1 . 1   200 OK C o n t e n t - T y p e :   app l i c a ti on/ j s on; C o n t e n t - L e n g t h : 352 { "i d"  : "50ea6150666e0de 4b097", "plac e_ i d"  :  " qgY v C i 0 j l h Z T g z N G I " ,     "sc ope "  : "AP P ", " s t a t u s "   : "OK" } S t a t u s - L i n e : H T T P - V e r s i o n | S t a t u s - C o d e | R e a s o n - P h r a s e H E ADE R S M E S S A G E B O D Y
On  each r eques t   serv er   r epli es  wit h r esponse.  T ypic al l y  r espons e  wil l  c o n t ai n   St a tu s li n e, h ea d er s  and   messag e body .  St a tu s li ne  sho w s  e x ec ut ion s t a tu s  of  r eque s t.   Ar e  th er e  an y  er r or s,  or   r eques t   pr oce ssed  succe ss f ul l y .   Th er e  ar e  pr e t t y  bi g  amoun t  of  s t a t us  c odes,   I ’ll   descr i be  t hem  be t t er   i n ne x t  sli de.   Header s  pa r t  is t he same  wit h r eques t .   I t   c on t ai ns  i n f or ma t i on  ab out   r es ponse ,  se ssion  da t a,  c ook ies,  and  siz e  of  m es sag e  body . And a  me ssage  body  i t self .   I t   c oul d be  J SON,  XML ,  HTM L , m i me   t ypes.. .
R e q u e s t   - r e sp o n se   s t r uc t ur e  and typ e s 18 W E B S E R V I C E 2xx: Suc ces s 200 OK 201 C r e a t e d 4x x:  C l i e n t E r r o r 400  Bad R e q u e s t 403 F orbi d d e n 404 Not F ou n d 405  Me thod   No t Allow e d 410 Go n e 5x x:  Ser v er E r r o r 500  I n t e r n a l   Se r v e r E r r o r
About  s t a t us  c odes.  Th e y  ar e  r epr ese n t ed  an d  descr i bed  i n  RF C  7 2 3 1 .   V al u e  o f  s t a tu s c o d e  r epr esen t  r esult of  r espons e  e x ec u tio n .  Ther e  ar e  fi v e  subgr oup s. One  hundr ed  t o  fi v e  hundr ed . Th e  mos t   c omm on  ar e  tw o h u n d r ed ,  serv er   shoul d r e t urn  t hi s  c ode  i n  c ase  of su cc ess f ul  r eques t   pr oce ssi ng .   F our h u n d r ed s t a tu s  c ode s  should be r e tu r ned  in  c ase if   som e th in g  w en t   wr ong   on  r eques t or s  si de . And t he  500 (In t er na l   Serv er   Err or )  s t a t us  c ode  i ndi c a t es  t ha t   t he serv er   enc oun t er ed  an  une xpect ed c ondit ion t ha t  pr e v en t ed  it   fr om   fu lf il l i ng   t he r eques t .
D EL E TE R E A D    U P D A TE C R E A TE 201  C r e a t e d   + L o c a t i o n P O S T   http://w ww .ex am pl e.c om /cu st om ers  + pa ylo ad GET http://w ww . exa m pl e. com /c ust om ers /33 2 45 200  Ok  + P a y l o a d PUT http://w ww . exa m pl e.co m / cus tom ers /3 32 45 200 Ok D EL ET E http://w ww . exa m pl e.co m / cus tom ers /3 32 45 200 Ok RES T  and C R U D h t t p : / / www . a w e s o me por t a l . c o m :12 3 4 / p a t h / v er s i o n /r e so ur ce ? f oo= f oo%2 0 b a r     p r o t o c o l a d d r e s s port r e s o u r ce p a t h quer y 20
A n d   a f e w  w o r d s  about  RES T : 1. RE S T  is a r c h it ec tu r al   s tyle  o f  b u il d ing  w eb  ser vic es .  R es t   servi ce   usual ly  r epr es en ts  al l  da t a  as r es our ce s,  and  t o  in t er act wit h  th is  r esour ce s  w e  use  HT TP   me t hods.   So  t o send  r eques t   t o  i n t er act   wit h some   da t a,  r eques t   wil l   l ook s  l i k e:  t ype  of p r ot oc ol,  t hen wil l   be  t he hos t ,  or   ad dr ess,   or   ma ybe  y ou  wil l   w on t   t o use  I P  an d  port.   Th e  ne x t  pa r t   i s  r esour ce   pa t h,  an d  her e  c oul d be v er si on  of  endpoin t .   And  la s t  part i s query ,  it   c on t ai ns par ame t er s  th a t  y ou  w an t   t o  send t o  r esour ce .   2. E ach r esour ce   c oul d be  c r ea t ed ,  r ea d ,  u p d a t ed or   d ele t ed .  W ell   it  ob viousl y  depends  on  acce ss  an d  wha t  log i c  i s impl em en t ed  i n w eb  se r vice ?  B ut   in   g ene r al : t hi s  is c al led  cru d ope r a ti ons.  The  m ai n  i dea  i s th a t   d a t a  sh o u ld  b e c r ea t ed  with  p o s t  me th o d ,  r ea d   with  GET  me th o d ,  t o  DELETE  d a t a, me th o d   DELETE   sh o u ld  b e  u sed ,  an d  t o  u p d a t e,  me th o d   u p d a t e  sh o u ld  b e  u sed .
Cli e n t  – s er v i ce inte r a c tion T I ME P o l l i n g 22
Cli e n t  – s er v i ce inte r a c tion T I ME Lon g - po l l i ng 23
Cli e n t  – s er v i ce inte r a c tion T I ME W e b S o c k e t 24
Cli e n t  – s er v i ce inte r a c tion C LI E N T Load Balancer W E B S E R V I C E W E B S E R V I C E W E B S E R V I C E 25
Cli e n t  – s er v i ce inte r a c tion 26 C LI E N T Load Balancer
Cli e n t  – s er v i ce inte r a c tion 27
And i n  bi g  pr ojects  each  modul e  c oul d ha v e a  l ot  of e x t er na l   c onnecti ons.   Be  c ar e fu l  with  es tima tio n s  about  h o w mu c h   time  y o u   n eed  t o  moc k dep end encie s . Th i s  i n  ni ce  e x ampl e.  Th i s  i s  schem e  of  Amaz on  a nd   Ne t f l i x   micr o  servi ce s  s t r uctu r e.  Th i s  pi ctur e  i s  a t   l eas t   f e w  y ear s  old , so   I  am sur e  t ha t   i n  pr ese n t   t i me  it   e v en  mor e  i mpr essiv e. B y  t he  w a y ,  th is i s c al led  “De a th   s t ar ” diagr am .  P r e t ty nic e  w a y  t o   sh ow  th a t s y s t em  s tr u ctu r e  is r eal ly  c omp li c a t ed .
W e b   s er v i ce testing WEB S ER VI C E R e s o u r c e s Se r vic e     Do main R ep o s i t o r i es D a t a mappi n g S t o r a g e 29
I t   i s ni ce  an d  help f ul   i f  y ou  h a v e  ac c ess t o  w eb  s er vi c e  c o d e  and   u n d er s t and   its s tru c tu r e  and   h o w  it  w o rk s .  B ut  in  c ase i f   y ou  don’t   it  c o u ld  b e trick y  t o  figur e  o u t  h o w  and   wh a t  t o   c h ec k  with  b ac k end   t es ts .   I n  g ene r al   m os t  w eb  s ervices   c ould be pr es en t  as f oll o wing   s t r uctu r e.  I t  is  doesn’t  r epr ese n t   r eal   s t r uctu r e,   but  jus t   v ery - v ery  hi gh   l e v el c once p t i on.   So  w eb  servi ce   wil l   ha v e som e  r eso u r c es  o r  some end p o in ts,   th a t  ar e  e xp osed  t o  cl ien t . Al so  t her e  ar e  wil l   be  some   la y er  th a t r epr esen ts  mai n  lo gic ,  some th ing  th a t ser vic e  is  r espons ible  f o r .     And t her e  ar e  wil l   be  some   la y er  th a t  in t er ac ts with   d a t a  s t o r ag e .  Ba c k  en d   is  u su al ly  abou t  d a t a .   Ma ybe  in  y our   c ase it  will   r ead  fr om   da t ab ase,  or   s t or e  some   da t a, or   e x ec ute s som e  l ogi c  i f   da t a  change d.   Th i s  i s  wha t  servi ce   usua l l y  r esponsi bl e  f or .
So  t aking   t hi s  s t r uctu r e  i n t o acc oun t , y ou  c old  pl an   di f f er en t  se t s  of  t es t , and  y ou  c oul d  b e  su r e  th a t all   la y er s  wil l  b e  c o v er ed . F or   r es our ce s  th er e  wil l  be  t es ts  th a t  che ck   w eb  se r vice   c on tr act.   Ob viousl y  p o sitiv e  t es ts .  B ut  a ls o   ho w  bac k end  pr o c es s  r eques ts  without   mand a t o r y  fiel d s,  with  wr o n g  d a t a type,   and   wh ic h   d a t a i s  o p tio n al .  And  so  on.   Do n ’t   e xp ec t  th a t only  v al id r equ es ts  wil l  b e  sen t  t o  b ac k end .  Ch ec k  th e  c o n tr ac t ,  th a t  b ac k end   r ea ll y  r e tu rn s  wh a t is  cl ai med  in  d oc u men t a tion . Th e ne x t  se t   of  t es t s wil l   be  t es ts t h a t check   lo gic   ( ? ) ,  i mpleme n t ed  i n  w eb  servi ce .   Th i s  pa r t  of  t es t s fu l l y  depends  on  wha t ’ s g oin g  on  i n  y our  pr ojec t.   Th is is h ar d   t o  giv e some g en er al   idea  wh a t  sh ould   b e  check ed   h er e.  But  u su al ly  th is i s d a t a i tself ,  li k e  d o es  b ac k end   r e tu rn   p r o p er  v al u es .
A c tu al ly i n   some cases y o u   c an’ t  e v en  v al ida t e an y th ing.  Lik e  if y o u   h a v e  b ac k end   th a t sen d s  y o u   financia l mark e t  d a t a.   Y o u   c an’ t  p r edict  wh a t y o u   wil l  r ec eiv e.   So  y ou  wil l   ha v e t o  find   w o rk ar o u n d s,  o r  ma yb e  cus t omer  will   d eci d e  th a t d a t a v al u es itself  sh ould n ’t   b e check ed ,  ju s t  c h ec k  th a t it  h as  p r o p er  s tru c tu r e .   Th i s  i s  possi bl e t o . And t he la s t   se t   is  t es ts t o  chec k  in t er ac tio n   with  d a t a  s t o r ag e ( ? !) .  T r y  t o  cr ea t e  some   da t a  wit h ba ck end,  r e t r i e v e,   upd a t e.  Wha t  y our  servi ce   al lo w s t o  do.  Do n ’t   e xp ect  th a t t h er e  ar e will  b e  only  v al id d a t a  in  d a t abase .   I f   f i eld  i s  nul l ab l e ,  ad d t es t   t ha t   wil l   chec k   t ha t   ba ck end  c oul d  sussess f ul l y pr oce ss  t hi s  k i nd  of  da t a  wit h nu l l s  i n  da t ab ase.   And a g ai n  t hi s  i s hi gh   l e v el  i dea.   Be c au se  t hi s is  not   possi bl e t o s t r i ctl y  sa y  th a t  y ou  should do  li k e  th is  and t hi s,  bec ause  th er e  is a bi g  v arie ty  of  ap pr oaches  an d  ar chite ctur es  of  ba ck  ends.  So  each  c ase wil l   ha v e  specif i c  t es t s but  y ou  c oul d use  t hi s  c once p t i on  l i k e  chec k   l i s t ,  t o  v er i f y  t ha t   y ou  ar e  not   missi ng   some t hi ng .
W e b   s er v i ce testing WEB S ER VI C E R e so u r ce s Se r vic e Do main R e p o s i t o r i e s D a t a ma ppi n g G a t e w a y s + HT TP c li en t S t o r a g e Ext er na l se r vice S T UB S TUB 33
I n  c ase  of  mic r o   ser vic es  t he ma i n  i dea  is   t he  sa me bu t  bec a us e  i t   i n t er act s wit h a not her   micr o  servi ce s  i t  c oul d  ha v e  a  lo t of e xt ern al   d epen d encie s .   So  w e  shoul d t ak e  i n t o  acc oun t  th a t   i t   wil l   ha v e it s  o wn  clien t  t o in t er act wit h anot her   parts  of back   end.  Y es ,  m onoli th ic  ba ck end wil l   pr obab l y ha v e it , but   t hi s  i s mo r e  i mpor t an t  in   t hi s c ase.   Y o u   s til l  c an  t es t  th is  ki n d  of  b ac k end   in  th e same w a y as mono li th ic .   But  A LSO…  y o u   wil l  p r o b ably  w an t t o  t es t  mic r o   ser vic es  separ a t ely .  Y ou sa w  dea t h s t ar di ag r am.   I t   wil l   be  t ooooo c ompli c a t ed  t o  ha ndl e  whole  s y s t em   t og e th er .   Al so o n e  of  p r os  of  th is ar chit ectu r e,  th a t y ou  c o u ld  r edep loy  modu les  separ a t ely .  So  it  wil l  b e nice   t o   b e  able t o   c h ec k  modu les  separ a t ely  b e f o r e  th e y  wil l  b e  d eplo y ed .  This c o u ld  b e  ac h ie v ed  b y  c r ea ting h erme tic   en vir o n men ts  f o r  ea c h  modu le .  A ll   e xt ern al   d ep en d en ci es  sh ould   b e  r ep la ced  b y  mock s  or  s tu b s.  And   th en  y o u   c o u ld  t es t modu le  in iso la tio n .
Pr os  of t hi s  ap pr oach  ar e  ob vious:   y ou  c oul d f ul l y  c on t r ol  da t a  t ha t   i s  c oming  fr om  e x t er na l  dependencies  an d  y ou  c oul d use  moc k ed  da t ab ase  or   i n  me mory  da t ab ase  wit h some   t es t  da t a.   B ut   al so ther e  ar e  som e  aspec ts  th a t  y ou  should  t ak e in t o a ccou n t :  1. Th e fi r s t   one  i s  s tu b s i t sel f .   Y ou   sh ou l d  in v es t  some  amo u n t  o f  time  t o  implemen t s tu b s,  o r  f ak e ser vic es,  o r  f ak e h t tp   c li en t .   I t  c oul d  be  not  so  c ompli c a t ed  f or   one  t w o  si mple  e x t er na l  dependencies.   But  it  c o u ld  b ec o me  d isa s t er  if ser vic e  h as bu n c h   o f  d epen d encie s  and   eac h  of  th em  h as  c omp li c a t ed   b eh a vio r .   And it   wil l  be  e v en  mor e  c omple x  if   t hi s  mo du le   is   in  de v el o pmen t .   T eam wil l   ad d  dependencies  f as t er   t hen y ou  wil l   be  ab l e  t o  moc k   t hem. 2. Al so y ou  shoul d decide  h o w  y o u   wil l  implemen t moc k s .  As an  e x ample:  I s it m or e c on v en ien t  t o  d ep lo y  mod u le  with   f ak e h t tp   c li en t,  or  r eplac e so me ser vic e  c la ss  with  moc k .  Or  th e sec o n d   o p tio n   is t o   ru n   modu le  with  f ak e hos ts p ar ame t er s  and   s t art f ak e  ser vic es  as separ a t e  w eb s er vic es .   So  i t   wil l   send  r eques t s  t o  f ak e  se r vice s  in s t ead  r eal  one s.
So   th er e is a  lo t  o f  s t a ff t o  th ink  about  b e f o r e  y o u   wil l  s t art  b ac k  end   aut o ma tio n  on  y o u r  p r o jec t .  As  y ou   se e   i t   c ou l d  be  pre t t y  si mp l e , a nd   y ou   wil l   be ab l e   t o  del i ver   hu nd re ds  of  t e s t s  i n  t hre e  mo n t hs,  or yo u  c ou ld  spen d   h a lf  y ear  on   mo c k s  a n d   e v en n ot  s t a rt  wit h   t es ts . Y ou shoul d a sk  y our self :  1. Ho w  m y  t es ts will   g e t ac c ess t o  b ac k end ?  2. Am  I  able t o   auth en tic a t e ? 3. Ca n  I  c r ea t e o r  r equ es t  enough   t es t  u ser s ? 4. Am   I  able t o  man ag e  t es t  d a t a, or  t es t DB  sh ould   b e ad d ed ? 5. Ca n  I  u se  t es t  en vir o n men t ,  or   I   sh o u ld  d eplo y  app li c a tio n  f o rm  t es t fr ame w o rk ? 6. Shoul d  I  c r ea t e  an y  moc k s ?  As an e x ampl e  modul e  c oul d i n t er act   wit h pr od  da t a,  so  y ou  c ould not   use  r eal depe ndenc ies  in   t es ts  bec au se  of  sec urit y  c once r ns.  
The mos t imp o rt an t  c o n c ep tio n s : 1. Depen d ency  I n jec tio n   DI  \ I n v er sio n   o f  c o n tr o l  I o C 2. Moc k  Fr ame w o rk 3. Domai n Specif i c Lang ua g es  de v elopmen t 4. Code - g ene r a ti on 5. BDD  . . ? 6. The Sin g le  R es p on si b il it y   Pr inc iple ,  SR P
The mos t imp o rt an t  Desi gn   P a t t ern s: 1. Buil der 2. Flu en t  in t er f ace 3. Sin gl e t on 4. Ab s tr act F act ory 5. F act or y  Me t hod 6. Cr ea ti on  Me th od 7. Dec or a t or
The mos t imp o rt an t  idio ms: 1. Gener i c 2. R e f l ec t i on 3. La mbda
Dependency  i njecti on i s a te chni que  wher eb y  one  object  (or   s t a t i c  me t hod)  supp l i es  t he  dependencies  of  an oth er   object.   A  dependency  i s  an  object  t ha t   c an  be  used (a  servi ce ).  A n   injec tio n   is th e passing  o f  a  d ep en d en cy  t o a  d ep en d en t  object  (a c li en t)  th a t w ould   u se  it .   The  servi ce   i s  made  pa r t  of t he  cli en t 's  s t a t e.  P assi n g  th e ser vic e  t o  th e  c li en t,  r a th er  th an  al lo wing  a  c li en t t o   b u il d   o r  find   th e ser vic e,  is th e  fu n d amen t al   r equ ir emen t  o f  th e  p a t t ern . The in t en t  behind  depe ndenc y  in jec ti on is  t o  d ec oup le  objects  t o the  e x t en t  th a t   n o   c li en t  c o d e  h as  t o  b e c h ang ed  simply  b ec ause  an  o b jec t  it  d epen d s  o n   n eeds  t o  b e c h ang ed  t o   a diff er en t  o n e .  Thi s  per mit s  f o ll o wing t h e Open   / C lo sed  p rinciple .
Depen d ency  injec tio n  is o n e  f o rm  o f  th e br o ader  t ec h n iqu e  o f  in v er sio n   o f  c o n tr o l .   As wit h ot her   f or ms  of  i n v er si on of  c on t r ol,  dependency  i njecti on supp or t s  t he  d epen d ency  in v er sio n   p rinciple .  Th e cl ien t d eleg a t es  th e r esp onsib il ity  of  p r ovi d ing   its d ep en d en ci es  t o  e xt ern al  c o d e  ( th e  injec t o r) .   Th e c l i en t   i s not  al l o w ed  t o c al l   t he  i nject or   c ode;  i t   i s  t he  i njecti ng   c ode  t ha t   c ons t r ucts  t he  servi ce s  an d  c al l s  t he  cli en t   t o in ject t hem.  Th i s  me an s  t he cli en t   c ode  does  not   need  t o  k no w  ab out  th e  i njecti ng   c ode,   ho w  t o  c ons t r uct  t he servi ce s  or   e v en  which actual   se r vice s  it   is usi ng;   th e cl ien t  only  n eed s  t o  kn ow  about  th e  in trin sic   in t erf ac es  o f  th e ser vic es  b ec ause  th ese de fine  h o w  th e c li en t  ma y  u se  th e ser vic es .  This  separ a t es th e  r espons ibil iti es  o f  u se  and   c o n s tru c tio n .
Th er e  ar e  t hr ee   c omm on  me an s  f or   a cli en t   t o  acce p t   a  dependency  i njecti on:   se t t er - ,  in t erf ac e - and   c o n s tru c t o r - b ased  injec tio n .  Se t t er   an d  c ons t r uct or   i njecti on di f f er   mai nl y  b y  when  t he y  c an  be used.   I n t er f ace  inj ec ti on dif f er s in  th a t  th e  depe ndenc y  is g iv en  a chance  t o  c on t r ol  i t s o wn  i njecti on.   E a c h  r equir es   tha t  se par a t e co ns truc tio n  c o d e  ( th e  injec t o r)   t ak es  r espons ibil ity  f o r  in tr o d u c ing  a c li en t  and   its  d epen d encie s  t o  eac h  other .
Th e  Depen d ency  I n jec tio n   d esign  p a t t ern   sol v es  p r o b lems  l i k e : 1. Ho w  c an   an  app li c a tio n  or  c la ss b e ind epen d en t  o f h o w  its o b jec ts  ar e c r ea t ed ? 2. Ho w  c an  th e  w a y  objects  ar e  cr ea t ed   b e  sp eci fied  in s ep ar a t e  c o n figur a tio n   fil es ? 3. Ho w  c an   an  app li c a tio n  su p p o rt  d iff er en t  c o n figur a tio n s ? Cr ea t i ng   objects  di r ec t l y  wit hi n  t he class t ha t   r equi r es  t he  objects  i s  i n f l e x i bl e  bec au se  i t   c omm i t s  t he class to  pa r t i cula r   objects  an d mak es  it   impossibl e  t o c hang e  th e  in s t an ti a tio n  la t er  ind epe nden tl y  fr om   (wit hout   ha vin g  t o  change ) the cla ss.   I t  s t o p s th e  c la ss  fr o m  b eing  r eusab le  if o th er  o b jec ts  ar e  r equ ir ed ,  an d  it mak es th e c la ss h ar d   t o   t es t b ec ause  r ea l  o b jec ts  c an't  b e r eplac ed  with  moc k o b jec ts . A class  i s  no   l on ger   re spon si ble   f or  cre ati ng   t he ob je cts  i t   re qu i re s, a nd   i t   do e sn' t   ha ve  t o  del e ga t e  ins t an t i ati on   t o  a  f ac t or y   ob je ct  as  i n  t he  Ab s t ract  F ac t or y   desi gn   pa t t e rn.
Moc k - o b jec t,   moc ki n g : I n  object - or i en t ed  pr ogr amming ,  moc k  o b jec ts  ar e simula t ed objects  th a t m imic  th e b eh a vio r  of  r eal   objects  in c on tr ol led  w a y s ,  m os t of t en  as p art of   a  soft w ar e  t es ting in iti a tiv e .  A  pr ogr amme r   t ypic al l y  cr ea t es  a mock   object  t o   t es t th e beh a vio r  o f  some o th er  o b jec t .
Mock   objects  c an si mulat e t he beha vior  of c omple x ,  r eal   objects  an d  ar e  t her e f or e  use f ul   when  a  r ea l o b jec t  is  impr ac tic al  or  impossible  t o  i nc or por a t e  i n t o  a  t es t .   I f  an objec t  h as  an y  o f  th e f o ll o wing  char ac t eris tics,  it ma y b e  u se fu l  t o  u se  a moc k object  in  its  p la ce : 1. t he object  su p p li es n o n - d e t erminis tic r esults  (e . g.  th e c urr en t   t i me   or   t he cur r en t   t em per a t ur e) ; 2. i t   ha s  s t a t es  th a t ar e diffic u lt  t o  cr ea t e  o r  r epr o d u c e  (e . g.  a  ne t w or k   er r or ); 3. it   is  slo w (e . g.  a c om pl e t e da t aba se ,  which w ould ha v e  t o  be  i ni t i ali z ed  be f or e  t he t es t ); 4. it  d o es  n o t  y e t e xis t  o r  ma y chang e  b eha vio r ; 5. it  w o u ld  h a v e  t o  incl u d e  in f o rma tio n   and   me th o d s  e x c lusiv ely f o r  t es tin g p u rp oses (and not f or   it s  actua l  t ask ). 6. uses  th ir d - p arty  ser vic es
F or   e x ample , an  al arm cl o c k pr o gr am  whi ch c au ses a bell   t o  r i ng   a t   a  ce r t ai n  t i me   mig h t   g e t th e  c u rr en t  time  fr o m  a time ser vic e .  T o  t es t   t hi s,  t he  t es t   mus t  w ai t   un t i l   t he al arm  t i me  t o  k no w  whe t her   i t   ha s  r ung  the  bell   c or r ec tl y .   I f  a mo ck   ti m e  se r vice   is used in pl ace   of  th e  r eal  t i me  servi ce ,  i t   c an  be  pr ogr amme d  t o  pr o vid e  t he bell - r i ng i ng   t i me  (o r   an y  oth er   t i me ) r eg ar dl ess  of  t he r eal   t i me , so   t ha t   t he  al arm  clock   pr ogr am  c an  be t es t ed in   i solati on.
« T h e  Pr ag ma tic  Pr ogr ammer: Fr om  Journe yman  t o  Mas t er » 1. Domai n  Lang uages 2. The P o w er   of  Pl ai n  T e xt 3. Code   Generato rs a. P ass iv e b. Ac tiv e 4. Co de   That ’ s  E asy  t o   T est 5. Ubi q uito us  A uto m atio n 6. R uthl es s  T es ting
« Пр огр аммис т - праг ма тик   путь  о т  по дма ст ерь я  к ма ст ер у » 1. Яз ык и,  о тр аж аю щие   специ фи ку  пре дме тной  об ласт и 2. Преим ущ ество пр ос т ог о т ек ст а 3. Г ен ерат оры т ек ст ов прог рамм a. Пас сивны е i. Прие мле ма я  точность  ( пр ав к а  сгенери ро ванн ого  к ода   р ук ами) b. Активны е i. Часть про ц ес са  сборки 4. Программ а ,  к о т ор ую ле гк о т естировать 5. Вез д есу щая авт оматизация 6. Без ж алост ное т ест ировани е
T es t  P yr ami d :  d e finitio n «An  e f f ec t i v e t es t   au t oma t i on s t r a t egy c al l s  f or   au t oma t i ng   t es t s a t   t hr ee   di f f er en t   l e v els,  as sh o wn  i n  Fig ur e,   whi ch depict s  th e  t es t  aut o ma tio n  p yr ami d »
T es t  P yr ami d   with  p er c en t ag e  #1 UI  - 5% Acc ep t an ce   – 5% I n t egr a ti on  – 10% Unit   - 80%
T es t  P yr ami d   with  p er c en t ag e  #2 UI  – 1% End  t o  End  Flo w  – 4% W or k fl o w  API   – 6% I n t egr a t i on  – 9% Domai n Log i c Ac ce p t an ce   – 10% Unit   – 70%
T es t  P yr ami d :  Unit  T es ting • « Unit  t es ting  sh o u ld  b e  th e f o u n d a tio n   o f  a  sol id t es t aut o ma tio n   s tr a t egy an d a s  such r epr es en ts  th e  la r g es t p art  o f the  p yr ami d . Aut om a t ed unit   t es ts ar e  w onderful   bec au se  t he y  giv e sp ec ifi c   d a t a t o   a pr o gr ammer — t h er e  is a   b u g  and   it ’ s  o n   li n e  4 7 . » • « Unit  t es t s ar e  usua l l y  wr i t t en  i n  th e same l angu ag e  as th e  s y s t em ,  pr ogr amme r s  ar e  ofte n mos t   c om f or t ab l e  wr i t i ng  th em . »
T es t  P yr ami d :  Ser vic e • « Al t houg h I   r e f er   t o  t he  midd le  la y er  o f  th e t es t aut o ma tio n   p yr ami d   as  th e  ser vic e  la y er ,  I   am no t  r es tricti ng us t o using  only  a  se r vice - or ien t ed  ar chit ec tu r e.  All   ap pl i c a t i ons  ar e  made  up of  v arious  servi ce s.   I n t he w a y I’ m usin g  i t , a  servi ce   i s  some t hi ng  th e  ap pl i c a t i on  does  i n  r esponse  t o som e  i npu t   or   se t   of  i npu t s. » • « Servi ce - l e v el  t es t i ng   i s  ab out  t es t i ng   t he  servi ce s  of  an  ap pl i c a t i on  separ a t ely f r om  i t s  user   i n t er f ace. »
The r emai n ing  r o le  o f  u ser  I n t erf ac e  T es ts • “ But  don’t   w e  need  t o  do  some   user   i n t er f ace  t es t i ng ?   Ab solu t ely , but  f ar less  of i t   th an  an y  other   t es t  type. • I ns t ead,  w e  r un  t he majorit y  of te s t s (suc h  as b ound ar y t es t s)  t hr oug h t he  servi ce   l a y er ,  i n v ok i ng   t he me t hods  (se r vices)   di r ec t l y t o  c on f i r m  t ha t   t he fu ncti onal i t y  i s  w or k i ng   pr oper l y .   A t  th e  user   i n t er f ace  l e v el wha t ’ s le f t   i s t es t i ng   t o c on f i r m  t ha t   t he  servi ce s  ar e  hook ed  up  t o  t he r i gh t   but t ons an d t ha t   t he v al ues ar e  di spl a yi ng   pr oper l y  i n t he r esult   f i eld.   T o  do  t hi s  w e  need  a much  smal l er  se t   of te s t s t o  r un  t hr oug h t he  user   i n t er f ace  l a y er . • Whe r e  man y or g an i z a t i ons  ha v e g one  wr ong   i n  t heir t es t  au t oma t i on  e f f or t s  o v er   t he y ear s  ha s been  i n  i gn or i ng   t hi s  whol e  midd l e  l a y er   of ser vice  t es t i ng .   Al t houg h  auto m a t ed  uni t  t es ti ng  is w onder fu l,  it  c an  c o v er   only so   m uch o f  an ap pl ic a t ion’ s  t es t i ng   needs.  Wit hout  servi ce - l e v el  t es t i ng   t o  f i l l   t he  g ap   be t w ee n  uni t   an d  user   i n t er f ace t es t i ng ,  al l   oth er   t es t i ng   ends up  bein g  per f or me d  t hr oug h t he  user   i n t er f ace,   r esult i ng   i n  t es t s th a t   ar e  e xp ensiv e  t o  ru n ,  e xp ensiv e  t o  writ e,  and   b rit tle ”
Find   a bal ance 
HT TP C l i en t 62 TEST F RAME W O R K T e s t f r a m e w o r k X M L / J S O N p a r s e r
Le t s  t ak e  a l ook   on  t ools  t ha t   y ou  c oul d use  i n  t he  f r ame w or k .   Y ou  wil l   pr obab l y use  some   HT TP   c li en t .   Th er e  ar e  pr e t t y  bi g  v arie t y  of  cli en t s.   I f   y ou  need  y ou  c oul d use  A pach e  HT TP   c li en t .  I t   wil l   t ak e,   ma ybe m or e  e f f or t,  t o use   it ,  and  y ou  wil l  nee d  t o  wr ig h t mor e  c ode ,  but   y ou  wil l  be  ab l e  t o  f ul l y  c on t r ol  wha t ’ s g oin g  on.   Anot her   op t i on  i s  Spring  R es tT empl at e t o in t er act  wit h  w eb  servi ce .   Th i s  i s  a b i t   di f f er en t  le v el of   ab s t r act i on but   und er   t he  hood  y ou  c oul d  s t i l l   use  Apa che c l i en t .. 
Y ou  wil l   de f i ni t ely  need  some   u n it  t es ting  fr ame w o rk .  JUnit ,  T es ts NG ,  i t   doesn’t  r eal l y  ma t t er .   And y ou  al r eady k no w  ho w  t o  use it .   I f  y ou  w or k   wit h se r vice   th a t  se nd  da t a  in   JSON f orm a t ,  t hi s  is   r eal ly  ni ce  li br ar y “ goo gle - gs o n ” .   Y ou  c ou l d u se  t hi s  l i br a r y t o  c on v ert  jsons i n t o  da t a  t r an s f er   objects.   Or   cr ea t e  json f r om  s t r i ng   or   dto .   I f   y ou  use  x ml,  y ou  c oul d use  f as t er  xml   l i br ar y .   T o  c o mp ar e  JSON’ s  I  sug g es t  t o use   JSONAs s e rt li br ar y .  It   c oul d  c ompar e  J SONs  wit h di f f er en t   sor t i ng   or der ,  or   y ou  c oul d sk i p  f i elds  f r om  c omparison.  As  an   e x ampl e y ou  cr ea t e  ne w  user ,  an d w an t   t o  v al i da t e  t ha t   w eb  servi ce   r e t urns  e x act l y  t he  same r esponse  t ha t   y ou  nee d,  but  y ou  c an  not  pr edict  som e  g ene r a t ed  v al ue in  r es ponse ,  li k e  user   i d.   So  y ou  c oul d sk i p pa r t i cula r   v al ue fr om  c omparison  an d  chec k   separ a t ely , that  i t   ha s pr oper   f or ma t   or   chec k   i t  wit h  r egul ar  e x pr ession.  
I f   y ou  ar e  g oin g  t o  moc k some  pa rt s  of  modu l es,  y ou   c ou l d u se  Mockit o .  Thi s  i s r eal l y nice t ool . So  wha t a lso I  ha v e  her e.  Fiddler .   Thi s  is  r eall y  not  th e  part of t he  f r ame w or k ,  but   separ a t e  tool,  t hat   w ork s  a s p r o x y ,  a nd   i t  c a p t ur es  sessions  an d  help s y ou  t o  debug   r eques t s   / r esponses  durin g  f r ame w or k   i mpleme n t a t i on.   Al so th i s  i s r eal l y use f ul   t o  be abl e t o sa v e  c ap t ur ed  sessions.   So  i f   some t hi ng   w or k s  not   as e x pect ed  y ou  c oul d  jus t sa v e  se ssion  and  se n t  it   t o de v elope r s.  Thi s  is  r eall y  c on v enien t.  So   I   w oul d sa y that  f i ddl er   i s jus t  mus t   ha v e if   y ou  w or k   wit h ba ck end . P os tman ,  i t  c oul d  be  use f ul   t oo ,  t hi s  i s  ni ce  ap pl i c a t i on  wit h f r i endl y U I   whe r e  y ou  c ould cr ea t e  r eque s t,  e x ec ut e  it   and  che ck   r es ponse . And t he la s t   one  t ha t   I  w oul d men t i on i s  RE S T  A s s ured .   Th i s  i s  t es t   au t oma t i on  f r ame w or k ,  t ha t   ha s  h t t p cli en t   an d  a  l ot of   s yn t a x   sug ar ,  th a t  al lo w s y ou  t o c r ea t e  eas y  r eadabl e  t es ts i n  Gh er k in   s tyl e.
Main too ls • XM L  /   JS ON  parse r • H TT P - cl ient • U nit - T est in g  Fram ew ork
Additional too ls • Mapp in g  tool • “Query”  tool
S P RING 68
Le t s  t ak e  a closer  l ook .   R es t  t emp la t e .   T o  use  i t   y ou  e v en  don’t   need  t o  bring   al l   Spring   s t uff  i n t o  t he fr ame w or k   jus t  cr ea t e  R es t T em pl a t e ()  i ns t an ce .   And  y ou  ar e  g ood  t o g o . Y ou c oul d specif y in wha t   t ype  y ou  w an t   t o  map r esponse  body .   T o  sen d   p o s t r equ es t  me th o d   p o s tF o rOb jec t c o u ld  b e us ed .   J us t  specif y  URI ,  ad d  p a ylo ad an d  select  i n  wha t   typ e  y o u   w an t t o  r ec eiv e  th e  r espons e .  Y ou c oul d map i t   t o  some   D T O  object  an d t ha n  chec k   some   f i elds.
S P RING 70
Le t   mak e  i t   a b i t   mor e  c ompli c a t ed,  y ou  c oul d use  HT TP   en tity t o  add   h ea d er s,  o r  y o u   c o u ld  p ass so me  v aria b les t o   u ri .  Li k e  i n  t hi s  e x ampl e:  I   ha v e  I D  p ar ame t er  her e,   an d  r es t   t em pl a t e  wil l   r epla c e i t wit h  p ar ame t er  fr om  me th od  c al l .   Her e  I D  wil l  be  r eplaced  wit h one   hun dr ed.
U sag e
Cor e
D at a Ge n er at ors
T es t s
TEST F RAME W O R K 76
W or k i ng   wit h  A p ac h e  HT TP   i s  pr e t t y  si mple  t oo.  Y ou shoul d ha v e  ins t ance o f  c li en t  i t self ,  an d  cr ea t e  ins t ance o f  r equ es t .  Tha n  y ou  jus t   r e trie v e r espons e  and   c h ec k s  th a t th e en tity c o n t en t  is fully c o n su med   and   th e c on t en t  s tr eam,  if e xis ts,  is  cl osed .  I t   c oul d be  use f ul   t o  ha v e  r espons e  wr app er  in th is  c ase tha t will   h a v e  e xit  c o d e,  r espons e  b o d y  as String ,  R esponse  body  as obj ec t ,  Ex ce p t i on  i f   an y oc cur r ed  durin g r eques t   e x ec ut i on, and  if   y ou  w an t   t o c heck   t ha t   r eque s t  pr oc es sing   ti m e,  y ou  c ould ha v e  th is  par ame t er   t oo. 
TEST F RAME W O R K 78 P O J O JSON JSON P O JO C u s t o m   D T O   or c om. g o ogle . g s on.J s o nEl e m en t
I n  mos t   c ases  y ou  wil l   w or k   wit h  JSON s an d  Go o gle - GSO N i s r eal l y  g ood  op t i on  her e.  Th er e  a t   l eas t   t her e  ap pr oaches  ho w  t o v al i da t e  r esponses.  The fi r s t  one   is  t o  c on v er t  J SON  in  t o D T O ,  or   PO J O  objec t.   PO J O  is  k in d  of  object  t ha t   doesn’t  ha v e an y logi c,   i t   c on t ai ns onl y da t a, an d  g e t t er s  wit h se t t er s.   I n  cur r en t   e x ampl e  y ou  c oul d see,   t ha t   I  e x pect f i eld  user _log i n wit h  und er sc or e.  So  I  shoul d specif y w hi ch v al ue  shoul d  be  mapped to  user   log in   par ame t er .   I n  c ase  if   v al ues  ar e  th e  same ,  as an  e x ample y ou  ha v e  c amel c ase  i n  gson ,  so  y ou  e v en  c oul d  g e t   r i d  of t hi s  an not a t i on, and   g oogl e  gson wil l   au t oma t i c al l y  map  json f i elds  t o pr oper   v al ues.   Con v er t i ng  objects  f r om  PO J OS  t o  J SON  s t r i ng s  i s r eal l y e as y .  I  jus t  need  tw o  m e th ods  t o  json and  fr om   json .
And  one  mor e  t hi ng .   I f   y ou  don’t   w an t   t o  cr ea t e  or   g ener a t e  D T O  cla sses,   y o u   c o u ld  map  y o u   c la ss t o   JSON  ele men t .  I t will   r ec u r siv ely  g o   tr o u t  JSON   and   ins t an tia t e  o b jec t  th a t r epr esen ts  y o u r  JSON .  So   th en   it c ould   b e  u sed   with  JSON  assert  libr ar y  t o  c omp ar e  it  with   another  JSON .  Th i s  i s  much  mor e  c on v enien t  th an   c ompar e  t hem  as  Strin gs, an d doesn’t  r equi r e  g ener a t i on of  PO J O  cla sses.
Mapping
REST ASSURED 82
And I   wil l   f i ni sh  t hi s  w or k shop  wit h short  descr i p t i on  of r es t   assu r ed  f r ame w or k .   I t p r e t ty  ea s y t o  u se  and   fle xible  enough .  R es t  A ssu r ed  p r o vide  t o n s  o f  s yn t a x  su g ar ,  wh ic h   h elp s  t o  cr ea t e  simple and   ea s y  u n d er s t and able  t es ts . T o  use  r es t   assu r ed  y ou  shoul d  s t a tic al l y  import  R es tAssur ed and   Ma t c h er s  c la sses .  T o  sen d   g e t r equ es t  y o u   …  jus t  c al l  “ g e t ” me th o d   and   sp ec if y  u ri .  Fr om  one  si de R es t   assu r ed  mimics  Gherkin s yn t a x ,  wit h i t ’ s  gi v en when  th en c ons tructions.   Fr om   anot her   it  look s  li k e  b u il d er ,  whe r e  al l  me t hods  g o one  a f t er  an oth er   separ a t ed b y  c omm a. I n   th is  e x ample I  se t b asi c   auth en tic a tio n ,  th en  send   GET  r equ es t  and   v al ida t e  s t a tu s c o d e.   W or d  when  i s  s yn t a x   sug ar ,  so  I  e v en  c an   sk i p i t   i n  th is  c ase. Also r es t  assur ed  pr o vide  f e w  s tr a t egies  of  r eq u es t  v al ida tio n . I n   th is  e x ample c o d e  send s  GET  r equ es t  wh ic h   r e tu rn s  r espons e  b o d y ,  and   th en  v al ida t e  it.
REST ASSURED 84 SET TI NG P A R A M E T E R S R E Q U E S T E X E C U T I O N R E SPO NSE V A L I D A T I O N S     R E SPONSE O B J E CT wi t h().
Ful l   R es tAssur ed t es t  s tru c tu r e  l ook s  l i k e  t hi s.   Lik e  in C u c u mb er  R es tAssur ed h as  Gi v en When   Then   k e y w o r d s .  GIVEN  secti o n  is r espons ible  f o r  c o n figur ing  th e  r equ es t . I n  t his  se c ti on   we c ou ld  specif y  h ead er s,  re q u es t bo d y   or  a n y   R es tAs sured pa rame t e rs   l i k e   pro x y ,  ssl se cu ri t y  s t aff  a nd   so  on . W HEN   sec tio n   r epr esen ts  HT TP  me th o d   and   URI .  I n   THEN  secti o n  g o es al l  v al ida tio n .  Al so y ou   c ou l d  e x t ract r e spon se   bo dy ,  an d  per f orm  va l i da t i on   wit h  t e s t   f rame work ,  i ns t e ad  va l i da t i ng   i n  re s t  Assure d . I   wil l  desc r ib e  eac h  se ction  in   m or e  de t ai l.
REST ASSURED 86 S e t  quer y p a r a m e t e r s     Se t   pa t h p a r a m e t e r s Hea ders se ct ion Add bod y
So ,  wha t  ab out  Gi v en  sec t i on . I n  Gi v en  Sect i on   y ou   c a n  sp ecif y pa r a me ter s  f or  URI  or  qu er y  par ame t er s,  it  m ak es   t es ts  m or e  fl e xi bl e,   and  c ode   m or e  r eusable. Al so , thing s  l i k e   Cookies and  Hea de rs shou l d  be speci f i e d  i n  given  se cti on .   Ne xt  o n e  is   BOD Y ,  wh er e  re q u es t bo d y   c ou ld  b e s p ecif ie d .   This  is  m os t  widel y  u se d  me t ho ds  f rom g i ven  se cti on .   I n   g ener a l,  gi v en  me tho d  a ll o w s  t o  se t a ll   neede d  r eques t  da t a , an t  se t  fr ame w ork  p ar ame t er s  b e f or e  r eq u es t  will   b e  issu ed .
REST   ASSU R ED . URI 88 B y   d e f a u l t   R E S T   ass ur ed   ass um es   hos t  lo c alh o s t   an d  port  80 80   whe n  doing   a r eq ue s t. Y o u   c an   chan g e  the   d e f a u l t   base  U RI ,  base  pa th,  po rt  f o r   all    s u b s e q u e n t r eq ue s ts:
B y d e f ault  th e  R es tAssur ed send s  c al l  t o  lo c al h o s t  if no  p ar ame t er s  w ar e  p r o vided. So  i n t hi s  e x ampl e  r es t   assu r ed  wil l   send  r eques t   t o  l oc al l hos t wit h port  8 0 8 0 ,  which is actua ll y  de f aul t  t om c a t port. Y ou  c an  change  de f au l t   ba se  uri , pa t h  an d port  b y  se t t i ng   t hese  v al ues  i n  ba se c l ass, an d t hen use  i n  al l   t es t s, and  y ou  wil l   ha v e a possi bi l i t y  t o  change  U RI f or   al l   t es t  fr om  one  pl ace,  whi ch is g ood  i f   y ou  t es t i ng   on  di f f er en t en vir onm en ts  which ha v e  di f f er en t  U RI.   Th e same   wit h  ba seP a t h . I n  pr o vid ed  e x ampl e,  if   I   specif y  ba seU RI an d ba se P a t h,  t he  r eques t   wil l   be  sen t  t o  BaseU RI +  BaseP a t h +  Ur i P ar ame t er .
REST   ASSU R ED . URI 90 P a t h   p a r a m e t e r s   m a k e s   it  ea sie r   t o   r ea d  the   r e q u e s t   pa th  as  w ell  as  en ab ling   the   r e q u e s t   pa th  t o   ea sily   be     re - us ab le  in  m a n y   t es ts   wit h  d i f f e r e n t   p a r a m e t e r v alu es . Named p a r a m e t e r s U nna med p a r a m e t e r s L i k e S tr ing. f o r m a t
Th er e  f e w  w a y s ho w  t o  mak e  UR I   v al ue  mor e  f l e x i bl e  or   c on f i gu r ab l e.  Y ou c oul d use  “ n amed  p ar ame t er s ” .   So  r es t   assu r ed  wil l   chec k   pa r ame t er   na me   an d  r epla ce   i t   wit h c or r espondi ng   v al ue.   Or  y o u   c o u ld  sp ec if y  p ar ame t er s  d ir ec tly in c al l  me th o d .   Th e y  c al l   i t   u n n amedP ar ame t er s . Y ou  c oul d mix   t hi s  t w o  t ypes  of  pa r ame t er s.   So  I n t hi s  e x ampl e:  f r ame w or k   wil l   chec k   t ha t :  ok   I  ha v e in  r eques t   specif i c a t i on  t hi s  pa r ame t er ,  so  I   wil l   r epla ce   i t   wit h specif i ed v al ue.   Th en it   chec k s,  ok   I  don’t  ha v e such par ame t er   na me   i n  r eques t   spec if ic a ti on,   but  I ha v e  one   e xt r a  par ame t er   in   th e  r eque s t  c al l,  so  I   wil l   r epla ce   i t   wit h t hi s  pa r ame t er .   And a l so par ame t er s  c oul d be  pa ssed th e same   as i n  s t r i ng   f or ma t   an d  R es tAssur ed wil l  r eplace  th em  one   b y  one .
REST   ASSU R ED . HEADER S. 92 Mult i - v a lu e hea d e r s Headers Hea ders s h or t c u t s
Th en y ou  wil l   pr obab l y  need  t o se t   h ea d er s  and   c o o ki es .  Me th o d   h ea d er  wil l  c r ea t e single header   i f   i t   w as c al l ed  wit h   tw o   p ar ame t er s and  mu ltiple h eader s  wit h th e  same   name   if   y ou  pass  th r ee  o r  mor e par ame t er s .  Me th o d   h ea d er s  c r ea t es h ea d er s  fr o m  p ai r s  o f p ar ame t er s .  Al so th er e  ar e  sh o rt c u ts  f o r  fr equ en tly  u sed  h ea d er s .   Actua l l y  und er   th e  hood  th e y  ar e  c al li ng  header ()   m e th od.  B ut   w ell, th es e  guy s r eall y  w an t   t o  mak e  our  c ode  si mple.
REST   ASSU R ED . HEADER S. 94 Ju s t   l i k e hea d e r s De t ai le d   c ookie bui ld er
Pr e t t y  t he  same i d ea   with  c o o ki es .   Y ou c oul d se t   si ng l e  c ook i e,   or   mult i   v al ued c ook i e.  I n t hi s  c ase r es t   assu r ed  wil l   cr ea t e  mu lti p le coo ki es  with  th e  same name and   d iff er en t  v al u e s . Al so if   y ou  need  mor e  fle xibili ty  in  c o o ki es ,  y ou  c oul d use  c o o ki eB u il d er , w hi ch al l o w s  y ou  t o  se t all   c o o ki e  p ar ame t er s .
REST   ASSU R ED .   SPEC B UIL D ER . 96 I n s t e a d   du plic a ting   r es pons e  e xpe ct a ti o ns   an d/ o r     r e q u e s t   p a r a m e t e r s   f o r   d i f f e r e n t   t es t s  y o u  c an   r e - us e    an   e n tir e sp e cif ic a t io n . T o   do  this   y o u  de f ine   a  sp ecif ic a t io n   us ing   eith er   the     R eq ue s tSpecB ui lde r or R e s p o n s e S p e c B u i l de r .
Se t ting  th e  same v al u es f o r  mu lti p le r equ es ts  d o esn’ t  h a v e  an y sen se .  T o  r edu c e  c o d e  d u p li c a tio n ,  R es tAssur ed fr ame w o rk  h as  R equ es t  sp ec ifi c a tio n .  Ho w  i t   w or k s:  y o u   c r ea t e r equ es t  sp ec ifi c a tio n  b u il d er  and   add   al l  p ar ame t er s  li k e  h ea d er s  and   c o o ki es.   A n d   th is speci fica tio n  c an  b e  u sed  in GI VE N sec tio n .  B ut   y ou  c an  add   add itio n al  p ar ame t er s  in g iv en   sectio n ,  or  e v en   o v errid e  v al u es  fr o m  r equ es t  sp ec ifi c a tio n .  The same s t a ff  with  R espons e  sp ec ifi c a tio n .   I t   c oul d be  used  t o app l y  som e  se t  of  v al id a tio ns  f or   al l  r es ponse s. 
REST   ASSU R ED .   OBJ ECT MAP PING . 98
And f e w  w or ds  ab out   r equ es t/r espons e o b jec ts  mapp ing .  I n  r es t   assu r ed  i t   i s r eal l y e as y .  Le t s  assu me   t ha t   I   ha v e  cla ss  Message   wit h r eque s t  da t a.   So  whe n  I   pass it   in   gi v en s ec ti on  wit h  body  me ssage ,  R es t Assur ed wil l  seria li z e  th e  o b jec t  t o  JSON   si nce  t he  r eques t   c on t en t - t ype is se t   t o " app li c a tio n / jso n ".  It  wi l l   f i r s t   t r y  to use  Jack son   i f   f ou nd  in   cla sspa t h an d i f   not   Gson wil l   be  used. I f  y ou  chang e  th e  c on t en t - type t o  " app li c a tion/ xm l "  R E S T  Assur ed  wil l   ser i al i z e t o  XML u si ng   J AXB.  I f   no  c on t en t - t ype is de f i ned  RE S T  Assur ed  wil l   t r y  t o  ser i al i z e  i n t he f oll o win g or der . Y ou  c an al so c r ea t e  a  J SON  doc ume n t  b y  su p p lying  a Map   t o  RE S T  Assur ed.
REST   ASSU R ED .   RESP ON SE V A L I D A T I O N . 100
And t hen r esponse  v al i da t i on.   Th er e  ar e  f e w  w a y s.   I t  c oul d  be  v al i da t ed  wit h  R es t Assur ed : St a tu sC ode ob vio u sly  v al ida t es  s t a tu s  c ode ,  and  b ody  me th od  t o  v al ida t es  r espons e  b o d y .   Fi r s t  pa r a me ter   i s  JSON  pat h,  a nd   sec on d  i s  assert . OR  …   y o u   c o u ld  e xtr ac t r espons e  o b jec t  and   v al ida t e  with  it .   Ex t r act   m e th od  wil l  r e tu r n  R es ponse   objec t,  which c an  be  passed t o  T es tNg asserts,  an d  v al i da t ed  her e.  And t he  th ir d   w a y  is map  r espons e  t o   D T O  o b jec t ,  an d  w or k   wit h i t .
REST   ASSU R ED .   J SON SCHEMA . 102
And e v en  i f   y ou  w an t  t o  v a li da t e  wit h  JS ON  sc hema ,  i t   c oul d be  done  i n  R es t Assur ed .   J us t   s t a t i c al l y  i mpor t   JsonSc h emaV al ida t o rC la ss ,  an d  ad d  ne w  dependency  i n  pom.x ml  an d  y ou  ar e  g ood  t o g o. 
REST   ASSU R ED .   P R O X Y .   SS L . L OG GIN G . 104
A t  th e e nd:  f e w  r eal l y  i mpor t an t  th i ng s  t ha t   I  ha v en’t me n t i oned  y e t . R es tAssur ed h as  b u il t in  me th o d s  t o  p erf o rm  auth o riz a tio n .   I f   y ou  wil l   spec if y  m e th od  basi c  in   gi v en  se ction,  R es tAssur ed wil l  tr y  t o  au t hen t i c a t e  wit h pr o vid ed  l ogi n/pass w or d.   Pr ee mp t i v e  me t hod  sho w s  t ha t   R es t Assur ed wil l   t r y  t o  au t hen t i c a t e  e v en  be f or e  i t   wil l   be  r eques t ed  f or   au t hen t i c a t i on.   Also  if  y ou  w an t t o  see y ou  r eq u es ts  in  fidd ler ,  and   I  assu me  y ou  w an t,   y o u   sh o u ld  se t p r o xy  v al u e .   B y  de f au l t   Fid dl er   w or k s  on  eig h t y - eig h t   on  eig h t y - eig h t port.  Also ,  i t   uses  self  sig ned  ce r t i f i c a t e,  whi ch wil l   be  r ec ogn i z ed  as u nsa f e.  T o  deal   wit h i t .   Y ou  c oul d cr ea t e  cus t om  k e y  s t or e  and  use  it   in   y our   t es ts, or   y ou  c ould ju s t  tu r n  of f ce r ti fi c a t e  v al id a ti on  when  y ou  w or k   wit h f i ddl er . Al so  R es tAssur ed h as  p r e t ty  g o o d   lo g ging  fu n c tio n al ity .
Ma v en   d epen d en c y
D epen d en c y
Usage
Us age
R es t  Ass ur ed  – please  pa y sp ecial  a t t en tion  t o  ne xt f ea tur es: a.  Comple x pa r sin g  and  v al ida tion i .  Gr oo v y ’ s c oll ection A PI ii . bod y( " shoppi ng.c a t eg or y . find {  it.@ typ e == 'gr oceri es'  } .it em ",  hasIt em s ("Ch oc ola t e", "Cof f ee")); ii i.  fr om(r esponse). g e tLis t (" shopp ing.ca t eg or y . find {  it.@ typ e ==  'gr oceri es ' } .i t em "); iv . ge t("/ shopp ing ").pa th( " shoppi ng.c a t eg or y . find {  it.@ty p e ==  'gr oceri es' }.it em ");
b. Deseria liz a tion with  Generics i . Lis t<Map<S tri ng ,  Ob ject>> ii .  io. r es t ass ur ed.mapper . T ypeR e f c.  Cus t om   p ar s er s i .  R es tAssur ed. r egis t erP ar ser (< c on t en t - typ e>,  <par ser >); ii .  R es tAssur ed. r egis t erP ar s er ("ap plic a tion/ vnd.uoml+ xm l ",  P ar se r .XML); ii i.  R es tAssur ed. un r egis t erP ar ser ("ap plic a tion/ vnd.uoml+ xm l ");
d.  Spec ific a tion  R e - use i .  R eques tSp ecB u ild er ii .  R esponseS p ecB ui lder e.  Filt er s i .  io. r es t ass ur ed. fil t er .Fil t e r ii .  io. r es t ass ur ed. fil t er .lo g.R eques tLog ging Filt er ii i.   io .r es t ass ur ed. fil t er .l og.R es ponseL og gingF il t er iv .  io. r es t ass ur ed. fil t er .lo g.E rr orLog ging Filt er f .  Sess ion  su pp ort
g.  Spring  Sup port i . S pring  Moc k  Mv c Mod ule ii . Boots tr app ing  R es tAssur edMoc kMv c ii i.  R es tAssur edMoc kMv c iv . As yn ch r onou s  R eques ts v .  Sp ri ng   MVC  Au then tic a tion iv .  R es tAss ur edW ebT es tCl ie n t h. Sc ala i . K otli n
S um m ar y If   y o u   w a n t   t o   s t a r t   aut oma t i o n   – s t a r t   f r o m API. • A lot of logic c an be v a l i d a t e d t r o u g h API wi th ou t being depen d en t   up on  the UI. • W e b ser vi ce is n ot tie d to a n y o ne o p e r a ti n g s y s t e m or p r o g r a m mi n g lan g ua g e , so w h y s h o ul d t he f r a m e w o r k be w r i t t e n in the sam e langu ag e? • API  t e s t s   a r e   E A S Y   TO A U T O M A T E , C H E A P a n d   F A S T . 114