Software Testing Engineering / API & CONTRACT TESTING / TST-021

Suggested study path · Step 6 of 6 · G1

Mock Bank Contract

Explore concept graph

Materi

Problem: provider transfer mengubah bentuk response tanpa mengubah endpoint. Bagaimana consumer membuktikan bahwa perubahan itu mematahkan data yang benar-benar dibacanya?

Mulai: identifikasi empat bagian expectation: status HTTP, amount string, currency IDR, dan error Problem Details ketika transfer tidak ditemukan.

Peta konsep statis. Ikuti alur teks dari atas ke bawah.
      Transfer client
             |
       Pact mock test
             |
      Generated pact
             |
        Pact Verifier
          /      \
  Healthy local  Mutated local
   Node provider  Node provider
         |          |
       PASS      amount:number
                    |
                 FAIL (expected)

Consumer expectation

Operation HTTP Contract fields
GET /transfers/tr-001 200 transferId, amount string, currency IDR, status PENDING
GET /transfers/missing 404 Content-Type application/problem+json, type, title, status

Kenapa tidak full E2E? Contract testing membuktikan kecocokan interface yang dipakai consumer. Untuk perubahan JSON type, pengujian provider Pact lebih cepat dan lebih terisolasi daripada men-deploy seluruh bank simulator. Bukan berarti integrasi dan security test bisa dihapus untuk alur pembayaran sesungguhnya.

Konsep 1 / 3

Define one real Pact consumer contract for the transfer 200 success and a stable 404 error.

An integration breaks when a provider changes fields that a consumer actually reads, even if other provider endpoints still function.

Mulai dari sini

Inspect the SYS-008 GET transfer 200 and 404 examples.

Intuisi utama

The consumer owns its expectation; Pact mocks the provider while the consumer client makes actual HTTP requests.

The lab consumer calls GET /transfers/tr-001 and expects 200, string amount, IDR currency and PENDING status; for a missing transfer it expects 404 and RFC 9457 problem type/title/status. Pact records these actual consumer interactions. The error expectation intentionally uses the SYS-008 NotFound response instead of inventing an error code. Authentication is outside this educational fixture.

Contoh bertahap

A transfer detail screen needs a predictable amount and a sensible not-found message.

  1. Write a tiny transfer-reading client and invoke it inside Pact.executeTest.

    Mengapa: It proves the consumer's own code is tested against the mock server.

    Hasil yang diharapkan: The client receives typed transfer fields from Pact's mock server.

  2. Record the 200 and 404 interactions in a Pact contract.

    Mengapa: Provider verification requires concrete consumer expectations, including errors.

    Hasil yang diharapkan: One Pact file records two different HTTP interactions.

Konsep 2 / 3

Replay the generated Pact through the real Pact provider verifier against a local HTTP provider.

A generated contract by itself does not prove any provider version serves compatible responses.

Mulai dari sini

Locate the generated Pact file and the Verifier's providerBaseUrl option.

Intuisi utama

Consumer mock tests create expectations; provider verification separately invokes a running provider.

The provider verifier reads the Pact file and sends requests to a temporary Node HTTP server; stateHandlers prepare the transfer-present and transfer-missing cases. Green means this local educational provider satisfies the selected consumer interactions. No actual bank, Spring MVC, JWT, database or deployed service is involved.

Contoh bertahap

A Pact file exists but no provider has run the verification.

  1. Start a local provider bound to a random 127.0.0.1 port.

    Mengapa: The test must observe real HTTP response behavior instead of a hand-written simulation.

    Hasil yang diharapkan: A local server serves the known transfer and not-found error.

  2. Run Pact Verifier with pactUrls and state handlers.

    Mengapa: The verifier replays exactly the consumer's recorded interactions against the provider.

    Hasil yang diharapkan: Both success and not-found interactions pass Pact verification.

Konsep 3 / 3

Catch a breaking provider amount type mutation before allowing a release.

A provider can silently serialize a decimal amount as a JSON number instead of the consumer's string representation.

Mulai dari sini

Read the amount matcher in the consumer's 200 interaction.

Intuisi utama

The same unchanged Pact must pass a healthy provider and fail a mutated provider.

The lab makes exactly one provider behavior change: amount becomes a JSON number instead of the declared decimal string. Pact's type matcher requires string; its verifier must reject the changed provider response. The negative test is an expected failure assertion, not an accidentally red CI job. Test both positive and intentionally negative verification and distinguish a local provider stub from a real production system.

Contoh bertahap

A provider refactor changes amount from a JSON string to the numeric value 125000.

  1. Keep the original Pact untouched and start a second provider with numeric amount.

    Mengapa: The negative control must test a provider regression rather than replacing the consumer contract.

    Hasil yang diharapkan: The emitted response contains a number rather than a string.

  2. Replay the exact Pact again and assert the verifier rejects the mutated provider.

    Mengapa: Failing provider verification blocks incompatible behavior before deployment.

    Hasil yang diharapkan: The mutated provider fails while the healthy provider previously passed.

Sumber primer

Sumber

Cross-domain connections

Curated relationships for this unit at G1. Proposed edges are suggestions, not prerequisites.

Concept connectionsG1 · 2 visible concepts · 2 curated links
100%

Dashed = proposed relationship; solid = reviewed connection. Only reviewed prerequisites constrain a learning path. Keyboard and text links remain available.