Upgrade to Pro — share decks privately, control downloads, hide ads and more …

まだまだ間に合うJUnit(再)入門 #jjug_ccc #ccc_g4

yusuke
November 23, 2019

まだまだ間に合うJUnit(再)入門 #jjug_ccc #ccc_g4

yusuke

November 23, 2019
Tweet

More Decks by yusuke

Other Decks in Technology

Transcript

  1. ࠓճͷςετର৅'J[[#V[[ public class FizzBuzz { public static void main(String[] args){

    for(int i=0;i<100;i++){ System.out.println(fizzBuzz(i)); } } public static String fizzBuzz(int number) { // ͜͜Λ࣮૷ } }
  2. import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class FizzBuzzTest { @Test void

    fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); } } Ξϊςʔγϣϯ Ξαʔγϣϯϝιου +6OJUςετͷେࣄͳೋཁૉ
  3. import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class FizzBuzzTest { @Test void

    fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); } } @TestΞϊςʔγϣϯ ςετΛߦ͏ϝιουͩͱએݴ
  4. import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class FizzBuzzTest { @Test void

    fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); } } assertEquals(a, b) b͕aͱ౳͍͜͠ͱΛओு
  5. όά͍ͬͯΔ࣮૷ public class FizzBuzz { public static void main(String[] args){

    for(int i=0; i<100; i++){ System.out.println(fizzBuzz(i)); } } public static String fizzBuzz(int number) { return "Fizz"; } } p[[#V[[͕'J[[͔͠ฦ͞ͳ͍
  6. ςετΛॆ࣮ͤ͞Δ class FizzBuzzTest { @Test void fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(45));

    assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(60)); } @Test void fizz {} @Test void buzz {} @Test void number {} }
  7. class FizzBuzzTest { @Test void fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(45)); assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(60));

    } @Test void fizz {} @Test void buzz {} @Test void number {} } ςετΛॆ࣮ͤ͞Δ ςετύλʔϯΛ૿΍͢ͱ඼࣭͕޲্ ςετϝιου͸Կݸ͋ͬͯ΋ྑ͍ Ξαʔγϣϯ͸Կݸ͋ͬͯ΋ྑ͍
  8. class FizzBuzzTest { @Test void fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(45)); assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(60));

    } @Test void fizz {} @Test void buzz {} @Test void number {} } ςετΛॆ࣮ͤ͞Δ ςετύλʔϯΛ૿΍͢ͱ඼࣭͕޲্ ςετϝιου͸Կݸ͋ͬͯ΋ྑ͍ Ξαʔγϣϯ͸Կݸ͋ͬͯ΋ྑ͍ assertϝιουͰ࠷ॳʹࣦഊͨ͠ ͱ͜ΖͰςετϝιουͷ࣮ߦ͸ఀࢭ
  9. ୅දతͳΞαʔγϣϯϝιου w BTTFSU&RVBMT B C BͱC͕౳͍͠ w BTTFSU5SVF B B͕USVF

    w BTTFSU'BMTF B B͕GBMTF w BTTFSU/VMM B B͕OVMM w BTTFSU/PU/VMM B B͕OVMMͰͳ͍
  10. class FizzBuzzTest { @Test void fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); } }

    public class FizzBuzzTest { @Test public void fizzBuzz { assertThat(FizzBuzz.fizzBuzz(15), equalTo("FizzBuzz")); } } JUnit 4.x JUnit 5.x
  11. import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class FizzBuzzTest { @Test void

    fizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); } } import junit.framework.TestCase; public class FizzBuzzTest extends TestCase { public void testFizzBuzz { assertEquals("FizzBuzz",FizzBuzz.fizzBuzz(15)); } } JUnit 3.x JUnit 5.x
  12. ෼͔Γ΍͍͢ςετϝιου໊ @Test void ड஫લʹ੥ٻঢ়ଶʹ͸ભҠͰ͖ͳ͍ {…} @Test void લ෷͍ͷ͓٬༷ʹ͸੥ٻॻͰ͸ͳ͘ೲ඼ॻΛൃߦ {…} @Test

    void orderInfo1 {…} @Test void orderInfo2 {…} ςετ಺༰͕෼͔Βͳ͍ςετϝιου໊ ςετ಺༰͕ࣗ໌ͳςετϝιου໊
  13. @Test void ड஫લʹ੥ٻঢ়ଶʹ͸ભҠͰ͖ͳ͍ {…} @Test void લ෷͍ͷ͓٬༷ʹ͸੥ٻॻͰ͸ͳ͘ೲ඼ॻΛൃߦ {…} @Test void

    orderInfo1 {…} @Test void orderInfo2 {…} ςετ಺༰͕෼͔Βͳ͍ςετϝιου໊ ςετ಺༰͕ࣗ໌ͳςετϝιου໊ ෼͔Γ΍͍͢ςετϝιου໊ ϓϩδΣΫτϝϯόʔશһ೔ຊޠʹתೳͰ͋ Ε͹ϝιου໊͸೔ຊޠͰ΋OK
  14. BTTFSU"MM import static FizzBuzz.* class FizzBuzzTest { @Test void fizzBuzz

    { assertAll( () -> assertEquals("Fizz", fizzBuzz(3)), () -> assertEquals("Buzz", fizzBuzz(5)), () -> assertEquals("7", fizzBuzz(7)), () -> assertEquals("FizzBuzz", fizzBuzz(15)) ); } ϥϜμࣜΛ࢖ͬͯ·ͱΊͯݕূ ࣦഊ͢ΔΞαʔγϣϯ͕͋ͬͯ΋શͯݕূ