Add twice() to ExpectedCount as convenience method

This commit is contained in:
Drummond Dawson 2016-11-30 22:58:52 -05:00 committed by Rossen Stoyanchev
parent 9a7028ad0d
commit c9abd99f44
4 changed files with 23 additions and 12 deletions

View File

@ -26,6 +26,7 @@ import org.springframework.util.Assert;
* import static org.springframework.test.web.client.ExpectedCount.*
*
* once()
* twice()
* manyTimes()
* times(5)
* min(2)
@ -77,6 +78,13 @@ public class ExpectedCount {
return new ExpectedCount(1, 1);
}
/**
* Exactly twice.
*/
public static ExpectedCount twice() {
return new ExpectedCount(2, 2);
}
/**
* Many times (range of 1..Integer.MAX_VALUE).
*/

View File

@ -31,7 +31,7 @@ import static org.junit.Assert.assertTrue;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.ExpectedCount.twice;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@ -53,7 +53,7 @@ public class DefaultRequestExpectationTests {
}
@Test
public void matchWithFailedExpection() throws Exception {
public void matchWithFailedExpectation() throws Exception {
RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo"));
expectation.andExpect(method(POST));
@ -63,7 +63,7 @@ public class DefaultRequestExpectationTests {
@Test
public void hasRemainingCount() throws Exception {
RequestExpectation expectation = new DefaultRequestExpectation(times(2), requestTo("/foo"));
RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo"));
expectation.andRespond(withSuccess());
expectation.createResponse(createRequest(GET, "/foo"));
@ -75,7 +75,7 @@ public class DefaultRequestExpectationTests {
@Test
public void isSatisfied() throws Exception {
RequestExpectation expectation = new DefaultRequestExpectation(times(2), requestTo("/foo"));
RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo"));
expectation.andRespond(withSuccess());
expectation.createResponse(createRequest(GET, "/foo"));

View File

@ -34,6 +34,7 @@ import static org.springframework.test.web.client.ExpectedCount.max;
import static org.springframework.test.web.client.ExpectedCount.min;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.ExpectedCount.twice;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@ -105,13 +106,15 @@ public class SimpleRequestExpectationManagerTests {
@Test
public void repeatedRequests() throws Exception {
this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(3), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(3), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.verify();
}
@ -152,9 +155,9 @@ public class SimpleRequestExpectationManagerTests {
@Test
public void repeatedRequestsNotInOrder() throws Exception {
this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(2), requestTo("/baz")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/baz")).andExpect(method(GET)).andRespond(withSuccess());
this.thrown.expectMessage("Unexpected HttpMethod expected:<GET> but was:<POST>");
this.manager.validateRequest(createRequest(POST, "/foo"));

View File

@ -31,7 +31,7 @@ import static org.springframework.http.HttpMethod.GET;
import static org.springframework.test.web.client.ExpectedCount.max;
import static org.springframework.test.web.client.ExpectedCount.min;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.ExpectedCount.times;
import static org.springframework.test.web.client.ExpectedCount.twice;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@ -76,8 +76,8 @@ public class UnorderedRequestExpectationManagerTests {
@Test
public void repeatedRequests() throws Exception {
this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(times(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.expectRequest(twice(), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess());
this.manager.validateRequest(createRequest(GET, "/bar"));
this.manager.validateRequest(createRequest(GET, "/foo"));