Skip to content

Commit

Permalink
Add ExpectedCount#never()
Browse files Browse the repository at this point in the history
Issue: SPR-15168
  • Loading branch information
rstoyanchev committed Jan 20, 2017
1 parent f07a23e commit bc88402
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Expand Up @@ -137,6 +137,7 @@ public boolean hasRemainingCount() {
}

public boolean isSatisfied() {
// Only validate min count since max count is checked on every request...
return (getMatchedRequestCount() >= getExpectedCount().getMinCount());
}
}
Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,6 +32,7 @@
* min(2)
* max(4)
* between(2, 4)
* never()
* </pre>
*
* @author Rossen Stoyanchev
Expand All @@ -49,7 +50,7 @@ public class ExpectedCount {
* See static factory methods in this class.
*/
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");
this.minCount = minCount;
this.maxCount = maxCount;
Expand Down Expand Up @@ -116,6 +117,14 @@ public static ExpectedCount max(int 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.
*/
Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,8 @@

import static org.junit.Assert.assertTrue;
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.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
Expand Down Expand Up @@ -92,6 +94,35 @@ public void performGetManyTimes() throws Exception {
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
public void performGetWithResponseBodyFromFile() throws Exception {

Expand Down

0 comments on commit bc88402

Please sign in to comment.