mockserver icon indicating copy to clipboard operation
mockserver copied to clipboard

Dynamic Callback on Expectation

Open 91wzhang opened this issue 2 years ago • 1 comments

#120 Solved a similar issue on the client API. However we have an existing use case as such

// example

List<Expectations> allExpectations; 
MockServerClient mockServerClient;

void createExpectations(List<Integer> valuesFromParam) {
    for (Integer value: valuesFromParam) {
        Expectation e = new Expectation();
        e.thenRespond(
            // ...
        );
        
        // ...

        allExpectations.add(e);
        mockServerClient.upsert(e);
    }
}

The response is built using both info from the method parameter and request as such

{
    "id": request.getBody().getId()
    "value": valueFromParam
}

Previously this was implemented with JS template where we basically construct the response json string. This is no longer supported when we upgrade to JDK 17 due to Nashorn deprecation.

I was looking at alternative solutions. Seems the 1:1 replacement would be using velocity template. Then I was trying to find an easier approach and found #120 which solved half of our problem.

Would you be able to suggest if I can achieve the purpose using the Expectation API? Thanks!

91wzhang avatar Apr 25 '23 22:04 91wzhang

It should be possible using a callback: https://www.mock-server.com/mock_server/creating_expectations.html#button_response_method_or_closure_callback

IvanZhivkov-sumup avatar May 13 '24 16:05 IvanZhivkov-sumup