Browse Source

Add twice() to ExpectedCount as convenience method

pull/1254/head
Drummond Dawson 9 years ago committed by Rossen Stoyanchev
parent
commit
c9abd99f44
  1. 8
      spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java
  2. 8
      spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java
  3. 13
      spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java
  4. 6
      spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java

8
spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java

@ -26,6 +26,7 @@ import org.springframework.util.Assert; @@ -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 { @@ -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).
*/

8
spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java

@ -31,7 +31,7 @@ import static org.junit.Assert.assertTrue; @@ -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 { @@ -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 { @@ -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 { @@ -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"));

13
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; @@ -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 { @@ -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 { @@ -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"));

6
spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java

@ -31,7 +31,7 @@ import static org.springframework.http.HttpMethod.GET; @@ -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 { @@ -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"));

Loading…
Cancel
Save