Hi, I think I have a common question but I couldn't find a guide for it so I decide to ask here.
What should we do to write a consumer test for a retrieve endpoint that requires resource ID to be generated automatically on the provider side?
b
Bas Dijkstra
04/26/2025, 6:01 AM
Is that resource ID a path or query parameter in the request, for example, or does it only appear in the response?
If the former, then you could use a provider state parameter that contains the ID (doesn’t matter what the value is, as long as it is valid) and handle that in your provider state handler method. Here’s an example where I do that in Java with a mocked database layer (so I don’t have to actually inject a row into a potentially complex database model): https://github.com/basdijkstra/practical-contract-testing-with-pact-java/blob/3ddef4fb9d91cfb32aa2193c8ffd4deac377a71c/address-provider/src/test/java/provider/ContractVerificationTest.java#L47
If the second, you could specify what you do know about the value. Is it a string? Use a string matcher. Is it a date or a UUID? You could use a regex matcher.
t
Tú Phạm
04/26/2025, 9:07 AM
It's the former case, the resource id is a path parameter of the interaction.
In my case, the provider resources endpoint is running live on a different path than the provider states endpoint, so we can't mock the resource endpoint from the provider state handler. Also the resource repository layer does not allow specifying resource id when creating a new resource, so we can't implement a provider state handler that "creates a new resource with id X", therefore the interaction "retrieve resource with id X" will always return a 404 error on verification.
I can't find a way to work around this situation with Pact.
b
Bas Dijkstra
04/26/2025, 12:11 PM
Yeah, Pact (or rather consumer-driven contract testing) pretty much requires having control over your test data on the provider side.
Is there no way to have your verification tests and your provider implementation in the same code base so you can mock the repository layer?