From c9abd99f44ab1bcd84baf5dbf90f1bf873800257 Mon Sep 17 00:00:00 2001 From: Drummond Dawson Date: Wed, 30 Nov 2016 22:58:52 -0500 Subject: [PATCH] Add twice() to ExpectedCount as convenience method --- .../test/web/client/ExpectedCount.java | 8 ++++++++ .../web/client/DefaultRequestExpectationTests.java | 8 ++++---- .../SimpleRequestExpectationManagerTests.java | 13 ++++++++----- .../UnorderedRequestExpectationManagerTests.java | 6 +++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java b/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java index 09f3077c356..f2bf7373c5f 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java @@ -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). */ diff --git a/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java index aea4f2d8113..7bf490eb13b 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java @@ -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")); diff --git a/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java index d6f2b9cbaab..d808268470c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java @@ -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: but was:"); this.manager.validateRequest(createRequest(POST, "/foo")); diff --git a/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java index 163f3865e67..71b48c548af 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java @@ -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"));