Add reset() to MockRestServiceServer

Issue: SPR-14306
This commit is contained in:
Rossen Stoyanchev
2016-05-26 11:44:40 -04:00
parent f4ab6d8d52
commit 4e8754ea87
6 changed files with 76 additions and 7 deletions

View File

@@ -133,6 +133,12 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
return new AssertionError(message + getRequestDetails());
}
@Override
public void reset() {
this.expectations.clear();
this.requests.clear();
}
/**
* Helper class to manage a group of request expectations. It helps with
@@ -175,6 +181,10 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
}
return null;
}
public void reset() {
this.expectations.clear();
}
}
}

View File

@@ -121,6 +121,13 @@ public class MockRestServiceServer {
this.expectationManager.verify();
}
/**
* Reset the internal state removing all expectations and recorded requests.
*/
public void reset() {
this.expectationManager.reset();
}
/**
* Return a builder for a {@code MockRestServiceServer} that should be used

View File

@@ -54,4 +54,9 @@ public interface RequestExpectationManager {
*/
void verify();
/**
* Reset the internal state removing all expectations and recorded requests.
*/
void reset();
}

View File

@@ -71,4 +71,10 @@ public class SimpleRequestExpectationManager extends AbstractRequestExpectationM
throw createUnexpectedRequestError(request);
}
@Override
public void reset() {
super.reset();
this.expectationIterator = null;
this.repeatExpectations.reset();
}
}

View File

@@ -48,4 +48,10 @@ public class UnorderedRequestExpectationManager extends AbstractRequestExpectati
throw createUnexpectedRequestError(request);
}
@Override
public void reset() {
super.reset();
this.remainingExpectations.reset();
}
}

View File

@@ -15,11 +15,14 @@
*/
package org.springframework.test.web.client;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@@ -29,27 +32,59 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*/
public class MockRestServiceServerTests {
private RestTemplate restTemplate = new RestTemplate();
@Test
public void buildMultipleTimes() throws Exception {
RestTemplate restTemplate = new RestTemplate();
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate);
MockRestServiceServer server = builder.build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
restTemplate.getForObject("/foo", Void.class);
this.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);
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server = builder.build();
server.expect(requestTo("/bar")).andRespond(withSuccess());
restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/bar", Void.class);
server.verify();
}
@Test
public void resetAndReuseServer() throws Exception {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server.reset();
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
server.verify();
}
@Test
public void resetAndReuseServerWithUnorderedExpectationManager() throws Exception {
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
.ignoreExpectOrder(true).build();
server.expect(requestTo("/foo")).andRespond(withSuccess());
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
server.reset();
server.expect(requestTo("/foo")).andRespond(withSuccess());
server.expect(requestTo("/bar")).andRespond(withSuccess());
this.restTemplate.getForObject("/bar", Void.class);
this.restTemplate.getForObject("/foo", Void.class);
server.verify();
}