MockRestServiceServer does not validate request

I am trying to write an integration test for my spring integration thread. I wanted to use MockRestServiceServer to record and map outgoing requests (using http: outgoing-gateway) on the Rest server. However, when I call the mockServer validation method, it does not validate as expected.

I write my tests as follows:

RestTemplate restTemplate = new RestTemplate();
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);

mockServer.expect(requestTo("adfasfadf.com")).andExpect(method(HttpMethod.GET));

// Call spring integration flow here

mockServer.verify();

When I test the MockRestServiceServer validation method, it does not call the RequestMatchers matching methods, which, I believe, is something wrong with this logic. Did I miss something?

/**
 * Verify that all expected requests set up via
 * {@link #expect(RequestMatcher)} were indeed performed.
 * @throws AssertionError when some expectations were not met
 */
public void verify() {
    if (this.expectedRequests.isEmpty() || this.expectedRequests.equals(this.actualRequests)) {
        return;
    }
    throw new AssertionError(getVerifyMessage());
}
+4
source share
2 answers

, MockRestServiceServer . , , , . RequestMatcherClientHttpRequest, .

@Override
public ClientHttpResponse executeInternal() throws IOException {
    if (this.requestMatchers.isEmpty()) {
        throw new AssertionError("No request expectations to execute");
    }
    if (this.responseCreator == null) {
        throw new AssertionError("No ResponseCreator was set up. Add it after request expectations, "
                + "e.g. MockRestServiceServer.expect(requestTo(\"/foo\")).andRespond(withSuccess())");
    }
    for (RequestMatcher requestMatcher : this.requestMatchers) {
        requestMatcher.match(this);
    }
    setResponse(this.responseCreator.createResponse(this));

    return super.executeInternal();
}

, , , .

+1

MockRestServiceServer, , . , !

, , :

public static MockRestServiceServer createServer(RestTemplate restTemplate) {
        Assert.notNull(restTemplate, "'restTemplate' must not be null");
        MockRestServiceServer mockServer = new MockRestServiceServer();
        RequestMatcherClientHttpRequestFactory factory = mockServer.new RequestMatcherClientHttpRequestFactory();
        restTemplate.setRequestFactory(factory);
        return mockServer;
    }

, , RequestMatcherClientHttpRequestFactory.

, RequestMatchers , RestTemplate.

, <int-http:outbound-gateway>. RestTemplate MockRestServiceServer.

0

Source: https://habr.com/ru/post/1613250/


All Articles