Skip to content

Commit bd7eb42

Browse files
undo
1 parent 4ddde8d commit bd7eb42

File tree

1 file changed

+27
-22
lines changed
  • spring-test/src/test/java/org/springframework/test/web/client/samples

1 file changed

+27
-22
lines changed

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

+27-22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.core.io.ClassPathResource;
2626
import org.springframework.core.io.Resource;
27+
import org.springframework.http.HttpMethod;
2728
import org.springframework.http.HttpRequest;
2829
import org.springframework.http.MediaType;
2930
import org.springframework.http.client.ClientHttpRequestExecution;
@@ -36,8 +37,6 @@
3637

3738
import static org.assertj.core.api.Assertions.assertThat;
3839
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
39-
import static org.springframework.http.HttpMethod.GET;
40-
import static org.springframework.http.MediaType.TEXT_PLAIN;
4140
import static org.springframework.test.web.client.ExpectedCount.manyTimes;
4241
import static org.springframework.test.web.client.ExpectedCount.never;
4342
import static org.springframework.test.web.client.ExpectedCount.once;
@@ -70,8 +69,8 @@ public void performGet() {
7069

7170
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
7271

73-
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(GET))
74-
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
72+
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
73+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
7574

7675
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
7776

@@ -87,7 +86,7 @@ public void performGetManyTimes() {
8786

8887
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
8988

90-
this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(GET))
89+
this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
9190
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
9291

9392
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
@@ -108,9 +107,9 @@ public void expectNever() {
108107

109108
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
110109

111-
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(GET))
110+
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
112111
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
113-
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(GET))
112+
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
114113
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
115114

116115
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
@@ -123,9 +122,9 @@ public void expectNeverViolated() {
123122

124123
String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";
125124

126-
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(GET))
125+
this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
127126
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
128-
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(GET))
127+
this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
129128
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
130129

131130
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
@@ -138,8 +137,8 @@ public void performGetWithResponseBodyFromFile() {
138137

139138
Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());
140139

141-
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(GET))
142-
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
140+
this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
141+
.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
143142

144143
this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
145144

@@ -152,19 +151,25 @@ public void performGetWithResponseBodyFromFile() {
152151
@Test
153152
public void verify() {
154153

155-
this.mockServer.expect(requestTo("/number")).andExpect(method(GET))
156-
.andRespond(withSuccess("1", TEXT_PLAIN));
154+
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
155+
.andRespond(withSuccess("1", MediaType.TEXT_PLAIN));
156+
157+
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
158+
.andRespond(withSuccess("2", MediaType.TEXT_PLAIN));
157159

158-
this.mockServer.expect(requestTo("/number")).andExpect(method(GET))
159-
.andRespond(withSuccess("2", TEXT_PLAIN));
160+
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
161+
.andRespond(withSuccess("4", MediaType.TEXT_PLAIN));
160162

161-
this.mockServer.expect(requestTo("/number")).andExpect(method(GET))
162-
.andRespond(withSuccess("4", TEXT_PLAIN));
163+
this.mockServer.expect(requestTo("/number")).andExpect(method(HttpMethod.GET))
164+
.andRespond(withSuccess("8", MediaType.TEXT_PLAIN));
163165

164-
this.mockServer.expect(requestTo("/number")).andExpect(method(GET)).andRespond(withSuccess("8", TEXT_PLAIN));
166+
@SuppressWarnings("unused")
167+
String result1 = this.restTemplate.getForObject("/number", String.class);
168+
// result1 == "1"
165169

166-
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("1");
167-
assertThat(this.restTemplate.getForObject("/number", String.class)).isEqualTo("2");
170+
@SuppressWarnings("unused")
171+
String result2 = this.restTemplate.getForObject("/number", String.class);
172+
// result == "2"
168173

169174
try {
170175
this.mockServer.verify();
@@ -187,7 +192,7 @@ public void repeatedAccessToResponseViaResource() {
187192
.bufferContent() // enable repeated reads of response body
188193
.build();
189194

190-
mockServer.expect(requestTo("/composers/42")).andExpect(method(GET))
195+
mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
191196
.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));
192197

193198
restTemplate.getForObject("/composers/{id}", Person.class, 42);
@@ -207,7 +212,7 @@ private ContentInterceptor(Resource resource) {
207212

208213
@Override
209214
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
210-
ClientHttpRequestExecution execution) throws IOException {
215+
ClientHttpRequestExecution execution) throws IOException {
211216

212217
ClientHttpResponse response = execution.execute(request, body);
213218
byte[] expected = FileCopyUtils.copyToByteArray(this.resource.getInputStream());

0 commit comments

Comments
 (0)