Skip to content

Commit bc88402

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

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
@@ -137,6 +137,7 @@ public boolean hasRemainingCount() {
137137
}
138138

139139
public boolean isSatisfied() {
140+
// Only validate min count since max count is checked on every request...
140141
return (getMatchedRequestCount() >= getExpectedCount().getMinCount());
141142
}
142143
}

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.
@@ -32,6 +32,7 @@
3232
* min(2)
3333
* max(4)
3434
* between(2, 4)
35+
* never()
3536
* </pre>
3637
*
3738
* @author Rossen Stoyanchev
@@ -49,7 +50,7 @@ public class ExpectedCount {
4950
* See static factory methods in this class.
5051
*/
5152
private ExpectedCount(int minCount, int maxCount) {
52-
Assert.isTrue(minCount >= 1, "minCount >= 0 is required");
53+
Assert.isTrue(minCount >= 0, "minCount >= 0 is required");
5354
Assert.isTrue(maxCount >= minCount, "maxCount >= minCount is required");
5455
this.minCount = minCount;
5556
this.maxCount = maxCount;
@@ -116,6 +117,14 @@ public static ExpectedCount max(int max) {
116117
return new ExpectedCount(1, max);
117118
}
118119

120+
/**
121+
* No calls expected at all, i.e. min=0 and max=0.
122+
* @since 4.3.6
123+
*/
124+
public static ExpectedCount never() {
125+
return new ExpectedCount(0, 0);
126+
}
127+
119128
/**
120129
* Between {@code min} and {@code max} number of times.
121130
*/

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)