การทดสอบ API ภายนอกด้วย WireMock
ทดสอบแบบผสานรวมโค้ดที่เรียกบริการ HTTP ภายนอก โดยจำลองบริการเหล่านั้นด้วย WireMock แทนการเรียกผ่านเครือข่ายจริง
การทดสอบ API ภายนอกด้วย WireMock เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Testing Mastery: JUnit, Mockito & Integration Tests และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The External Dependency Problem
Integration tests often touch real collaborators, but calling a live third-party API is slow, flaky, and may cost money. WireMock runs a local HTTP server you can program to mimic that API.
What WireMock Provides
WireMock lets you:
- Stub HTTP responses for specific requests
- Simulate delays, errors, and edge cases
- Verify that your code made the expected calls
Starting a WireMock Server
In JUnit you start WireMock on a port (or a random one) before tests run.
WireMockServer server =
new WireMockServer(options().dynamicPort());
server.start();Stubbing a Response
Tell WireMock how to respond to a request using its fluent builder.
server.stubFor(get(urlEqualTo("/users/1"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{\"id\":1}")));Pointing Your Client at WireMock
Configure the code under test to use WireMock's base URL. Using the dynamic port keeps tests isolated.
String baseUrl =
"http://localhost:" + server.port();
UserClient client = new UserClient(baseUrl);Exercising the Code
Now call your client. It hits WireMock, which returns the stubbed payload, and your assertions check the parsed result.
User u = client.fetch(1);
assertEquals(1, u.getId());Simulating Failures
Return a 500 or a malformed body to test your error handling and retries.
server.stubFor(get(urlEqualTo("/users/2"))
.willReturn(aResponse().withStatus(500)));Simulating Latency
Add a fixed delay to verify timeout handling.
server.stubFor(get(anyUrl())
.willReturn(aResponse()
.withFixedDelay(3000)));Verifying Requests
WireMock can assert that your code actually made a call with the right method, path, and headers.
server.verify(getRequestedFor(
urlEqualTo("/users/1")));Cleaning Up
Stop the server after tests, and reset stubs between tests so they stay independent.
server.resetAll();
server.stop();Where It Fits
WireMock sits between unit tests (which mock objects) and full E2E tests (which use real services). It gives realistic HTTP behavior without external dependencies.
Quick Check
What is the main reason to use WireMock in integration tests?
Recap
You learned to test external API calls:
- WireMock runs a local programmable HTTP server
- Stub responses, errors, and delays with its builder
- Point your client at the dynamic port
- Verify requests and reset between tests
คำถามที่พบบ่อย
บทเรียน “การทดสอบ API ภายนอกด้วย WireMock” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบ API ภายนอกด้วย WireMock” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบ API ภายนอกด้วย WireMock”
ทดสอบแบบผสานรวมโค้ดที่เรียกบริการ HTTP ภายนอก โดยจำลองบริการเหล่านั้นด้วย WireMock แทนการเรียกผ่านเครือข่ายจริง คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบ API ภายนอกด้วย WireMock” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม
ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยกับการทดสอบการผสานรวม
- การตั้งค่าการทดสอบการผสานรวม
- การทดสอบการโต้ตอบกับฐานข้อมูล
- การทดสอบ API ภายนอกด้วย WireMock