liveBook · Manning

Mocking

As explained in Build Financial Software with Generative AI (From Scratch)

Mocking is a testing and development technique that replaces real data or dependencies with controlled predefined substitutes.

Mocking replaces an unavailable or unsuitable dependency with a predefined substitute. It lets a UI call an expected API before the real backend is ready, so frontend and backend work can proceed in parallel. section 6.5

Use mocked responses to develop against a stable contract, exercise error responses or edge-case data, and check how a screen handles values that may be difficult to obtain from a live service. Start with hardcoded rows to shape the interface, then replace them with a mocked API call. section 6.5 section 6.4

A WireMock-based workflow keeps the substitute close to the API interaction:

  • Define a stub mapping that matches a request method and URL, then returns a status, headers, and body. section 6.5.2
  • Store reusable JSON response data in the __files directory and point a mapping's bodyFileName to it. section 6.5.2
  • Refresh WireMock mappings after changes, then call the configured endpoint to verify the response. section 6.5.2

Mock recent ACH uploads

A GET request to /api/v1/files can return a JSON array of uploaded ACH files with an id, date, filename, credit total, and debit total. The UI can render and format these values while the real API is still unavailable. section 6.5.2 section 6.4

Choose the substitute according to what the test must prove. Mocking can remove a database dependency, but it is not appropriate when the purpose is to verify that records were actually written to the database. For that case, consider an isolated in-memory database or a managed test PostgreSQL database instead. section 5.2.1

Listing 6.24 ACH response mapping
{
    "request": {
        "method": "GET",         #1
        "url": "/api/v1/files"   #2
    },
    "response": {
        "status": 200,           #3
        "headers": {                           #4
            "Content-Type": "application/json"  #4
        },                                      #4
        "bodyFileName": "ach_files_response.json"  #5
    }
}

This mapping shows the contract between a GET request for ACH files and a JSON body stored separately as a reusable response file. section 6.5.2

How Build Financial Software with Generative AI (From Scratch) covers it

How deep it goes
Walks through WireMock stubs, external response files, mappings, and mapping refreshes, while deferring broader WireMock features.
Examples
Uses recent ACH-upload rows, a Hello World stub, and a GET /api/v1/files JSON response.
What it assumes
Builds on hardcoded ACH-upload UI rows and introduces the WireMock setup needed to replace them with an API call.
What you can do after
Apply it via WireMock to return predefined API responses for UI development and error or edge-case scenarios.
How it connects
Connects API calls, hardcoded data, WireMock, UI development, edge-case testing, and database testing.

Also explained in

Compare how 21 books explain mocking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Financial Software with Generative AI (From Scratch). The section links point to the parts of the book it draws on.