@Override public MockRestServiceServer build() { if (this.ignoreExpectOrder) { return build(new UnorderedRequestExpectationManager()); } else { return build(new SimpleRequestExpectationManager()); } }
@Override public void match(ClientHttpRequest request) throws IOException { for (RequestMatcher matcher : getRequestMatchers()) { matcher.match(request); } }
/** * Invoke this for an expectation that has been matched. * <p>The count of the given expectation is incremented, then it is * either stored if remainingCount > 0 or removed otherwise. */ public void update(RequestExpectation expectation) { expectation.incrementAndValidate(); updateInternal(expectation); }
@Test public void match() throws Exception { RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo")); expectation.match(createRequest(GET, "/foo")); }
/** * Set up an expectation for a single HTTP request. The returned * {@link ResponseActions} can be used to set up further expectations as * well as to define the response. * <p>This method may be invoked any number times before starting to make * request through the underlying {@code RestTemplate} in order to set up * all expected requests. * @param matcher request matcher * @return a representation of the expectation */ public ResponseActions expect(RequestMatcher matcher) { return expect(ExpectedCount.once(), matcher); }
@Override protected ClientHttpResponse executeInternal() throws IOException { ClientHttpResponse response = expectationManager.validateRequest(this); setResponse(response); return response; } };
@Override public RequestExpectation matchRequest(ClientHttpRequest request) throws IOException { RequestExpectation expectation = this.remainingExpectations.findExpectation(request); if (expectation == null) { throw createUnexpectedRequestError(request); } this.remainingExpectations.update(expectation); return expectation; }
public boolean isSatisfied() { // Only validate min count since max count is checked on every request... return (getMatchedRequestCount() >= getExpectedCount().getMinCount()); } }
public boolean hasRemainingCount() { return (getMatchedRequestCount() < getExpectedCount().getMaxCount()); }
/** * 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() { this.expectationManager.verify(); }
@Override public boolean hasRemainingCount() { return getRequestCount().hasRemainingCount(); }
@Override public void incrementAndValidate() { getRequestCount().incrementAndValidate(); }
@Override public boolean isSatisfied() { return getRequestCount().isSatisfied(); }
@Override protected void afterExpectationsDeclared() { this.remainingExpectations.addAllExpectations(getExpectations()); }
/** * Exactly twice. */ public static ExpectedCount twice() { return new ExpectedCount(2, 2); }
/** * Exactly once. */ public static ExpectedCount once() { return new ExpectedCount(1, 1); }