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

Testing Java With Spock

2722ca734f5f05badb74f976d1f2b6c8?s=47 yonetty
December 25, 2019

Testing Java With Spock

Java Testing Challenge LT大会(第1回)でのLTネタ。

2722ca734f5f05badb74f976d1f2b6c8?s=128

yonetty

December 25, 2019
Tweet

Transcript

  1. 5FTUJOH+BWBXJUI4QPDL /PW  +BWB5FTUJOH$IBMMFOHF-5 !UZPOFLVCP

  2. "CPVU.F OBNFlถٱอ߶z UXJUUFSl!UZPOFLVCPz CMPHlIUUQTCMPHZOLCYZ[z XPSLGPSl๭4*FSz KPClύοέʔδιϑτͷ։ൃ5-z GBWPSJUFMBOHl4DBMBz

  3. ϨΨγʔγεςϜ͋Δ͋Δ ςετ͠ʹ͍͘ ΞʔΩςΫνϟɾઃܭ ςετίʔυ͕গͳ͍ վम͢Δͷ͕ා͍ɺ ϦϑΝΫλϦϯά΋ ༰қʹͰ͖ͳ͍ ύονϫʔΫతͳ ͦͷ৔͠ͷ͗ͷվम

  4. 5FTUBCJMJUZ ςετ͠΍͍͢ઃܭ Α͍ઃܭ cc

  5. &/6' 5%%͸Α͍ઃܭʹಋ͕͘ɺ Α͍ΞʔΩςΫνϟ͕ࣗવʹ ੜ·ΕΔΘ͚Ͱ͸ͳ͍ ͋Δఔ౓ͷΞʔΩςΫςΟϯά ͸ඞཁ &/PVHIEFTJHO6Q'SPOU

  6. $MFBO"SDIJUFDUVSFΛ࠾༻ w υϝΠϯ૚ʹϩδοΫΛू໿ w ৄࡉ 8FC %*ίϯ %# FUD ΁ͷґଘੑΛഉআ

    υϝΠϯϦονͳΦϒδΣΫτ 10+0 ςετ͠΍͍͢ʂ ςετ͕଎͍ʂʂ
  7. ࢖͍ͬͯΔςετπʔϧ FFςετ डೖςετ ຊ೔͸4QPDLʹ ͍ͭͯͷ͓࿩ w $VDVNCFS4FSFOJUZΛ࢖ͬͨ FFςετɺडೖςετ #%% 

    w 4QPDLΛ࢖ͬͨϢχοτςετ 5%%
  8. 8IZ4QPDL +BWBͰίʔυΛॻ͘ͷ͕ͭΒ͍ʢ৑௕ʣ ɾεΫϦϓτݴޠͰ͋Δ(SPPWZͷखܰ͞ɾॊೈੑ ɾ4QPDLͷ%4-ʹΑΔςετίʔυͷՄಡੑ

  9. 4QPDLͷ͍͍ͱ͜Ζ ᶃ (SPPWZͰίʔυΛॻ͘ͷָ͕ ᶄ #%%ελΠϧ (JWFO8IFO5IFO  ᶅ ڧྗͳΞαʔγϣϯ ᶆ

    ύϥϝλϥΠζυɾςετ ᶇ ϞοΫϥΠϒϥϦෆཁ
  10. (SPPWZͰίʔυΛॻ͘ͷָ͕ when: "ύʔςΟΛ૊Ή" def party = Party.of(taro, hanako, ichiro) then:

    "ϝϯόʔ͕ਖ਼͍͠" party.getNumberOfPeople() == 3 party.getPeople() == [taro, hanako, ichiro] ม਺એݴʹܕͷ ࢦఆ͕ෆཁ EFG -JTU΍.BQͷ Ϧςϥϧදه ϓϦϛςΟϒܕ΋ΦϒδΣΫτ ܕ΋Ͱ౳஋ൺֱ
  11. #%%ελΠϧ (JWFO8IFO5IFO def "20ࡀ͸େਓʹ෼ྨ͞ΕΔ"() { given: "೥ྸ͸20ࡀ" int age =

    20 when: "ྉۚ෼ྨΛऔಘ" def feeClass = FeeClassification.of(age) then: "େਓ" feeClass == FeeClassification.Adults } HJWFO ࣄલ৚݅Λهड़ XIFO ςετର৅ͷ ΞΫγϣϯΛهड़ UIFO ࣄޙ৚݅Λݕূ ୯ͳΔϒϩοΫ෼ׂ Ͱ͸ͳ͍ʂ w w w w
  12. ڧྗͳΞαʔγϣϯ when: "ύʔςΟΛ૊Ή" def party = Party.of(taro, hanako, ichiro) then:

    "ϝϯόʔ͕ਖ਼͍͠" party.getNumberOfPeople() == 3 party.getPeople() == [taro, hanako, ichiro] Condition not satis ed: party.getPeople() == [taro, hanako, ichiro] | | | | | | | | | | (33) (8) | | | (35) | | false | [ (35), (8), (33)] <com.example.tjws.Party@ f a peopleList=[ (35), (8), (33)]> UIFOϒϩοΫʹ͸Ξαʔτ ͍ͨ͠౳ࣜΛྻڍ͢Δ Ξαʔγϣϯʹࣦഊͨ͠ ৔߹ͷ໰୊֬ೝ͕༰қ
  13. ύϥϝλϥΠζυɾςετ @Unroll def "೥ྸʹԠͯ͡ਖ਼͘͠෼ྨ͞ΕΔ #ageࡀ -> #expectedClass"() { when: "ྉۚ෼ྨΛऔಘ"

    def feeClass = FeeClassification.of(age) then: "෼ྨ͕ਖ਼͍͠" feeClass == expectedClass where: age | expectedClass 0 | FeeClassification.Infants 3 | FeeClassification.Infants 4 | FeeClassification.Children 12 | FeeClassification.Children 13 | FeeClassification.Adults 64 | FeeClassification.Adults 65 | FeeClassification.SeniorCitezens } ڥք஋ͷςετͳͲɺ৚݅Λ͍Ζ͍Ζ ม͑ͯςετΛߦ͍͍ͨ࣌ʹɺςετ έʔεΛ෼͚Δͷ͸৑௕Ͱ໘౗ ˣ XIFSFϒϩοΫʹදܗࣜͰهड़ͨ͠ ύϥϝʔλΛ࢖ͬͯɺ܁Γฦ͠ςετ ͕࣮ߦ͞ΕΔ
  14. ϞοΫϥΠϒϥϦෆཁ given: "ଠ࿠͸େਓ(20ࡀ)" def taro = new Person("ଠ࿠", 20) and:

    "ύʔςΟ͸Ұਓ" def party = Party.of(taro) and: "ྉۚදͷϞοΫ" def feeTable = Mock(FeeTable) and: "ྉۚܭࢉػ" def calculator = new FeeCalculator(feeTable) when: "ྉۚܭࢉΛߦ͏" def amount = calculator.calcTotalFee(party) then: "ྉۚද͕࢖༻͞ΕΔ" 1 * feeTable.getFeeFor(FeeClassification.Adults) >> 1000 and: "ֹ͕ۚਖ਼͍͠" amount == 1000 ϞοΫ΍ελϒػೳ͸ 4QPDLʹ಺แ͞Ε͍ͯΔ ϞοΫͷৼΔ෣͍ͱظ଴ಈ࡞Λ UIFOϒϩοΫʹهड़Ͱ͖Δ ϝιου͕ݺ͹ΕΔ૝ఆճ਺͸ O ͰදݱͰ͖Δ
  15. ·ͱΊ w 4QPDLΛ࢖͏ͱςετΛॻ͘ͷָ͕ʂ w ೉͘͠ͳ͍ͷͰɺ+BWBͷ։ൃϓϩδΣΫ τʹ΋ಋೖ͕͠΍͍͢ʂ w 4QPDLͰָ͍͠5FTU-JGFΛʂ αϯϓϧίʔυ͸(JU)VCͰެ։ͯ͠·͢ https://github.com/yonetty/testing-java-with-spock

  16. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠