Mock Rest API call in spring boot filter

285 Views Asked by At

i have a spring boot microservice with 4 endpoints, i have a custom filter which extends OncePerRequestFilter, the logic in the filter is as below: call a Rest API with some query params, based on the response of the API, either throw a 403 Forbidden or continue with further execution (if everything is fine with the response). the filter works as expected with multiple URI's, now how can i mock the response of the Rest API call in filter while writing integration tests.

i do have some external calls which i mock using WireMock (sample below)

wireMockServer = new WireMockServer(port);
wireMockServer.start();
WireMock.configureFor("localhost", port);

WireMock.stubFor(
    WireMock.get(WireMock.urlPathMatching("/path/.*"))
        .willReturn(
            WireMock.aResponse()
                .withStatus(200)
                .withBody(new ObjectMapper().writeValueAsString(response))));

this does not help me while trying to mock the external call in the filter.

Any pointers would be helpful. Thanks!

0

There are 0 best solutions below