Prompt

How do I mock an internal service for contract testing?

Technology / Testing & QA1 observationLast seen Oct 6, 2026

Latest observation

Oct 6, 2026OpenAI APIWeb search: off

To mock an internal service for contract testing, the goal is usually not to create a generic fake, but to provide a predictable implementation that obeys the agreed API contract so your consumer tests can run without the real service.

Typical approaches

1) Use a contract-testing tool that generates mocks

If you’re doing consumer-driven contract testing, tools like:

  • Pact
  • Spring Cloud Contract
  • WireMock (often paired with contract definitions)

can generate a mock server from the contract or stubs from recorded interactions.

Best when: you want the mock to be derived from the contract automatically.


2) Build a stub server for the internal service

Create a small HTTP server that returns canned responses for the endpoints your consumer uses.

Example responsibilities:

  • Return expected success responses
  • Return error cases your consumer needs to handle
  • Match on request path, method, headers, and body
  • Keep response schemas aligned with the contract

Best when: you need simple, controlled behavior and don’t want full tooling.


3) Mock at the client boundary in your tests

If you only need unit tests, mock the service client interface rather than the service itself.

Example:

  • In Java: mock the InternalServiceClient
  • In Python: patch the HTTP client call
  • In JavaScript: mock the API wrapper module

Best when: you want fast isolated tests, not full contract verification.


Good practice for contract testing

For contract tests, the mock should:

  • Mirror the real API shape: endpoints, request/response schema, status codes
  • Be strict enough to catch breaking changes
  • Only implement interactions actually used by the consumer
  • Avoid business logic unless the contract requires it
  • Be versioned with the contract

Example with WireMock

You can stub responses like this:

{
  "request": {
    "method": "GET",
    "urlPath": "/users/123"
  },
  "response": {
    "status": 200,
    "body": "{\"id\":123,\"name\":\"Alice\"}",
    "headers": {
      "Content-Type": "application/json"
    }
  }
}

Then your consumer test hits the mock instead of the real service.


Example with Pact conceptually

  1. Consumer test defines expected interaction.
  2. Pact creates a contract file.
  3. Provider verification checks the real service against the contract.
  4. Consumer can run against the Pact mock server locally.

Recommendation

If your goal is contract testing, use one of these:

  • Pact if you want consumer-driven contracts
  • Spring Cloud Contract if you’re in the Spring ecosystem
  • WireMock if you want lightweight HTTP stubs

If you want, I can show you:

  • a specific example in your language/framework
  • how to set up Pact or WireMock
  • how to mock an internal gRPC service instead of HTTP

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.