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

7919a30bc59113c94f31676158cb5881?s=47 irof
September 19, 2019

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

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

7919a30bc59113c94f31676158cb5881?s=128

irof

September 19, 2019
Tweet

Transcript

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

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

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

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

  5. όʔδϣϯΛோΊΔ     w ฒྻ࣮ߦ w ඪ४ग़ྗͷΩϟϓνϟ w

    ಈతςετ w !5FNQ%JS w 5FTU8BUDIFS w ج൫։ൃऀ޲͚"1*ॆ࣮ w !5JNFPVU w ֦ுϙΠϯτͭ w NPEVMFରԠ ϚΠΫϩόʔδϣϯ͸όάϑΟοΫεɻ
  6. Ϟδϡʔϧؔ࿈ w WJOUBHFͱMBVODIFSपΓ͸লུɻίʔυॻ͘ͱ͖ʹग़ͯ͘Δ΋ͷ͚ͩɻ w ·Ͱ͸6TFS(VJEFʹ͜ͷਤ͋ͬͨΜ͚ͩͲɺ͔Βফ͑ͨɻͳ͔ͥ͸஌Βͳ͍ɻ

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

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

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

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

  11. +6OJUඪ४ͷ֦ு Ұ෦ 5JNFPVU&YUFOTJPO   5FNQ%JSFDUPSZ FYQU  %JTBCMFE$POEJUJPO 1BSBNFUFSJ[FE5FTU&YUFOTJPO

    5FTU*OGP1BSBNFUFS3FTPMWFS
  12. 5JNFPVU&YUFOTJPO @Timeout(value = 3, unit = TimeUnit.MILLISECONDS) public class SampleTimeout

    { @Test void test() throws Exception { TimeUnit.MILLISECONDS.sleep(5); } } w λΠϜΞ΢τʢ໓ଟʹ࢖Θͳ͍ʣ
  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"-͚ͩͲʣ
  14. αʔυύʔςΟ੡ w ཁ&YUFOTJPOͷొ࿥ɻ

  15. 4QSJOH&YUFOTJPO // Spring @ExtendWith(SpringExtension.class) public class SampleSpringTest { // Spring

    Boot @SpringBootTest public class SampleSpringBootTest { w 4QSJOH࢖͏ͳΒɻ w 4QSJOH#PPU5FTU͸ϝλΞϊςʔγϣϯͷྫʹ΋ͳΔɻ
  16. 4QSJOH#PPU5FTU w 4QSJOHϢʔβʔͳΒ׳ΕͯΔʢʁʣϝλΞϊςʔγϣϯɻ w +6OJUͩͱ4QSJOH#PPU5FTUͱ3VO8JUIΛซه͢Δඞཁ͕͋ͬͨɻ

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

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

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

  20. &YUFOTJPOͷಈ͔͠ํ

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

  22. &YUFOTJPOͷొ࿥

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

  24. &YUFOTJPO͸$POUFYUʹཷΊΔ

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

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

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

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

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

  30. ෼͚ΔͳΒ͜͏ʁ

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

  32. ྫΛڍ͛ͯΈΔ

  33. %JTBCMFE$POEJUJPO

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

  35. ࣮ࡍΑ͘࢖͏΋ͷ

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

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

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

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

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

    RuntimeException(); } }
  41. ֦ுର৅ͷબ୒

  42. ֦ுର৅ͷબ୒

  43. ྫ֎ϋϯυϥΛ࣮૷ class MahouExtension implements TestExecutionExceptionHandler { @Override public void handleTestExecutionException(

    ExtensionContext context, Throwable throwable) { } } w Կ΋͠ͳ͖ΌѲΓͭͿ͢͜ͱʹͳΔɻ
  44. &YUFOE8JUIͰొ࿥ @ExtendWith(MahouExtension.class) class MahouTest { @Test void ઈରࣦഊ͢Δ൵͍͠ςετ() { throw

    new RuntimeException(); } }
  45. ΍ͬͨʔ

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

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

  48. ࡞ͬͯΈΑ͏ ॓୊ ςετʹಠࣗΞϊςʔγϣϯ Λ࢖ͬͨ΋ͷɻ ʢώϯτ͸࣍ͷϖʔδʣ

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

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

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

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

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

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

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

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