Class DefaultRequestExpectation
- java.lang.Object
- org.springframework.test.web.client.DefaultRequestExpectation
- All Implemented Interfaces:
RequestExpectation
,RequestMatcher
,ResponseActions
,ResponseCreator
public class DefaultRequestExpectation extends Object implements RequestExpectation
Default implementation ofRequestExpectation
that simply delegates to the request matchers and the response creator it contains.- Since:
- 4.3
- Author:
- Rossen Stoyanchev
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
DefaultRequestExpectation.RequestCount
Helper class that keeps track of actual vs expected request count.
Constructor Summary
Constructors Constructor Description DefaultRequestExpectation(ExpectedCount expectedCount, RequestMatcher requestMatcher)
Create a new request expectation that should be called a number of times as indicated byRequestCount
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResponseActions
andExpect(RequestMatcher requestMatcher)
Add a request expectation.void
andRespond(ResponseCreator responseCreator)
Define the response.ClientHttpResponse
createResponse(ClientHttpRequest request)
Note that as of 5.0.3, the creation of the response, which may block intentionally, is separated from request count tracking, and this method no longer increments the count transparently.protected DefaultRequestExpectation.RequestCount
getRequestCount()
protected List<RequestMatcher>
getRequestMatchers()
protected ResponseCreator
getResponseCreator()
boolean
hasRemainingCount()
Whether there is a remaining count of invocations for this expectation.void
incrementAndValidate()
Increase the matched request count and check we haven't passed the max count.boolean
isSatisfied()
Whether the requirements for this request expectation have been met.void
match(ClientHttpRequest request)
Match the given request against specific expectations.
Constructor Detail
DefaultRequestExpectation
public DefaultRequestExpectation(ExpectedCount expectedCount, RequestMatcher requestMatcher)
Create a new request expectation that should be called a number of times as indicated byRequestCount
.- Parameters:
expectedCount
- the expected request expectedCount
Method Detail
getRequestCount
protected DefaultRequestExpectation.RequestCount getRequestCount()
getRequestMatchers
protected List<RequestMatcher> getRequestMatchers()
getResponseCreator
@Nullable protected ResponseCreator getResponseCreator()
andExpect
public ResponseActions andExpect(RequestMatcher requestMatcher)
Description copied from interface:ResponseActions
Add a request expectation.- Specified by:
andExpect
in interfaceResponseActions
- Returns:
- the expectation
andRespond
public void andRespond(ResponseCreator responseCreator)
Description copied from interface:ResponseActions
Define the response.- Specified by:
andRespond
in interfaceResponseActions
- Parameters:
responseCreator
- the creator of the response
match
public void match(ClientHttpRequest request) throws IOException
Description copied from interface:RequestMatcher
Match the given request against specific expectations.- Specified by:
match
in interfaceRequestMatcher
- Parameters:
request
- the request to make assertions on- Throws:
IOException
- in case of I/O errors
createResponse
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException
Note that as of 5.0.3, the creation of the response, which may block intentionally, is separated from request count tracking, and this method no longer increments the count transparently. InsteadincrementAndValidate()
must be invoked independently.- Specified by:
createResponse
in interfaceResponseCreator
- Parameters:
request
- the request- Throws:
IOException
hasRemainingCount
public boolean hasRemainingCount()
Description copied from interface:RequestExpectation
Whether there is a remaining count of invocations for this expectation.- Specified by:
hasRemainingCount
in interfaceRequestExpectation
incrementAndValidate
public void incrementAndValidate()
Description copied from interface:RequestExpectation
Increase the matched request count and check we haven't passed the max count.- Specified by:
incrementAndValidate
in interfaceRequestExpectation
isSatisfied
public boolean isSatisfied()
Description copied from interface:RequestExpectation
Whether the requirements for this request expectation have been met.- Specified by:
isSatisfied
in interfaceRequestExpectation