From c8b0ff2fcaecf3f3a61a00103c62302319ae3306 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 20 Jan 2017 17:11:09 -0500 Subject: [PATCH] Add ExpectedCount#never() Issue: SPR-15168 --- .../web/client/DefaultRequestExpectation.java | 1 + .../test/web/client/ExpectedCount.java | 13 ++++++-- .../test/web/client/samples/SampleTests.java | 33 ++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java b/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java index d51c965f27f..f8d21a79f59 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/DefaultRequestExpectation.java @@ -139,6 +139,7 @@ public class DefaultRequestExpectation implements RequestExpectation { } public boolean isSatisfied() { + // Only validate min count since max count is checked on every request... return (getMatchedRequestCount() >= getExpectedCount().getMinCount()); } } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java b/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java index 09f3077c356..0c441bb7ab8 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/ExpectedCount.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ import org.springframework.util.Assert; * min(2) * max(4) * between(2, 4) + * never() * * * @author Rossen Stoyanchev @@ -48,7 +49,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; @@ -108,6 +109,14 @@ public class ExpectedCount { 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. */ diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/SampleTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/SampleTests.java index 6d5c7452b6f..62285b949b4 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/SampleTests.java +++ b/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"); * 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.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; @@ -92,6 +94,35 @@ public class SampleTests { 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 {