1

I'm working on my integration-tests and I want to mock API calls with Java's MockServer. MockServersClient have a method which mocks response but only by string as a parameter. Example:

new MockServerClient("localhost", 1080)
    .when(
        request()
    )
    .respond(
        response()
            .withBody("some_response_body")
    );

I have a service method that returns object I want as a response and I planned to access data by calling that service method and then pass it as response to already mentioned MockServer's method in .withBody(). Something like:

new MockServerClient("localhost", 1080)
    .when(
        request()
    )
    .respond(
        response()
            .withBody(new MyServiceClass().callMyServiceMethod())
    );

I guess I need to convert that response I get from service method but how?

amerr-k
  • 39
  • 7

1 Answers1

1

You'll need to convert the mocked response to the type of the actual response.

So, if you have the actual api of the type application/json, you'll need to transform the mocked response as:

new MockServerClient("localhost", 1080)
    .when(
        request()
    )
    .respond(
        response()
            .withBody(convertToJson(new MyServiceClass().callMyServiceMethod()))
    );

One thing to be made sure here is you need to have exact same response objects/dtos as the actual api