0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · درس

اختبار واجهات API الخارجية باستخدام WireMock

اختبر تكامليًا الشيفرة التي تستدعي خدمات HTTP خارجية عبر محاكاة تلك الخدمات باستخدام WireMock بدلًا من الاتصال بالشبكة.

اختبار واجهات API الخارجية باستخدام WireMock درس مجاني في Testing Mastery: JUnit, Mockito & Integration Tests على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. اختبارات الوحدات مقابل اختبارات التكامل
  2. إعداد اختبارات التكامل
  3. اختبار تفاعلات قاعدة البيانات
  4. اختبار واجهات API الخارجية باستخدام WireMock
← العودة إلى Testing Mastery: JUnit, Mockito & Integration Tests