Repository navigation
Add ExpectedCount support to MockWebServiceServer - #1945
Open
sharanggupta wants to merge 1 commit into
Open
sharanggupta wants to merge 1 commit into
sharanggupta wants to merge 1 commit into
Conversation
MockWebServiceServer expects every request exactly once, so a client that sends the same request several times needs as many identical expectations, and a request that is sent a variable number of times cannot be expected at all. This commit adds ExpectedCount, modeled after the one of the MockRestServiceServer in Spring Framework, and an expect(ExpectedCount, RequestMatcher) variant. The expectations are still met in the order in which they are recorded, like the SimpleRequestExpectationManager of Spring Framework does: a request is matched against the expectations that already matched a request and can match more first, then against the next expectation. verify() fails if an expectation was met fewer times than its minimum count. As an expectation can now be met more than once, the payload and SOAP envelope matchers and creators transform their Source only once. A ResourceSource can be read only once, which broke the Resource variants of payload(), soapEnvelope(), withPayload() and withSoapEnvelope() on the second request. See spring-projectsgh-1154 Signed-off-by: Sharang Gupta <sharang@sharanggupta.dev>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
ExpectedCountsupport to theMockWebServiceServer, as discussed in #1154, harmonized with theExpectedCountof theMockRestServiceServerin Spring Framework.What it adds
ExpectedCountwith the same factory methods and ranges as Spring Framework:once(),twice(),times(int),min(int),max(int),between(int, int),manyTimes()andnever().MockWebServiceServer.expect(ExpectedCount, RequestMatcher).expect(RequestMatcher)is now a shortcut forexpect(ExpectedCount.once(), requestMatcher).SimpleRequestExpectationManagerof Spring Framework: a request is matched against the expectations that already matched a request and can match more first, then against the next expectation.verify()fails when an expectation was met fewer times than its minimum count.Internals
The package-private
MockSenderConnectionused to be both the expectation and the connection, and cleared its matchers onclose(), so it could not serve more than one request. It is now split into a package-privateRequestExpectation(matchers, response creator and count, implementingResponseActions) and aMockSenderConnectionper request, which finds its expectation insend(). TheMockWebServiceMessageSendernow synchronizes access to its expectations, since matching a request updates more than one piece of state.The existing messages are kept: "No further connections expected" for an extra connection, "Further connection(s) expected" from
verify(), and theIllegalStateExceptionwhen an expectation is recorded after the test started. When a request does not meet an expectation that can be met more than once and no further expectation is left, the mismatch of that expectation is reported (for instance the payload difference), as it is for the first request.One behavior change for existing tests: an expectation now counts only the requests that met it. If a test sends a request that does not match, catches the
AssertionError, and then callsverify(),verify()now fails, because the expectation was not met. Before, the request consumed the expectation andverify()passed. This is also howMockRestServiceServer.verify()behaves after a failed request.verifyFailsAfterARequestThatDidNotMeetTheExpectationpins it, and I can restore the previous behavior if you prefer.A related fix
Once an expectation can be met more than once, its matchers and response creator run more than once. The
PayloadDiffMatcher,SoapEnvelopeDiffMatcher,PayloadMessageCreatorandSoapEnvelopeMessageCreatortransformed theirSourceon every call, but theResourceSourceused by theResourcevariants ofpayload(),soapEnvelope(),withPayload()andwithSoapEnvelope()wraps an input stream that can be read only once. They now transform the source once and reuse the document; the creators still copy it into each message. Three tests cover this and failed before that part of the change.I did not add an unordered mode (Spring Framework's
ignoreExpectOrder). It would build on the sameRequestExpectation, and I can follow up if you want it.Testing
Developed test first: the new tests do not compile against
main(ExpectedCountand the newexpectvariant), and theResourcetests failed with the counting in place until the sources were read once.ExpectedCountTestsfor the ranges and argument checks.MockWebServiceServerTeststhrough aWebServiceTemplate:times,twice,min,max,between,neverwith and without a request, too many and too few requests, the order of the expectations, a range that lets the next expectation match, a request that matches no expectation,verify()after a mismatched request,reset(), and repeatedResourcepayloads and SOAP envelopes.MockSenderConnectionTestsandMockWebServiceMessageSenderTestsadapted to the split../gradlew :spring-ws-test:check :spring-ws-test:javadoc :spring-ws-docs:asciidoctorpasses locally on JDK 25 (112 tests inspring-ws-test, none skipped).