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

Startup Spock #javajo

405fe9ab689473f267e2cfbd95f78c75?s=47 kyonmm
March 25, 2017

Startup Spock #javajo

Java女子部でSpockのハンズオンをしたときのスライドです。

405fe9ab689473f267e2cfbd95f78c75?s=128

kyonmm

March 25, 2017
Tweet

Transcript

  1. STARTUP SPOCK #JAVAJO kyon_mm 2017/03/25 1

  2. SELF INTRODUCTION • ͖ΐΜ : @kyon_mm גࣜձࣾΦϯβϩʔυ • ςετΞʔΩςΫτɺδΣωϥϦετ •

    Groovy, F#, C#, Scala • جૅษڧձ, TDDBootCamp, ͳ͝΍͔Scrum 2
  3. THIS SESSION : TARGET • Javaݴޠͷجຊจ๏͸ͳΜͱͳ͘Θ͔͍ͬͯ ͯɺίʔυΛ͔͍ͨΓͰ͖Δɻ • Can you

    code FizzBuzz ? 3
  4. THIS SESSION : GOAL • SpockͰ؆୯ͳίʔυΛॻ͚ΔΑ͏ʹͳΔɻ 4

  5. NO • Groovy LanguageΛ࢖͍͜ͳ͢ • SexyͳCodeΛ௥ٻ͢Δ 5

  6. MENU • https://github.com/kyonmm/startup-spock • Groovy Language for Java Language User

    • Getting Start Spock • Spock with TDD 6
  7. GROOVY LANGUAGE • Groovyͱ͸ • Java8ඇޓ׵ͳ΋ͷ • ͓֮͑ͯ͘ͱศརͳ΋ͷ 7

  8. GROOVYͱ͸ • ઴ਐతܕ෇͖(੩తܕݕࠪ͸Φϓγϣϯ) • JVMͰಈ࡞͢Δɻgroovy-all.jarͱҰॹʹ࢖͏ɻ • GradleɺJenkinsfile͸GroovyͷDSLɻ 8

  9. GROOVYͱ͸ • Java6ͱ΄΅ޓ׵ੑΛͨ΋͍ͬͯΔ͕ɺ Java7,Java8ͷ৽ػೳͱ͸Ұ෦ޓ׵ੑ͕ͳ͍ɻ (Java6Ҏલ͔ΒಠࣗͰ࣮૷͍ͯͨ͠Γͱ͔) • IntelliJ IDEA + Gradle

    + Spock͕ఆ൪ • OO͕ϕʔεɻޠኮ͸RubyɺPythonͬΆ͞ɻ 9
  10. GROOVY LANGUAGE • Groovyͱ͸ • Java8ඇޓ׵ͳ΋ͷ • ͓֮͑ͯ͘ͱศརͳ΋ͷ 10

  11. Ҏ߱ͷηογϣϯͷͨΊʹ ؀ڥߏங • ιʔείʔυΛߋ৽ͨ͠ͷͰɺGitHub͔Β࠷৽ͷιʔείʔυΛམ ͱ͖͍ͯͯͩ͘͠͞ɻ(git reset --hard; git pull;) •

    IntelliJ IDEAͷ࣍ͷઃఆΛ͠·͠ΐ͏ɻ • Plugins -> Spock -> Install • Build Project Automatically • Edit Configurations -> Defaults -> JUnit -> Before LaunchͷBuildΛબ ୒ͯ͠࡟আ( - ϘλϯΛΫϦοΫ) 11
  12. JAVA8ͱඇޓ׵ͳ΋ͷ • ϥϜμͷ୅ΘΓʹΫϩʔδϟΛ࢖͏ • ϝιουࢀরɺίϯετϥΫλࢀর͕ͳ͍ • try-with-resource͕࢖͑ͳ͍ • ഑ྻϦςϥϧ͕࢖͑ͳ͍ 12

  13. ϥϜμͷ୅ΘΓʹΫϩʔδϟ Λ࢖͏ • ϥϜμ • ࣜɻJava7·Ͱ͸ؔ਺ΦϒδΣΫτͰ࣮ݱ͍ͯͨ͠Γɻ • GroovyͰ͸ϥϜμͷॻ͖ํΛαϙʔτ͍ͯ͠ͳ͍ɻ • GroovyͰ͸ΫϩʔδϟͰ࣮ݱ͢Δɻ

    • src/test/groovy/example/startup/incompatible/java8/ Lambda.groovy 13
  14. ϝιουࢀরɺ ίϯετϥΫλࢀর͕ͳ͍ • ϝιουࢀর • Ϋϥε໊::ϝιου໊,ΦϒδΣΫτ໊::ϝιου໊
 (ಛʹ)ϥϜμͱͷซ༻Ͱ؆ܿͳهड़Λ࣮ݱͰ͖Δɻ
 Stream.of("javajo", "jjug", "kyon_mm").forEach(System.out::println)

    • GroovyͰ͸ϝιουࢀরɺίϯετϥΫλࢀরͷॻ͖ํΛαϙʔτ͍ͯ͠ͳ͍ɻ • GroovyͷΫϩʔδϟͰ͸Ҿ਺Λ໌ࣔ͠ͳ͍ͱ͖͸ݩͷΦϒδΣΫτ͕ it ͰΘͨͬ ͯ͘ΔͷͰɺ{ println(it) }ͷΑ͏ʹ࢖͏ɻ • src/test/groovy/example/startup/incompatible/java8/MethodReference.groovy 14
  15. TRY-WITH-RESOURCE • try-with-resource • ྫ֎Λߟྀͨ͠Ϧιʔε(IO)ͷΫϩʔζΛࣗಈతʹ΍ͬͯ͘ΕΔ • GroovyͰ͸try-with-resourceͷॻ͖ํΛαϙʔτ͍ͯ͠ͳ͍ɻ • ͨͩ͠ɺ୅ସखஈͱͯ͠ withCloseable

    ϝιουΛ࢖͏͜ͱ͕Ͱ͖ Δɻ • src/test/groovy/example/startup/incompatible/java8/ TryWithResource.groovy 15
  16. ഑ྻϦςϥϧ͕ͳ͍ • ഑ྻϦςϥϧ • int[] xs = new int[]{1,2,3}; •

    GroovyͰ͸{}͸ΫϩʔδϟͳͷͰΤϥʔʹͳΔ • ϦετϦςϥϧͰArrayListΛੜ੒ͯ͠ɺޙΖʹas int[] Λ௥ه͢ Δɻ • src/test/groovy/example/startup/incompatible/java8/ ArrayLiteral.groovy 16
  17. Α͘ΈΔͰ͋Ζ͏ Τϥʔϝοηʔδ • groovy.lang.MissingMethodException: No signature of method: example.startup.groovy.Keyword.newMethod() •

    example.startup.groovy.Keyword.newMethod͍ͬͯ͏ϝ ιουͳ͔ͬͨΑ • groovy.lang.MissingPropertyException: No such property: y for class: example.startup.groovy.Keyword • KeyWordΫϥεʹy͍ͬͯ͏ϓϩύςΟͳ͔ͬͨΑ 17
  18. GROOVY LANGUAGE • Groovyͱ͸ • Java8ඇޓ׵ͳ΋ͷ • ͓֮͑ͯ͘ͱศརͳ΋ͷ 18

  19. ͓֮͑ͯ͘ͱศརͳ΋ͷ • ϦετϦςϥϧ • ϚοϓϦςϥϧ • จࣈྻϦςϥϧ • ਖ਼نදݱϦςϥϧ •

    GroovyͷίϨΫγϣϯૢ࡞ϝιουͱJavaͷstreamͱͷൺֱ 19
  20. GROOVYͰΑ͔ͭ͘͏ॻ͖ํ • defͰએݴ • ςετϝιου໊͸จࣈྻએݴ 20

  21. ͦͷଞ஫ҙ • booleanΛظ଴͞ΕΔ৔ॴ(ex. if(x))ʹɺBoolean Ҏ֎ͷࢀরܕ͕͸͍ͬͨ৔߹͸ͳ͍৔߹͸ ࣍ͷنଇͰ൑ఆ͞Ε·͢ɻ • "", [], [:],

    0, null -> false • ͦΕΒҎ֎ -> true 21
  22. MENU • Groovy Language for Java Language User • Getting

    Start Spock • Spock with TDD 22
  23. • Spockͱ͸ • JUnitͱͷൺֱ • Spockͷ࢖͍͔ͨ 23

  24. SPOCKͱ͸ • JUnit4ϕʔεͷGroovyͷςεςΟϯάϑϨʔ ϜϫʔΫ • BDDΛҙ͍ࣝͯ͠Δ • powerassert ͸Spock͕ΦϦδϯ(ͩͱࢥ͏) 24

  25. JUNITͱͷൺֱ 25 JUnit Spock Specification Ϋϥε spock.lang.SpecificationΛܧঝ Feature Method @Test

    SpockͷϥϕϧΛ࢖༻͢Δϝιου Helper Method ϝιουએݴ @Share Fixture Method @Before setupϝιου Fixture Method @After cleanupϝιου Fixture Method @BeforeClass setupSpecϝιου Fixture Method @AfterClass cleanupSpecϝιου
  26. SPOCKͷ࢖͍͔ͨ • Specification • given, when, then, and, where •

    power assert • setup, teardown, setupSpec, cleanupSpec • @Unroll, @Shared, @Ignore 26 src/test/groovy/example/startup/spock ഑ԼΛΈͳ͕Β
  27. MENU • Groovy Language for Java Language User • Getting

    Start Spock • Spock with TDD 27
  28. SPOCK WITH TDD • SpockͰTDDΛϖΞϓϩάϥϛϯάͰ΍ͬͯΈ· ͠ΐ͏ɻ • ͓୊͸ʮλεΫϦετ(λεΫ؅ཧπʔϧ)ʯͷ։ൃ Ͱ͢ɻ •

    ࠓճ͸γεςϜͷೖྗ෦෼͸লུ͠·͢ɻ(CUI, GUI ͷ։ൃ͸লུ͠·͢ɻ) 28
  29. λεΫϦετ • λεΫ໊ͱظ೔Λొ࿥ͨ͠Γɺొ࿥ࡁΈͷ ΋ͷΛࢀরͨ͠Γɺ࡟আͨ͠ΓͰ͖Δɻ • λεΫ໊΍ظ೔ͰݕࡧͰ͖Δɻ 29

  30. TaskList ---------------------- add(Task) : TaskList delete(Task) : TaskList list(Task) :

    List<Task> search(String) : List<Task> search(Date, Date) : List<Task> searchFrom(Date) : List<Task> searchTo(Date) : List<Task> Task ---------------------- title : String dueTo : Date * 30
  31. TDD • ςετۦಈ։ൃ(Test Driven Development) • ςετ࣮૷ -> ϓϩμΫτ࣮૷ ->

    ϦϑΝΫλ Ϧϯά ͷ܁Γฦ͠Ͱ։ൃ͢Δɻ • ઃܭ͓Αͼ෼ੳͷख๏ 31
  32. TDD3ݪଇ • ϓϩμΫτίʔυ͸ࣦഊ͢ΔςετίʔυΛ ੒ޭͤ͞ΔͨΊ͚ͩʹॻ͘ • ςετίʔυ͸ࣦഊͤ͞ΔͨΊ͚ͩʹॻ͘ • ෳ਺ͷςετΛ੒ޭͤ͞ΔΑ͏ͳϓϩμΫτ ίʔυΛॻ͍ͯ͸͍͚ͳ͍ 32

  33. ࠓ೔΍ΔTDDϥΠΫͳ։ൃ ςετ࣮૷ฤ 1. (΋͠ɺ͍·͔Β࣮૷͠Α͏ͱ͍ͯ͠Δ΋ͷ͸طʹ୭͔͕࣮૷͠ ͯ͘Ε͍ͯͨΒ)
 ͲΜͳखॱͰɺͦΕΛ࢖͏ͷ͔? 2. 1Ͱࢥ͍͍ͭͨ࢖͍ํΛ೔ຊޠͰ΋ɺٙࣅίʔυͰ΋͍͍ͷͰς Ωετʹ͢Δɻ 3.

    ͦΕΛSpockͷgiven, when, thenʹ͋ͯ͸ΊΔɻ 4. ਖ਼͍͠Groovyίʔυʹ͢Δɻ 33
  34. ࠓ೔΍ΔTDDϥΠΫͳ։ൃ ϓϩμΫτ࣮૷ฤ 1. ςετΛ࣮૷ͨ͠Βςετ͕ಈ࡞͢ΔΑ͏ͳϓϩ μΫτΛ࣮૷͢Δɻ 2. ΋͠ɺ15෼ͨͬͯςετ͕੒ޭ͠ͳ͔ͬͨΒɺ͍ ·࣮ݱ͠Α͏ͱ͍ͯ͠ΔςετίʔυΛ@Ignore͠ ͯɺ΋͏͢͜͠খ͍͞ৼ෣͍Λ࣮ݱ͢Δςετίʔ υΛهड़ͯ͠ɺ࠶౓ϓϩμΫτ࣮૷ʹ௅ઓ͢Δɻ

    34
  35. ࠓ೔΍ΔTDDϥΠΫͳ։ൃ ϦϑΝΫλϦϯάฤ 1. ϓϩμΫτίʔυͷॏෳ͕͋Ε͹ڞ௨Խ 2. ςετίʔυͷॏෳ͕͋Ε͹ڞ௨Խ 3. ໊લʹҧ࿨ײ͕͋Ε͹ϦωʔϜ΍ϝιου ͷҠಈɺநग़ 35

  36. λεΫϦετ • λεΫ໊ͱظ೔Λొ࿥ͨ͠Γɺొ࿥ࡁΈͷ ΋ͷΛࢀরͨ͠Γɺ࡟আͨ͠ΓͰ͖Δɻ • λεΫ໊΍ظ೔ͰݕࡧͰ͖Δɻ 36

  37. TaskList ---------------------- add(Task) : TaskList delete(Task) : TaskList list(Task) :

    List<Task> search(String) : List<Task> search(Date, Date) : List<Task> searchFrom(Date) : List<Task> searchTo(Date) : List<Task> Task ---------------------- title : String dueTo : Date * 37
  38. λεΫϦετ:௥Ճ͓୊ • λεΫ໊ͱظ೔Λొ࿥ͨ͠Γɺొ࿥ࡁΈͷ΋ͷΛ ࢀরͨ͠Γɺ࡟আͨ͠ΓͰ͖Δɻ • λεΫ໊΍ظ೔ͰݕࡧͰ͖Δɻ • λεΫΛӬଓԽ͓ͯ͘͜͠ͱͰɺΞϓϦ͕ऴྃ͠ ͯ΋λεΫΛ࢒͓͚ͯ͠Δ(JSONͱ͔CSVʹग़ྗ͠ ͯಡΈͩͤΔΑ͏ʹ͢Δ)

    38