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

Testing Java With Spock

yonetty
December 25, 2019

Testing Java With Spock

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

yonetty

December 25, 2019
Tweet

More Decks by yonetty

Other Decks in Technology

Transcript

  1. (SPPWZͰίʔυΛॻ͘ͷָ͕ when: "ύʔςΟΛ૊Ή" def party = Party.of(taro, hanako, ichiro) then:

    "ϝϯόʔ͕ਖ਼͍͠" party.getNumberOfPeople() == 3 party.getPeople() == [taro, hanako, ichiro] ม਺એݴʹܕͷ ࢦఆ͕ෆཁ EFG -JTU΍.BQͷ Ϧςϥϧදه ϓϦϛςΟϒܕ΋ΦϒδΣΫτ ܕ΋Ͱ౳஋ൺֱ
  2. #%%ελΠϧ (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
  3. ڧྗͳΞαʔγϣϯ 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ϒϩοΫʹ͸Ξαʔτ ͍ͨ͠౳ࣜΛྻڍ͢Δ Ξαʔγϣϯʹࣦഊͨ͠ ৔߹ͷ໰୊֬ೝ͕༰қ
  4. ύϥϝλϥΠζυɾςετ @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ϒϩοΫʹදܗࣜͰهड़ͨ͠ ύϥϝʔλΛ࢖ͬͯɺ܁Γฦ͠ςετ ͕࣮ߦ͞ΕΔ
  5. ϞοΫϥΠϒϥϦෆཁ 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 ͰදݱͰ͖Δ