MockRestServiceServerBuilder can be re-used
Issue: SPR-14306
This commit is contained in:
@@ -179,34 +179,35 @@ public class MockRestServiceServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builder to create a {@code MockRestServiceServer}.
|
* Builder to create a {@code MockRestServiceServer}.
|
||||||
*/
|
*/
|
||||||
public interface MockRestServiceServerBuilder {
|
public interface MockRestServiceServerBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow expected requests to be executed in any order not necessarily
|
* Whether to allow expected requests to be executed in any order not
|
||||||
* matching the order of declaration. This is a shortcut for:<br>
|
* necessarily matching the order of declaration.
|
||||||
* {@code builder.expectationManager(new UnorderedRequestExpectationManager)}
|
*
|
||||||
|
* <p>When set to "true" this is effectively a shortcut for:<br>
|
||||||
|
* {@code builder.build(new UnorderedRequestExpectationManager)}.
|
||||||
|
*
|
||||||
|
* @param ignoreExpectOrder whether to ignore the order of expectations
|
||||||
*/
|
*/
|
||||||
MockRestServiceServerBuilder ignoreExpectOrder();
|
MockRestServiceServerBuilder ignoreExpectOrder(boolean ignoreExpectOrder);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configure a custom {@code RequestExpectationManager}.
|
* Build the {@code MockRestServiceServer} and set up the underlying
|
||||||
* <p>By default {@link SimpleRequestExpectationManager} is used. It is
|
|
||||||
* also possible to switch to {@link UnorderedRequestExpectationManager}
|
|
||||||
* by setting {@link #ignoreExpectOrder()}.
|
|
||||||
*/
|
|
||||||
MockRestServiceServerBuilder expectationManager(RequestExpectationManager manager);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Build the {@code MockRestServiceServer} and setting up the underlying
|
|
||||||
* {@code RestTemplate} or {@code AsyncRestTemplate} with a
|
* {@code RestTemplate} or {@code AsyncRestTemplate} with a
|
||||||
* {@link ClientHttpRequestFactory} that creates mock requests.
|
* {@link ClientHttpRequestFactory} that creates mock requests.
|
||||||
*/
|
*/
|
||||||
MockRestServiceServer build();
|
MockRestServiceServer build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An overloaded build alternative that accepts a custom
|
||||||
|
* {@link RequestExpectationManager}.
|
||||||
|
*/
|
||||||
|
MockRestServiceServer build(RequestExpectationManager manager);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class DefaultBuilder implements MockRestServiceServerBuilder {
|
private static class DefaultBuilder implements MockRestServiceServerBuilder {
|
||||||
@@ -215,7 +216,7 @@ public class MockRestServiceServer {
|
|||||||
|
|
||||||
private final AsyncRestTemplate asyncRestTemplate;
|
private final AsyncRestTemplate asyncRestTemplate;
|
||||||
|
|
||||||
private RequestExpectationManager expectationManager = new SimpleRequestExpectationManager();
|
private boolean ignoreExpectOrder;
|
||||||
|
|
||||||
|
|
||||||
public DefaultBuilder(RestTemplate restTemplate) {
|
public DefaultBuilder(RestTemplate restTemplate) {
|
||||||
@@ -232,21 +233,24 @@ public class MockRestServiceServer {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MockRestServiceServerBuilder ignoreExpectOrder() {
|
public MockRestServiceServerBuilder ignoreExpectOrder(boolean ignoreExpectOrder) {
|
||||||
expectationManager(new UnorderedRequestExpectationManager());
|
this.ignoreExpectOrder = true;
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public MockRestServiceServerBuilder expectationManager(RequestExpectationManager manager) {
|
|
||||||
Assert.notNull(manager, "'manager' is required.");
|
|
||||||
this.expectationManager = manager;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MockRestServiceServer build() {
|
public MockRestServiceServer build() {
|
||||||
MockRestServiceServer server = new MockRestServiceServer(this.expectationManager);
|
if (this.ignoreExpectOrder) {
|
||||||
|
return build(new UnorderedRequestExpectationManager());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return build(new SimpleRequestExpectationManager());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MockRestServiceServer build(RequestExpectationManager manager) {
|
||||||
|
MockRestServiceServer server = new MockRestServiceServer(manager);
|
||||||
MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
|
MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
|
||||||
if (this.restTemplate != null) {
|
if (this.restTemplate != null) {
|
||||||
this.restTemplate.setRequestFactory(factory);
|
this.restTemplate.setRequestFactory(factory);
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2016 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.springframework.test.web.client;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||||
|
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link MockRestServiceServer}.
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
|
*/
|
||||||
|
public class MockRestServiceServerTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void buildMultipleTimes() throws Exception {
|
||||||
|
|
||||||
|
RestTemplate restTemplate = new RestTemplate();
|
||||||
|
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
|
||||||
|
|
||||||
|
MockRestServiceServer server = builder.build();
|
||||||
|
server.expect(requestTo("/foo")).andRespond(withSuccess());
|
||||||
|
restTemplate.getForObject("/foo", Void.class);
|
||||||
|
server.verify();
|
||||||
|
|
||||||
|
server = builder.ignoreExpectOrder(true).build();
|
||||||
|
server.expect(requestTo("/foo")).andRespond(withSuccess());
|
||||||
|
server.expect(requestTo("/bar")).andRespond(withSuccess());
|
||||||
|
restTemplate.getForObject("/bar", Void.class);
|
||||||
|
restTemplate.getForObject("/foo", Void.class);
|
||||||
|
server.verify();
|
||||||
|
|
||||||
|
server = builder.build();
|
||||||
|
server.expect(requestTo("/bar")).andRespond(withSuccess());
|
||||||
|
restTemplate.getForObject("/bar", Void.class);
|
||||||
|
server.verify();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -49,7 +49,7 @@ public class SampleTests {
|
|||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.restTemplate = new RestTemplate();
|
this.restTemplate = new RestTemplate();
|
||||||
this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder().build();
|
this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder(true).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -125,10 +125,11 @@ public class SampleTests {
|
|||||||
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
|
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
String result = this.restTemplate.getForObject("/number", String.class);
|
String result1 = this.restTemplate.getForObject("/number", String.class);
|
||||||
// result == "1"
|
// result1 == "1"
|
||||||
|
|
||||||
result = this.restTemplate.getForObject("/number", String.class);
|
@SuppressWarnings("unused")
|
||||||
|
String result2 = this.restTemplate.getForObject("/number", String.class);
|
||||||
// result == "2"
|
// result == "2"
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -5095,7 +5095,7 @@ means requests are allowed to come in any order. Here is an example:
|
|||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
[subs="verbatim,quotes"]
|
[subs="verbatim,quotes"]
|
||||||
----
|
----
|
||||||
server = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder().build();
|
server = MockRestServiceServer.bindTo(restTemplate).ignoreExpectOrder(true).build();
|
||||||
----
|
----
|
||||||
|
|
||||||
Even with unordered requests by default each request is allowed to execute once only.
|
Even with unordered requests by default each request is allowed to execute once only.
|
||||||
|
|||||||
Reference in New Issue
Block a user