Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
WireMockでHTTPをモックしよう
Search
YutaSaito
August 05, 2022
Technology
0
240
WireMockでHTTPをモックしよう
2022/8/6 Java Do データアクセスについて聞きたい・話したい!
#javado
YutaSaito
August 05, 2022
Tweet
Share
More Decks by YutaSaito
See All by YutaSaito
依存関係のテストだけじゃないArchUnitのこんな使い方
yutasaito
2
260
JUnitで闘うレガシーコード改善
yutasaito
3
930
Other Decks in Technology
See All in Technology
Whats_new_in_Podman_and_CRI-O_2025-06
orimanabu
3
160
バクラクのモノレポにおける AI Coding のための環境整備と {Roo,Claude} Code活用事例 / AI Coding in Bakuraku's Monorepo: Environment Setup & Case Studies with {Roo, Claude} Code
upamune
9
5.6k
Go Connectへの想い
chiroruxx
0
160
"SaaS is Dead" は本当か!? 生成AI時代の医療 Vertical SaaS のリアル
kakehashi
PRO
2
120
AWS Lambdaでサーバレス設計を学ぼう_ベンダーロックインの懸念を超えて-サーバレスの真価を探る
fukuchiiinu
4
960
マルチテナント+マルチプロダクト SaaS への AI Agent の組み込み方
kworkdev
PRO
2
240
Workflows から Agents へ ~ 生成 AI アプリの成長過程とアプローチ~
belongadmin
2
110
Two-Tower モデルで実現する 検索リランキング / Shibuya_AI_2
visional_engineering_and_design
2
170
AIエージェントのフレームワークを見るときの個人的注目ポイント
os1ma
1
500
20250612_GitHubを使いこなすためにソニーの開発現場が取り組んでいるプラクティス.pdf
osakiy8
1
560
技術職じゃない私がVibe Codingで感じた、AGIが身近になる未来
blueb
0
120
データ戦略部門 紹介資料
sansan33
PRO
1
3.2k
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Thoughts on Productivity
jonyablonski
69
4.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Done Done
chrislema
184
16k
Build The Right Thing And Hit Your Dates
maggiecrowley
36
2.7k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
How to Think Like a Performance Engineer
csswizardry
24
1.7k
Gamification - CAS2011
davidbonilla
81
5.3k
Transcript
!:VUB4BJUP 8JSF.PDLͰ )551ΛϞοΫ͠Α͏
ࣗݾհ wಘҙྖҬ+BWB "84 w͖ͳϓϩٿνʔϜϋϜ wग़౦ wւಓྺະ্ɻ͓͢͢Ίͷ؍ޫඒຯ͍͠͝൧Λڭ͑ͯԼ͍͞ʂ w5XJUUFS!:VUB4BJUP
8JSF.PDLͬͯʁ
)551௨৴͕ൃੜ͢ΔίʔυͰͷࠔΓ͝ͱ w ৽͘͠"1*Λݺͼग़͢ඞཁ͕ੜ͕ͨ͡ɺଓઌͷγεςϜ͕·ͩ࡞ΒΕ͍ͯͳ ͍ͨΊίʔυ͕ಈ͔͘Θ͔Βͳ͍ w ςετڥͰଓઌ"1*͕ظͨ͠Λฦͯ͘͠Εͳ͍ͨΊςετग़དྷͳ͍έ ʔε͕ଘࡏ͢Δ w "1*ͷ௨৴͕ඞཁͳϩδοΫͷςετίʔυΛॻ͚ͳ͍
8JSF.PDLͬͯʁ $MJFOU 4FSWJDF (&5IUUQFYBNQMFDPNIFMMP 0, \lNFTTBHFzlIFMMPz^
8JSF.PDLͬͯʁ $MJFOU (&5IUUQFYBNQMFDPNIFMMP 0, \lNFTTBHFzlIFMMPz^
8JSF.PDLͷಈ͔͠ํ
8JSF.PDLΛಈ͔͢ํ๏େ͖͘छྨ͋Γ·͢ w +BWBͷϥΠϒϥϦΛར༻ͯ͠+6OJUͰىಈ͢Δ w +BSΛμϯϩʔυͯ͠ىಈ͢Δ w %PDLFSΠϝʔδΛىಈ͢Δ
8JSF.PDLΛಈ͔͢ํ๏େ͖͘छྨ͋Γ·͢ w +BWBͷϥΠϒϥϦΛར༻ͯ͠+6OJUͰىಈ͢Δ w +BSΛμϯϩʔυͯ͠ىಈ͢Δ w %PDLFSΠϝʔδΛىಈ͢Δ
+BWBͷϥΠϒϥϦΛར༻ͯ͠+6OJUͰىಈ͢Δ @WireMockTest public class HelloWireMockTest { @Test void helloΛฦ͢(WireMockRuntimeInfo wmRuntimeInfo)
{ stubFor(get("/hello").willReturn(okJson("{ \"message\": \"hello\" }"))); String baseUrl = wmRuntimeInfo.getHttpBaseUrl(); WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl(baseUrl).build(); webTestClient .get() .uri("hello") .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus() .isOk() .expectBody() .jsonPath("$.message") .isEqualTo("hello"); } }
8JSF.PDLΛಈ͔͢ํ๏େ͖͘छྨ͋Γ·͢ w +BWBͷϥΠϒϥϦΛར༻ͯ͠+6OJUͰىಈ͢Δ w +BSΛμϯϩʔυͯ͠ىಈ͢Δ w %PDLFSΠϝʔδΛىಈ͢Δ
%PDLFSΠϝʔδΛىಈ͢Δ $ docker run -it --rm -p 8080:8080 wiremock/wiremock:2.33.2 SLF4J:
Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. /$$ /$$ /$$ /$$ /$$ /$$ | $$ /$ | $$|__/ | $$$ /$$$ | $$ | $$ /$$$| $$ /$$ /$$$$$$ /$$$$$$ | $$$$ /$$$$ /$$$$$$ /$$$$$$$| $$ /$$ | $$/$$ $$ $$| $$ /$$__ $$ /$$__ $$| $$ $$/$$ $$ /$$__ $$ /$$_____/| $$ /$$/ | $$$$_ $$$$| $$| $$ \__/| $$$$$$$$| $$ $$$| $$| $$ \ $$| $$ | $$$$$$/ | $$$/ \ $$$| $$| $$ | $$_____/| $$\ $ | $$| $$ | $$| $$ | $$_ $$ | $$/ \ $$| $$| $$ | $$$$$$$| $$ \/ | $$| $$$$$$/| $$$$$$$| $$ \ $$ |__/ \__/|__/|__/ \_______/|__/ |__/ \______/ \_______/|__/ \__/ port: 8080 enable-browser-proxying: false disable-banner: false no-request-journal: false verbose: false $ curl -X POST \ —data '{ "request": { "url": "/hello", "method": "GET" }, "response": { "status": 200, "body": "{ \"message\": \"hello\"} ", "headers": { "Content-Type": "application/json" }}}' \ http://localhost:8080/__admin/mappings/new $ curl http://localhost:8080/hello { "message": "hello"} %
8JSF.PDLͷ͍ํ
ελϒͷ࡞ @WireMockTest public class BasicStubbing { @Test void stubing(WireMockRuntimeInfo wmRuntimeInfo)
{ stubFor(get("/hello").willReturn(aResponse().withStatus(200).withBody("Hello World!"))); WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl(wmRuntimeInfo.getHttpBaseUrl()).build(); webTestClient .get() .uri("/hello") .exchange() .expectStatus() .isOk() .expectBody(String.class) .isEqualTo("Hello World!"); } }
ϦΫΤετϚονϯά 63-ϚονϯάͰਖ਼نදݱΛར༻͢Δ @WireMockTest public class Matching { @Test void urlRegex(WireMockRuntimeInfo
wmRuntimeInfo) { stubFor(get("/users/*").atPriority(10).willReturn(notFound())); stubFor(get(urlMatching("/users/(123|456)")).willReturn(okJson("{ \"message\": \"hello\" }"))); WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl(wmRuntimeInfo.getHttpBaseUrl()).build(); webTestClient .get() .uri(uriBuilder -> uriBuilder.path("/users/{id}").build("123")) .exchange() .expectStatus() .isOk() .expectBody() .jsonPath("$.message") .isEqualTo("hello"); webTestClient .get() .uri(uriBuilder -> uriBuilder.path("/users/{id}").build("789")) .exchange() .expectStatus() .isNotFound(); } }
ϦΫΤετϚονϯά ϦΫΤετͷଐੑΛར༻ͨ͠Ϛονϯά @WireMockTest public class Matching { @Test void header(WireMockRuntimeInfo
wmRuntimeInfo) { stubFor( get("/users/123") .withHeader("Accept", equalTo("application/json")) .willReturn(okJson("{ \"message\": \"hello\" }"))); stubFor(get("/users/123").withHeader("Accept", equalTo("text/plain")).willReturn(ok("hello"))); WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl(wmRuntimeInfo.getHttpBaseUrl()).build(); webTestClient .get() .uri("/users/123") .accept(MediaType.APPLICATION_JSON) .exchange() .expectStatus() .isOk() .expectBody() .jsonPath("$.message") .isEqualTo("hello"); webTestClient .get() .uri("/users/123") .accept(MediaType.TEXT_PLAIN) .exchange() .expectStatus() .isOk() .expectBody(String.class) .isEqualTo("hello"); } }
ϨεϙϯεςϯϓϨʔτ public class Templating { @RegisterExtension static WireMockExtension wireMockExtension =
new WireMockExtension.Builder() .options( WireMockConfiguration.wireMockConfig() .dynamicPort() .extensions(new ResponseTemplateTransformer(true))) .build(); @Test void templated() { wireMockExtension.stubFor( get("/templated") .willReturn( aResponse() .withBody("{{request.headers.X-Request-Id}}") .withTransformers("respones-template"))); WebTestClient webTestClient = WebTestClient.bindToServer() .baseUrl(wireMockExtension.getRuntimeInfo().getHttpBaseUrl()) .build(); webTestClient .get() .uri("/templated") .header("X-Request-Id", "123") .exchange() .expectStatus() .isOk() .expectBody(String.class) .equals("123"); } }
োΛγϛϡϨʔγϣϯ @WireMockTest public class SimulationgFaults { @Test void timeout(WireMockRuntimeInfo wmRuntimeInfo)
{ stubFor(get("/delayed").willReturn(aResponse().withStatus(200).withFixedDelay(3000))); WebTestClient webTestClient = WebTestClient.bindToServer() .baseUrl(wmRuntimeInfo.getHttpBaseUrl()) .responseTimeout(Duration.ofMillis(2000)) .build(); assertThatThrownBy( () -> { webTestClient.get().uri("/delayed").exchange().expectStatus().isOk(); }) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("Timeout"); } }
ଞʹ৭ʑ͋Γ·͢ʂ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠