Skip to content

Commit c8b0ff2

Browse files
committed
Add ExpectedCount#never()
Issue: SPR-15168
1 parent f9a4856 commit c8b0ff2

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public boolean hasRemainingCount() {
139139
}
140140

141141
public boolean isSatisfied() {
142+
// Only validate min count since max count is checked on every request...
142143
return (getMatchedRequestCount() >= getExpectedCount().getMinCount());
143144
}
144145
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* min(2)
3232
* max(4)
3333
* between(2, 4)
34+
* never()
3435
* </pre>
3536
*
3637
* @author Rossen Stoyanchev
@@ -48,7 +49,7 @@ public class ExpectedCount {
4849
* See static factory methods in this class.
4950
*/
5051
private ExpectedCount(int minCount, int maxCount) {
51-
Assert.isTrue(minCount >= 1, "minCount >= 0 is required");
52+
Assert.isTrue(minCount >= 0, "minCount >= 0 is required");
5253
Assert.isTrue(maxCount >= minCount, "maxCount >= minCount is required");
5354
this.minCount = minCount;
5455
this.maxCount = maxCount;
@@ -108,6 +109,14 @@ public static ExpectedCount max(int max) {
108109
return new ExpectedCount(1, max);
109110
}
110111

112+
/**
113+
* No calls expected at all, i.e. min=0 and max=0.
114+
* @since 4.3.6
115+
*/
116+
public static ExpectedCount never() {
117+
return new ExpectedCount(0, 0);
118+
}
119+
111120
/**
112121
* Between {@code min} and {@code max} number of times.
113122
*/

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@
2828

2929
import static org.junit.Assert.assertTrue;
3030
import static org.springframework.test.web.client.ExpectedCount.manyTimes;
31+
import static org.springframework.test.web.client.ExpectedCount.never;
32+
import static org.springframework.test.web.client.ExpectedCount.once;
3133
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
3234
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
3335
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@@ -92,6 +94,35 @@ public void performGetManyTimes() throws Exception {
9294
this.mockServer.verify();
9395
}
9496

97+
@Test
98+
public void expectNever() throws Exception {
99+
100+
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
101+
102+
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
103+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
104+
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
105+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
106+
107+
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
108+
109+
this.mockServer.verify();
110+
}
111+
112+
@Test(expected = AssertionError.class)
113+
public void expectNeverViolated() throws Exception {
114+
115+
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
116+
117+
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
118+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
119+
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
120+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
121+
122+
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
123+
this.restTemplate.getForObject("/composers/{id}", Person.class, 43);
124+
}
125+
95126
@Test
96127
public void performGetWithResponseBodyFromFile() throws Exception {
97128

0 commit comments

Comments
 (0)