Testing External APIs with WireMock
Integration-test code that calls external HTTP services by stubbing those services with WireMock instead of hitting the network.
Testing External APIs with WireMock is a free Testing Mastery: JUnit, Mockito & Integration Tests lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Testing Mastery: JUnit, Mockito & Integration Tests learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
Frequently asked questions
Is the “Testing External APIs with WireMock” lesson free?
Yes — the full text of “Testing External APIs with WireMock” is free to read here on the web, and the Testing Mastery: JUnit, Mockito & Integration Tests course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Testing Mastery: JUnit, Mockito & Integration Tests course, upgrade to CoddyKit PRO.
What will I learn in “Testing External APIs with WireMock”?
Integration-test code that calls external HTTP services by stubbing those services with WireMock instead of hitting the network. You practise Testing Mastery: JUnit, Mockito & Integration Tests with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Testing Mastery: JUnit, Mockito & Integration Tests?
No prior experience is required. Testing Mastery: JUnit, Mockito & Integration Tests on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Testing External APIs with WireMock” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Testing Mastery: JUnit, Mockito & Integration Tests lesson?
Yes. Every Testing Mastery: JUnit, Mockito & Integration Tests lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit vs. Integration Tests
- Setting Up Integration Tests
- Testing Database Interactions
- Testing External APIs with WireMock