Dynamic Callback on Expectation
#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!
It should be possible using a callback: https://www.mock-server.com/mock_server/creating_expectations.html#button_response_method_or_closure_callback