Handling Dynamic Fields: Timestamps, UUIDs, and Tokens

Learn how to handle dynamic fields like timestamps, UUIDs, and tokens in your integration tests using StableMock's automatic detection and manual ignore patterns.

One of the biggest challenges in record-replay testing is dynamic data. When you record an HTTP interaction, the request often contains fields that change every time, such as:

  • Creating timestamps (e.g., "createdAt": "2026-05-20T10:00:00Z")
  • Generating unique request IDs (UUIDs)
  • Randomized authentication tokens or nonces

If you replay this test later, your application will generate a new timestamp or ID. By default, WireMock will reject the request because the body strictly doesn't match the recorded stub.

StableMock solves this with two powerful strategies: Automatic Detection and Manual Ignore Patterns.

Strategy 1: Automatic Dynamic Field Detection

StableMock can intelligently analyze your traffic to figure out which fields are dynamic. It does this by comparing multiple executions of the same test.

How It Works

  1. You run the test in RECORD mode.
  2. StableMock notices that you sent the "same" request twice, but certain fields (like `timestamp`) changed.
  3. It generates a detected-fields.json analysis file.
  4. In PLAYBACK mode, it automatically ignores those specific JSON paths or regex patterns.

Example: Detecting Changing UUIDs

Here is a test that sends requests with a random requestId. StableMock detects that this field is volatile and should be ignored during matching.

@U(urls = { "https://api.example.com" })
@SpringBootTest
class DynamicFieldTest extends BaseStableMockTest {

    @Autowired
    private MyApiClient client;

    @Test
    void testAutoDetection() throws InterruptedException {
        // 1. Send first request with a random ID
        client.sendRequest(new Request(UUID.randomUUID()));

        // 2. Send second request with a NEW random ID
        client.sendRequest(new Request(UUID.randomUUID()));

        // StableMock compares the two, sees that 'id' changed but 'body' stayed the same,
        // and marks 'id' as a dynamic field to be ignored.
    }
}

💡 Real World Code: See the full implementation in DynamicFieldDetectionTest.java in the examples repo.

Strategy 2: Manual Ignore Patterns

Sometimes you know exactly which fields will change and you want to explicitly ignore them upfront. You can do this using the ignore attribute of the @U annotation.

The ignore Attribute

You can specify JSON paths (prefixed with json:) or query parameters to exclude from the matching logic.

@U(
    urls = { "https://jsonplaceholder.typicode.com" },
    ignore = {
        "json:timestamp",      // Ignore the 'timestamp' path in JSON body
        "json:meta.requestId", // Ignore nested fields
        "query:traceId"        // Ignore 'traceId' query parameter
    }
)
@SpringBootTest
class ManualIgnoreTest extends BaseStableMockTest {

    @Test
    void testManualIgnore() {
        // This request sends specific headers/bodies that matched the recording,
        // EXCEPT for the timestamp which changes every time.
        // StableMock will allow it because we told it to ignore 'timestamp'.
        client.sendPost(new Post(Instant.now()));
    }
}

💡 Pro Tip: Check out ManualIgnorePatternsTest.java to see this in action.

Which Strategy Should You Use?

Strategy Best For Pros
Automatic Detection Complex objects with many changing fields Zero configuration; learns from behavior
Manual Ignore Known volatile fields (timestamps, nonces) Explicit, deterministic, faster recording

Conclusion

Handling dynamic fields is crucial for brittle-free integration tests. With StableMock, you can either let the tool learn what changes automatically or explicitly define rules, giving you the flexibility to handle any API payload.

Next: Learn how to Master Spring Boot Integration Testing.

Stop Rewriting Mocks By Hand, Son

Record once. Test offline forever.

Join developers who've made the switch. Free forever. No credit card required.

🌾 MIT License | 🚀 Open Source | 💯 Free Forever