|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
28 | 28 |
|
29 | 29 | import static org.junit.Assert.assertTrue;
|
30 | 30 | 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; |
31 | 33 | import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
|
32 | 34 | import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
33 | 35 | import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
@@ -92,6 +94,35 @@ public void performGetManyTimes() throws Exception {
|
92 | 94 | this.mockServer.verify();
|
93 | 95 | }
|
94 | 96 |
|
| 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 | + |
95 | 126 | @Test
|
96 | 127 | public void performGetWithResponseBodyFromFile() throws Exception {
|
97 | 128 |
|
|
0 commit comments