MockRestServiceServerBuilder can be re-used

Issue: SPR-14306
This commit is contained in:
Rossen Stoyanchev
2016-05-26 09:48:16 -04:00
parent a1851845ad
commit f4ab6d8d52
4 changed files with 91 additions and 30 deletions

View File

@@ -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();
}
}

View File

@@ -49,7 +49,7 @@ public class SampleTests {
@Before
public void setup() {
this.restTemplate = new RestTemplate();
this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder().build();
this.mockServer = MockRestServiceServer.bindTo(this.restTemplate).ignoreExpectOrder(true).build();
}
@Test
@@ -125,10 +125,11 @@ public class SampleTests {
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
@SuppressWarnings("unused")
String result = this.restTemplate.getForObject("/number", String.class);
// result == "1"
String result1 = this.restTemplate.getForObject("/number", String.class);
// result1 == "1"
result = this.restTemplate.getForObject("/number", String.class);
@SuppressWarnings("unused")
String result2 = this.restTemplate.getForObject("/number", String.class);
// result == "2"
try {