Is this how we inject an id dynamically into the b...
# pact-jvm
f
Is this how we inject an id dynamically into the body for a consumer?
Copy code
@ExtendWith(PactConsumerTestExt.class)
class PactConsumerSendScoreIT {

    private final Map<String, String> headers = new HashMap<>();
    private final String path = "/v5/assignmentStatus/update";

    @Pact(provider = PACT_PROVIDER, consumer = PACT_CONSUMER)
    public RequestResponsePact scoreConsumerPact(PactDslWithProvider builder) {

        headers.put("Content-Type", "application/json");

        //Body given and returned
        DslPart body = new PactDslJsonBody()
                .valueFromProviderState("assignmentId", "assignmentId", "c1ef3bbf-55a2-4638-8f93-22b2916fe085")
                .stringType("timestamp", DateTime.now().plusHours(3).toString())
                .decimalType("scoreGiven", 75.00)
                .decimalType("scoreMaximum", 100.00)
                .stringType("comment", "Good work!")
                .stringType("status", "IN_PROGRESS")
                .stringType("userId", "c2ef3bbf-55a2-4638-8f93-22b2916fe085")
                .close();

        return builder
                .given("Scoring info is passed between ags-tool and assignmentapi")
                .uponReceiving("Scoring info is passed between ags-tool and assignmentapi")
                .path(path)
                .method("POST")
                .body(body)
                .headers(headers)
                .willRespondWith()
                .status(201)
                .body(body)
                .toPact();

    }

    @Test
    @PactTestFor(pactMethod = "scoreConsumerPact", providerName = PACT_PROVIDER, port = "8080", pactVersion = PactSpecVersion.V3)
    void runTest(MockServer mockServer) {

        String updateAssignmentId = "c2ef3bbf-55a2-4638-8f93-22b2916fe085";

        HashMap<String, Object> map = new HashMap<>();
        map.put("timestamp", DateTime.now().plusHours(3).toString());
        map.put("scoreGiven", 75.00);
        map.put("scoreMaximum", 100.00);
        map.put("comment", "Good work!");
        map.put("status", "IN_PROGRESS");
        map.put("userId", "c2ef3bbf-55a2-4638-8f93-22b2916fe085");
        map.put("assignmentId", updateAssignmentId);

        //Mock url
        RequestSpecification rq = Util.getRequestSpecification().baseUri(mockServer.getUrl()).headers(headers);

        Response response = rq.body(map)
                .post(path);

        assertEquals(201, response.getStatusCode());
    }
b
@Francislainy Campos we figured out that the expression needed to include the ${} at least if it's a string - once we updated it to
valueFromProviderState("assignmentId", "${assignmentId}", "c1ef3bbf-55a2-4638-8f93-22b2916fe085")
it seems to work
🙌 1
f
That’s awesome, Brendan. Thank you for letting me know. I’ve added this to my SO question, so it should be there for future reference. https://stackoverflow.com/questions/74641538/how-to-inject-dynamic-id-for-body-of-pact-contract-written-in-java/75046058#75046058