Browse Source

Add ExpectedCount#never()

Issue: SPR-15168
pull/1316/head
Rossen Stoyanchev 9 years ago
parent
commit
c8b0ff2fca
  1. 1
      spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java
  2. 13
      spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java
  3. 33
      spring-test/src/test/java/org/springframework/test/web/client/samples/SampleTests.java

1
spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java

@ -139,6 +139,7 @@ public class DefaultRequestExpectation implements RequestExpectation {
} }
public boolean isSatisfied() { public boolean isSatisfied() {
// Only validate min count since max count is checked on every request...
return (getMatchedRequestCount() >= getExpectedCount().getMinCount()); return (getMatchedRequestCount() >= getExpectedCount().getMinCount());
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,6 +31,7 @@ import org.springframework.util.Assert;
* min(2) * min(2)
* max(4) * max(4)
* between(2, 4) * between(2, 4)
* never()
* </pre> * </pre>
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -48,7 +49,7 @@ public class ExpectedCount {
* See static factory methods in this class. * See static factory methods in this class.
*/ */
private ExpectedCount(int minCount, int maxCount) { private ExpectedCount(int minCount, int maxCount) {
Assert.isTrue(minCount >= 1, "minCount >= 0 is required"); Assert.isTrue(minCount >= 0, "minCount >= 0 is required");
Assert.isTrue(maxCount >= minCount, "maxCount >= minCount is required"); Assert.isTrue(maxCount >= minCount, "maxCount >= minCount is required");
this.minCount = minCount; this.minCount = minCount;
this.maxCount = maxCount; this.maxCount = maxCount;
@ -108,6 +109,14 @@ public class ExpectedCount {
return new ExpectedCount(1, max); return new ExpectedCount(1, max);
} }
/**
* No calls expected at all, i.e. min=0 and max=0.
* @since 4.3.6
*/
public static ExpectedCount never() {
return new ExpectedCount(0, 0);
}
/** /**
* Between {@code min} and {@code max} number of times. * Between {@code min} and {@code max} number of times.
*/ */

33
spring-test/src/test/java/org/springframework/test/web/client/samples/SampleTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,6 +28,8 @@ import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.client.ExpectedCount.manyTimes; import static org.springframework.test.web.client.ExpectedCount.manyTimes;
import static org.springframework.test.web.client.ExpectedCount.never;
import static org.springframework.test.web.client.ExpectedCount.once;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; 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.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@ -92,6 +94,35 @@ public class SampleTests {
this.mockServer.verify(); this.mockServer.verify();
} }
@Test
public void expectNever() throws Exception {
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.mockServer.verify();
}
@Test(expected = AssertionError.class)
public void expectNeverViolated() throws Exception {
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
this.restTemplate.getForObject("/composers/{id}", Person.class, 43);
}
@Test @Test
public void performGetWithResponseBodyFromFile() throws Exception { public void performGetWithResponseBodyFromFile() throws Exception {

Loading…
Cancel
Save