0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · レッスン

WireMockによる外部APIのテスト

ネットワークへ接続する代わりにWireMockで外部HTTPサービスをスタブ化し、そのサービスを呼び出すコードを統合テストします。

「WireMockによる外部APIのテスト」はCoddyKit上の無料Testing Mastery: JUnit, Mockito & Integration Testsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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

よくある質問

「WireMockによる外部APIのテスト」レッスンは無料ですか?

はい。「WireMockによる外部APIのテスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Testing Mastery: JUnit, Mockito & Integration Testsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Testing Mastery: JUnit, Mockito & Integration Testsコースには全4レッスンが含まれています。

「WireMockによる外部APIのテスト」で何を学びますか?

ネットワークへ接続する代わりにWireMockで外部HTTPサービスをスタブ化し、そのサービスを呼び出すコードを統合テストします。 ブラウザで直接実行するハンズオンコードでTesting Mastery: JUnit, Mockito & Integration Testsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Testing Mastery: JUnit, Mockito & Integration Testsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのTesting Mastery: JUnit, Mockito & Integration Testsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「WireMockによる外部APIのテスト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このTesting Mastery: JUnit, Mockito & Integration Testsレッスンでコードを書いて実行できますか?

はい。すべてのTesting Mastery: JUnit, Mockito & Integration Testsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 単体テストと結合テストの違い
  2. 結合テストのセットアップ
  3. データベース連携のテスト
  4. WireMockによる外部APIのテスト
← Testing Mastery: JUnit, Mockito & Integration Testsに戻る