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

Testare le API esterne con WireMock

Esegua integration test sul codice che chiama servizi HTTP esterni simulando tali servizi con WireMock invece di accedere alla rete.

Testare le API esterne con WireMock è una lezione Testing Mastery: JUnit, Mockito & Integration Tests gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Testing Mastery: JUnit, Mockito & Integration Tests, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Testare le API esterne con WireMock» è gratuita?

Sì — il testo completo di «Testare le API esterne con WireMock» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Testing Mastery: JUnit, Mockito & Integration Tests, passa a CoddyKit PRO. Il corso Testing Mastery: JUnit, Mockito & Integration Tests include 4 lezioni in totale.

Cosa imparerò in «Testare le API esterne con WireMock»?

Esegua integration test sul codice che chiama servizi HTTP esterni simulando tali servizi con WireMock invece di accedere alla rete. Eserciti Testing Mastery: JUnit, Mockito & Integration Tests con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Testing Mastery: JUnit, Mockito & Integration Tests?

Non è richiesta alcuna esperienza precedente. Testing Mastery: JUnit, Mockito & Integration Tests su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Testare le API esterne con WireMock»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Testing Mastery: JUnit, Mockito & Integration Tests?

Sì. Ogni lezione Testing Mastery: JUnit, Mockito & Integration Tests include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Test unitari e di integrazione
  2. Configurare i test di integrazione
  3. Testare le interazioni con il database
  4. Testare le API esterne con WireMock
← Torna a Testing Mastery: JUnit, Mockito & Integration Tests