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

JUnit 5の拡張機能を完全にマスターした

irof
September 19, 2019

JUnit 5の拡張機能を完全にマスターした

2019-09-19T19:00 関ジャバ'19 9月度のセッション資料です。
https://kanjava.connpass.com/event/141792/

irof

September 19, 2019
Tweet

More Decks by irof

Other Decks in Technology

Transcript

  1. +6OJUͷ֦ுػೳΛ
    ׬શʹϚελʔͨ͌͠
    5
    ؔδϟό݄౓!JSPG
    #PU8͸%ਲ͍Ͱ·ͩΫϦΞͰ͖ͯͳ͍ɻ

    View full-size slide

  2. ͓඼ॻ͖
    ࠷ۙͷ+6OJU
    &YUFOTJPOͷ۩ମྫ
    &YUFOTJPOͷԼࢧ͑
    &YUFOTJPOͷ࡞Γํ

    View full-size slide

  3. ͝஫ҙ
    +6OJU
    ਤ͸*OUFSOBM΍&YQFSJNFOUBMͳ
    ͲΛؚΈ·͢ɻ

    View full-size slide

  4. ࠷ۙͷ+6OJU
    ϦϦʔε௚ޙͷػೳෆ଍ظ͸ൈ͚ɺ
    ػೳతʹ͸མͪண͍͖ͯͨɻ

    View full-size slide

  5. όʔδϣϯΛோΊΔ



    w ฒྻ࣮ߦ
    w ඪ४ग़ྗͷΩϟϓνϟ
    w ಈతςετ
    w !5FNQ%JS
    w 5FTU8BUDIFS
    w ج൫։ൃऀ޲͚"1*ॆ࣮
    w !5JNFPVU
    w ֦ுϙΠϯτͭ
    w NPEVMFରԠ
    ϚΠΫϩόʔδϣϯ͸όάϑΟοΫεɻ

    View full-size slide

  6. Ϟδϡʔϧؔ࿈
    w WJOUBHFͱMBVODIFSपΓ͸লུɻίʔυॻ͘ͱ͖ʹग़ͯ͘Δ΋ͷ͚ͩɻ
    w ·Ͱ͸6TFS(VJEFʹ͜ͷਤ͋ͬͨΜ͚ͩͲɺ͔Βফ͑ͨɻͳ͔ͥ͸஌Βͳ͍ɻ

    View full-size slide

  7. +6OJU͸࣮༻ʹ଱͑Δ͔
    ·ͬͨ͘໰୊ͳ͍ɻ

    View full-size slide

  8. &YUFOTJPOͷ۩ମྫ
    ͍͔ͭ͘঺հ͠·͢ɻ
    Կ͕Ͱ͖Δ͔งғؾ͕Θ͔Ε͹͍͍ͳͱɻ

    View full-size slide

  9. &YUFOTJPO
    +6OJU+VQJUFSͷ֦ு
    3VOOFS3VMFͷޙܧ
    +6OJU1MBUGPSNͷ֦ுͰ͸ͳ͍

    View full-size slide

  10. ຊՈ੡
    w &YUFOTJPOͷొ࿥ෆཁɻউखʹಈ͘ɻʢޙͰग़ͯ͘Δ%&'"6-5@&95&/4*0/4ʣ

    View full-size slide

  11. +6OJUඪ४ͷ֦ு Ұ෦

    5JNFPVU&YUFOTJPO

    5FNQ%JSFDUPSZ FYQU

    %JTBCMFE$POEJUJPO
    1BSBNFUFSJ[FE5FTU&YUFOTJPO
    5FTU*OGP1BSBNFUFS3FTPMWFS

    View full-size slide

  12. 5JNFPVU&YUFOTJPO
    @Timeout(value = 3,
    unit = TimeUnit.MILLISECONDS)
    public class SampleTimeout {
    @Test
    void test() throws Exception {
    TimeUnit.MILLISECONDS.sleep(5);
    }
    }
    w λΠϜΞ΢τʢ໓ଟʹ࢖Θͳ͍ʣ

    View full-size slide

  13. 5FNQ%JSFDUPSZ
    public class SampleTempDir {
    @TempDir
    Path tempDirPath;
    @Test
    void test() {
    System.out.println(tempDirPath);
    }
    }
    w !5FNQ%JSͳϑΟʔϧυ΍ύϥϝʔλ͕͋ͬͨΒ04ͷUFNQEJSʹσΟϨΫτϦΛ࡞ͬͯ
    1BUIΛηοτͯ͘͠ΕΔɻ
    w KVOJUQJPOFFSʹ͋ͬͨͷ͕ຊՈʹऔΓࠐ·Εͨɻʢ·ͩ&91&3*.&/5"-͚ͩͲʣ

    View full-size slide

  14. αʔυύʔςΟ੡
    w ཁ&YUFOTJPOͷొ࿥ɻ

    View full-size slide

  15. 4QSJOH&YUFOTJPO
    // Spring
    @ExtendWith(SpringExtension.class)
    public class SampleSpringTest {
    // Spring Boot
    @SpringBootTest
    public class SampleSpringBootTest {
    w 4QSJOH࢖͏ͳΒɻ
    w 4QSJOH#PPU5FTU͸ϝλΞϊςʔγϣϯͷྫʹ΋ͳΔɻ

    View full-size slide

  16. 4QSJOH#PPU5FTU
    w 4QSJOHϢʔβʔͳΒ׳ΕͯΔʢʁʣϝλΞϊςʔγϣϯɻ
    w +6OJUͩͱ4QSJOH#PPU5FTUͱ3VO8JUIΛซه͢Δඞཁ͕͋ͬͨɻ

    View full-size slide

  17. w .PDLJUP+6OJU3VOOFSͷ୅ସɻ
    w NPDLJUPDPSFʹೖ͍ͬͯͳ͍ NPDLJUPNPDLJUPKVOJUKVQJUFS
    ͷͰ஫ҙɻ
    .PDLJUP&YUFOTJPO

    View full-size slide

  18. +6OJU+VQJUFS#%%4PGU"TTFSUJPOT
    w "TTFSU+ͷ֦ுɻ
    w "GUFS&BDI$BMMCBDLͰ࣮ݱ͍ͯ͠Δɻ

    View full-size slide

  19. &YUFOTJPOͷԼࢧ͑
    ͬ͘͟Γɺ͔ΜͨΜʹɻ

    View full-size slide

  20. &YUFOTJPOͷಈ͔͠ํ

    View full-size slide

  21. &YUFOTJPOͷొ࿥
    !&YUFOE8JUI
    !3FHJTUFS&YUFOTJPO
    4FSWJDF-PBEFS

    View full-size slide

  22. &YUFOTJPOͷొ࿥

    View full-size slide

  23. ొ࿥ࡁΈ&YUFOTJPO
    w ͜Ε͕͋Δ͔Βඪ४ͷ֦ு͕&YUFOE8JUIͱ͔ͳ͠Ͱಈ͘

    View full-size slide

  24. &YUFOTJPO͸$POUFYUʹཷΊΔ

    View full-size slide

  25. &OHJOF&YFDVUJPO$POUFYU
    w &OHJOF&YFDVUJPO$POUFYU͸ۭͬΆɻ
    w +VQJUFSͰͳ͍+6OJU1MBUGPSN͸&YUFOTJPOͳΜͯ஌ͬͨͬͪ͜Όͳ͍ɻ

    View full-size slide

  26. &YUFOTJPO$POUFYU
    &YUFOTJPOͷ࣮૷ΫϥεͰ࢖͏
    ʢ͜ͱ͕ଟ͍ʣ
    ౉ͬͯ͘Δ͔͸֦ுґଘ
    ʢ&YUFOTJPOΠϯλϑΣʔε͸
    ϝιου࣋ͨͳ͍ʣ

    View full-size slide

  27. &YUFOTJPO$POUFYUͷड͚औΓ
    w ΠϯλϑΣʔεͷҾ਺Ͱ౉͞ΕΔɻ
    w &YUFOTJPO$POUFYU͔ΒςετΠϯελϯεͳͲΛऔಘͯ͠৭ʑ͢Δɻ

    View full-size slide

  28. ֦ுϙΠϯτ
    ֦ுϙΠϯτ͝ͱʹΠϯλ
    ϑΣʔε͕༻ҙ͞Ε͍ͯΔɻ
    ݺͼग़͠͸+6OJU೚ͤɻ
    ৽͍֦͠ுϙΠϯτΛ଍͢ͷ
    ͸೉͍͠ɻ

    View full-size slide

  29. &YUFOTJPO)JFSBSDIZ
    w ߇͑Ίʹݴͬͯ໾ʹཱͨͳ͍ਤɻ

    View full-size slide

  30. ෼͚ΔͳΒ͜͏ʁ

    View full-size slide

  31. $BMMCBDL͕ݺ͹ΕΔλΠϛϯά
    w 6TFS(VJEFࢀর

    View full-size slide

  32. ྫΛڍ͛ͯΈΔ

    View full-size slide

  33. %JTBCMFE$POEJUJPO

    View full-size slide

  34. 4QSJOH&YUFOTJPO
    5FTU&YFDVUJPO-JTUFOFSʹͭͳ͙࢓૊
    Έ1BSBNFUFSղܾɻ
    ػೳ௥Ճ͸5FTU&YFDVUJPO-JTUFOFSͰɻ
    5FTU$POUFYUҾͬுΓग़͠͸৻ॏʹɻ

    View full-size slide

  35. ࣮ࡍΑ͘࢖͏΋ͷ

    View full-size slide

  36. &YUFOTJPOͷ࡞Γํ
    ؆୯ʹɻ

    View full-size slide

  37. ࡞Γํ
    ֦ுΠϯλϑΣʔ
    εΛૉ௚ʹ࢖͏ɻ
    ෺଍Γͳ͍ਓ͸
    FOHJOF࣮૷͢Ε͹
    ͍͍ʢී௨͠ͳ͍

    View full-size slide

  38. ΍Δ͜ͱ
    ࣮૷ΫϥεΛ࡞੒
    &YUFOE8JUIͳͲͰొ࿥
    w ؆୯Ͱ͠ΐʁ

    View full-size slide

  39. ࡞ͬͯΈΑ͏
    ͳʹ͕͋ͬͯ΋ςετ͸௨ͬ
    ͯཉ͍͠ʂ

    View full-size slide

  40. ௨ͬͯཉ͍͠ςετ
    class MahouTest {
    @Test
    void ઈରࣦഊ͢Δ൵͍͠ςετ() {
    throw new RuntimeException();
    }
    }

    View full-size slide

  41. ֦ுର৅ͷબ୒

    View full-size slide

  42. ֦ுର৅ͷબ୒

    View full-size slide

  43. ྫ֎ϋϯυϥΛ࣮૷
    class MahouExtension
    implements TestExecutionExceptionHandler {
    @Override
    public void handleTestExecutionException(
    ExtensionContext context,
    Throwable throwable) {
    }
    }
    w Կ΋͠ͳ͖ΌѲΓͭͿ͢͜ͱʹͳΔɻ

    View full-size slide

  44. &YUFOE8JUIͰొ࿥
    @ExtendWith(MahouExtension.class)
    class MahouTest {
    @Test
    void ઈରࣦഊ͢Δ൵͍͠ςετ() {
    throw new RuntimeException();
    }
    }

    View full-size slide

  45. ׬શʹϚελʔͨ͠
    w ͪͳΈʹ00.&͸#MBDL-JTUFEFYDFQUJPOͱͯ͠ಛผѻ͍͞ΕͯΔ͔Βμϝɻ

    View full-size slide

  46. ୤ઢ#MBDLMJTUFE&YDFQUJPOT
    w 0VU0G.FNPSZ&SSPS͚ͩ͸ଈ࣌ऴྃ͢Δɻʢ+6OJU1MBUGPSNʣ

    View full-size slide

  47. ࡞ͬͯΈΑ͏ ॓୊

    ςετʹಠࣗΞϊςʔγϣϯ
    Λ࢖ͬͨ΋ͷɻ
    ʢώϯτ͸࣍ͷϖʔδʣ

    View full-size slide

  48. w TVQQPSUύοέʔδΛ࢖͏ͷ͕ਪ঑ʢ৑௕ʹͳͬͪΌ͏͠ʣ
    w 4VQQPSUFE6UJMJUJFTJO&YUFOTJPOT

    View full-size slide

  49. ֦ுͷՖܗ͸
    1BSBNFUFS3FTPMWFS
    ͩͱࢥ͏
    w ͜͜ʹه͢ʹ͸༨ന͕ڱ͗͢ΔɻʢׂѪʣ

    View full-size slide

  50. ࡞Δͱ͖ͷϙΠϯτ
    ঢ়ଶ͸4UPSFͰѻ͏
    ϝλΞϊςʔγϣϯΛ࡞Δ
    ҆શ૷ஔʹ߄ͯͳ͍

    View full-size slide

  51. w ,FFQJOH4UBUFJO&YUFOTJPOT
    &YUFOTJPO4UPSF
    &YUFOTJPOʹঢ়ଶΛ࣋ͭͱ൵͍͜͠
    ͱ͕ى͜Δɻ໘౗Ͱ΋4UPSFͰɻ
    ࢖͍ํ͸ೋஈ֊ͷ.BQɻ
    .BQ/BNFTQBDF .BQ, 7
    ͷΠϝʔδɻ͋͘·ͰΠϝʔδɻ

    View full-size slide

  52. 4UPSFͱ/BNFTQBDFͱ1BSFOUͱ
    w &YUFOTJPO7BMVF4UPSFΠϯελϯεͷϥΠϑαΠΫϧ͸&YUFOTJPO$POUFYUͱಉ͡
    w 4UPSFΠϯελϯε͸HFU4UPSFͷͨͼʹ࡞ΒΕΔʢ͚ͲͲ͏Ͱ΋͍͍ʣ
    w /BNFTQBDF࢖͍ʹ͍͘

    View full-size slide

  53. ϝλΞϊςʔγϣϯ
    Ξϊςʔγϣϯʹ෇༩͢ΔΞϊςʔγϣϯ
    Ξϊςʔγϣϯͷهड़͸৑௕ͳͷͰੵۃతʹ࢖
    ͏
    &YUFOTJPO࡞ͬͨΒ&YUFOE8JUIͰΞϊςʔτ͠
    ͨΞϊςʔγϣϯΛͱΓ͋͑ͣ࡞Δͱ͍͍ɻ
    +BWBඪ४ͷ࢓૊ΈͰ͸ͳ͍
    w .FUB"OOPUBUJPOTBOE$PNQPTFE"OOPUBUJPOT

    View full-size slide

  54. ҆શ૷ஔ͍ͭͯ·͢
    w 5FTU*OTUBODF'BDUPSZͰOVMMΛฦͨ͠
    w *OWPDBUJPO*OUFSDFQUPSͰϝιουݺ͹ͳ͔ͬͨ
    w มͳ͜ͱ͕Ͱ͖ͳ͘ͳͬͯΔɻͨͱ͑͹ςετϝιουΛݺ͹ͳ͘͢Δͱ͔ɻ

    View full-size slide

  55. ͜ΕͰ+6OJUΛ
    ׬શʹϚελʔͨ͠
    ʜʜͱݴ͍͍ͬͯͱࢥ͏ɻ
    ςετࣗମ͸10+0ͩ͠ɺΞαʔγϣϯํ๏͸બ୒ࣜɻ
    FOHJOF΍MBVODIFS͸+6OJU +VQJUFS
    Ͱ͸ͳ͘ɺ
    +6OJU1MBUGPSNͷ࿩ͳͷͰɻ

    View full-size slide