junit4 on one page (synoptique junit)

2
 No need to extend !"#$%&#" Fixture objects used by many tests executes before each test t o set up common test data Naming convention for test case classes a test method check the expected value against the computed value more exible assertions  JUnit reports unexpected exceptions check that code throws the desired exception naming convention for expected exception check any boolean condition © 2011 J. B. Rainsberger, all rights reserved  JUnit 4 on one page '&()&*" (&,-./&01#,-210$3,$"#$4 05'6/$ #$&$0( 6/*,7&5(/"#$,8&$(7"/#,"92&:!64 05'6/$ #$&$0( 6/*,-210$,;##"/$,<4 05'6/$ -&=&,2$0:,<4 05'6/$ -&=&,2$0:,/"*">,?&$$"/14 05'6/$ 6/*,-210$,<4 '2.:0( (:&## @"::A1"B$"5!"#$ C  '/0=&$" D0#':&E F0#':&E4  '/0=&$" @&:" #&:"4  GH"I6/"  '2.:0( =60F J0/"@&:"!6D0#':&EKL C  F0#':&E M 1"J D0#':&EKL4  #&:" M 1"J @&:"KF0#':&EL4  N O  G!"#$  '2.:0( =60F '/6F2($P621FKL $7/6J# Q>("'$061 C  #&:",61H&/(6F"KRSTU3VRL4  &##"/$Q92&:#KRWX,YVRZ F0#':&E,*"$!">$KLL4  N  G!"#$  '2.:0( =60F '/6F2($[6$P621FKL $7/6J# Q>("'$061 C  #&:",61H&/(6F"KRYYYYYRL4  &##"/$!7&$KF0#':&E,*"$!">$KLZ  "92&:!6KR[6 '/6F2($ I621F I6/ YYYYYRLL4  N  G!"#$  '2.:0( =60F $/E\#01*]/62'#H"I6/"B1=6)01*8&$ (7"#KL  $7/6J# Q>("'$061 C  $/E C  ?&$$"/1,(65'0:"KRK^^F^^FLK^^FLRL,5&$(7"/KRX_TRL  ,*/62'KSL4  I&0:KR`62 /"&F & 5&$(7 */62' ."I6/" 5&$(701* $7" /"*">abRL4  N (&$(7 KB::"*&:@$&$"Q>("'$061 ">'"($"FL C  N  N  G!"#$  '2.:0( =60F /"*2:&/Q>'/"##061#8&$(7KL $7/6J# Q>("'$061 C  &##"/$!/2"K?&$$"/1,5&$(7"#KR^^F^^F ^^FRZ RX_TRLL4  &##"/$P&:#"K?&$$"/1,5&$(7"#KR^^F^^ F^^FRZ R-./&01#RLL4  N  G!"#$  '2.:0( =60F I01FB$"5B1%6::"($061KL $7/6J# Q>("'$061 C  c0#$dD0#':&Ee F0#':&E# M 1"J ;//&Ec0#$dD0#':&EeKL4  F0#':&E#,&FFKF0#':&EL4  &##"/$@&5"KF0#':&EZ F0#':&E#,*"$KfLL4  N  G!"#$  '2.:0( =60F :0#$%&1@$6/"[2::g"I"/"1("#KL  $7/6J# Q>("'$061 C  c0#$dA.-"($e :0#$ M %6::"($061#,dA.-"($e #01*:"$61c0#$K12::L4  &##"/$[6$[2::K:0#$L4  &##"/$[2::K:0#$,*"$KfLL4  N  '2.:0( =60F $70#B#[6$;!"#$H"(&2#"B$h&#[6;11 6$&$061KL  $7/6J# Q>("'$061 C  I&0:KR!7" $"#$ /211"/ J61i$ /21 5",RL4  N  G!"#$  '2.:0( =60F $70#B#[6$;!"#$H"(&2#"B$h&#;?&/& 5"$"/K  G@2''/"##j&/101*#KR212#"FRL A.-"($ '&/&5"$"/L  $7/6J# Q>("'$061 C  I&0:KR!70# 5"$76F /"#2:$# 01 &1 "//6/ 01 $7" $"#$ /211"/,RL4  N  G!"#$  '2.:0( @$/01* $70#B#[6$;!"#$H"(&2#"B$g"$2/1 #;k&:2"KL  $7/6J# Q>("'$061 C  I&0:KR!70# 5"$76F /"#2:$# 01 &1 "//6/ 01 $7" $"#$ /211"/,RL4  /"$2/1 RI&0:R4  N N '&()&*" (&,-./&01#,-210$3,$"#$4 05'6/$ -210$,I/&5"J6/),!"#$%&#"4 05'6/$ 6/*,-210$,01$"/1&:,/211"/#,l\10$Um%:&##g211"/4 05'6/$ 6/*,-210$,/211"/,g21j0$74 Gg21j0$7Kl\10$Um%:&##g211"/,(:&##L '2.:0( (:&## g21c"*&(E!"#$#!"#$ ">$"1F# !"#$%&#" C  '2.:0( =60F $"#$;16$7"/?/6F2($P621FKL $7/6J# Q>("'$061 C  I01&: D0#':&E F0#':&E M 1"J D0#':&EKL4  I01&: @&:" #&:" M 1"J @&:"KF0#':&EL4  #&:",61H&/(6F"KRTU3V_RL4  &##"/$Q92&:#KRWST,VfRZ F0#':&E,*"$!">$KLL4  N N check that the expected and computed result are the same physical objects check that references are null or not null these are not tests Run JUnit 3 tests, if you already have them 1. create some objects (“Arrange”) 2. invoke a method (“Act”) 3. check the result (“Assert”)

Upload: antonsandu68

Post on 05-Nov-2015

218 views

Category:

Documents


0 download

DESCRIPTION

JUnit4 on One Page (Synoptique Junit sur une page)

TRANSCRIPT

  • No need to extend TestCase

    Fixture objects used by many tests

    executes before each test to set up common test data

    Naming convention for test case classes

    a test method

    check the expected value against the computed value

    more flexible assertions

    JUnit reports unexpected exceptions

    check that code throws the desired exception

    naming convention for expected exception

    check any boolean condition

    2011 J. B. Rainsberger, all rights reserved

    JUnit 4 on one page

    package ca.jbrains.junit4.test;

    import static org.hamcrest.Matchers.equalTo;import static org.junit.Assert.*;

    import java.util.*;import java.util.regex.Pattern;

    import org.junit.*;

    public class SellOneItemTest { private Display display; private Sale sale;

    @Before public void wireSaleToDisplay() { display = new Display(); sale = new Sale(display); } @Test public void productFound() throws Exception { sale.onBarcode("12345"); assertEquals("$7.95", display.getText()); }

    @Test public void productNotFound() throws Exception { sale.onBarcode("99999"); assertThat(display.getText(), equalTo("No product found for 99999")); }

    @Test public void tryUsingGroupsBeforeInvokingMatches() throws Exception { try { Pattern.compile("(\\d\\d)(\\d)").matcher("762") .group(1); fail("You read a match group before matching the regex?!"); } catch (IllegalStateException expected) { } }

    @Test public void regularExpressionsMatch() throws Exception { assertTrue(Pattern.matches("\\d\\d\\d", "762")); assertFalse(Pattern.matches("\\d\\d\\d", "jbrains")); }

    @Test public void findItemInCollection() throws Exception { List displays = new ArrayList();

    displays.add(display);

    assertSame(display, displays.get(0)); }

    @Test public void listCanStoreNullReferences() throws Exception { List list = Collections. singletonList(null); assertNotNull(list); assertNull(list.get(0)); }

    public void thisIsNotATestBecauseItHasNoAnnotation() throws Exception { fail("The test runner won't run me."); }

    @Test public void thisIsNotATestBecauseItHasAParameter( @SuppressWarnings("unused") Object parameter) throws Exception { fail("This method results in an error in the test runner."); }

    @Test public String thisIsNotATestBecauseItReturnsAValue() throws Exception { fail("This method results in an error in the test runner."); return "fail"; }}

    package ca.jbrains.junit4.test;

    import junit.framework.TestCase;

    import org.junit.internal.runners.JUnit38ClassRunner;import org.junit.runner.RunWith;

    @RunWith(JUnit38ClassRunner.class)public class RunLegacyTestsTest extends TestCase { public void testAnotherProductFound() throws Exception { final Display display = new Display(); final Sale sale = new Sale(display); sale.onBarcode("23456"); assertEquals("$12.50", display.getText()); }}

    check that the expected and computed result are the same physical objects

    check that references are null or not null

    these are not tests

    Run JUnit 3 tests, if you already have them

    1. create some objects (Arrange)

    2. invoke a method (Act)

    3. check the result (Assert)