0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · Lektion

Externe APIs mit WireMock testen

Testen Sie Code, der externe HTTP-Dienste aufruft, im Rahmen von Integrationstests, indem Sie diese Dienste mit WireMock simulieren, statt das Netzwerk zu verwenden.

Externe APIs mit WireMock testen ist eine kostenlose Testing Mastery: JUnit, Mockito & Integration Tests-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Testing Mastery: JUnit, Mockito & Integration Tests-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Testing Mastery: JUnit, Mockito & Integration Tests-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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

Häufig gestellte Fragen

Ist die Lektion „Externe APIs mit WireMock testen“ kostenlos?

Ja — der vollständige Text von „Externe APIs mit WireMock testen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Testing Mastery: JUnit, Mockito & Integration Tests-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Testing Mastery: JUnit, Mockito & Integration Tests-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Externe APIs mit WireMock testen“?

Testen Sie Code, der externe HTTP-Dienste aufruft, im Rahmen von Integrationstests, indem Sie diese Dienste mit WireMock simulieren, statt das Netzwerk zu verwenden. Du übst Testing Mastery: JUnit, Mockito & Integration Tests mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Testing Mastery: JUnit, Mockito & Integration Tests zu starten?

Keine Vorkenntnisse erforderlich. Testing Mastery: JUnit, Mockito & Integration Tests auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Externe APIs mit WireMock testen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Testing Mastery: JUnit, Mockito & Integration Tests-Lektion Code schreiben und ausführen?

Ja. Jede Testing Mastery: JUnit, Mockito & Integration Tests-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Unit- und Integrationstests
  2. Integrationstests einrichten
  3. Datenbankinteraktionen testen
  4. Externe APIs mit WireMock testen
← Zurück zu Testing Mastery: JUnit, Mockito & Integration Tests