24
24
25
25
import org .springframework .core .io .ClassPathResource ;
26
26
import org .springframework .core .io .Resource ;
27
+ import org .springframework .http .HttpMethod ;
27
28
import org .springframework .http .HttpRequest ;
28
29
import org .springframework .http .MediaType ;
29
30
import org .springframework .http .client .ClientHttpRequestExecution ;
36
37
37
38
import static org .assertj .core .api .Assertions .assertThat ;
38
39
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 ;
41
40
import static org .springframework .test .web .client .ExpectedCount .manyTimes ;
42
41
import static org .springframework .test .web .client .ExpectedCount .never ;
43
42
import static org .springframework .test .web .client .ExpectedCount .once ;
@@ -70,8 +69,8 @@ public void performGet() {
70
69
71
70
String responseBody = "{\" name\" : \" Ludwig van Beethoven\" , \" someDouble\" : \" 1.6035\" }" ;
72
71
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 ));
75
74
76
75
this .restTemplate .getForObject ("/composers/{id}" , Person .class , 42 );
77
76
@@ -87,7 +86,7 @@ public void performGetManyTimes() {
87
86
88
87
String responseBody = "{\" name\" : \" Ludwig van Beethoven\" , \" someDouble\" : \" 1.6035\" }" ;
89
88
90
- this .mockServer .expect (manyTimes (), requestTo ("/composers/42" )).andExpect (method (GET ))
89
+ this .mockServer .expect (manyTimes (), requestTo ("/composers/42" )).andExpect (method (HttpMethod . GET ))
91
90
.andRespond (withSuccess (responseBody , MediaType .APPLICATION_JSON ));
92
91
93
92
this .restTemplate .getForObject ("/composers/{id}" , Person .class , 42 );
@@ -108,9 +107,9 @@ public void expectNever() {
108
107
109
108
String responseBody = "{\" name\" : \" Ludwig van Beethoven\" , \" someDouble\" : \" 1.6035\" }" ;
110
109
111
- this .mockServer .expect (once (), requestTo ("/composers/42" )).andExpect (method (GET ))
110
+ this .mockServer .expect (once (), requestTo ("/composers/42" )).andExpect (method (HttpMethod . GET ))
112
111
.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 ))
114
113
.andRespond (withSuccess (responseBody , MediaType .APPLICATION_JSON ));
115
114
116
115
this .restTemplate .getForObject ("/composers/{id}" , Person .class , 42 );
@@ -123,9 +122,9 @@ public void expectNeverViolated() {
123
122
124
123
String responseBody = "{\" name\" : \" Ludwig van Beethoven\" , \" someDouble\" : \" 1.6035\" }" ;
125
124
126
- this .mockServer .expect (once (), requestTo ("/composers/42" )).andExpect (method (GET ))
125
+ this .mockServer .expect (once (), requestTo ("/composers/42" )).andExpect (method (HttpMethod . GET ))
127
126
.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 ))
129
128
.andRespond (withSuccess (responseBody , MediaType .APPLICATION_JSON ));
130
129
131
130
this .restTemplate .getForObject ("/composers/{id}" , Person .class , 42 );
@@ -138,8 +137,8 @@ public void performGetWithResponseBodyFromFile() {
138
137
139
138
Resource responseBody = new ClassPathResource ("ludwig.json" , this .getClass ());
140
139
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 ));
143
142
144
143
this .restTemplate .getForObject ("/composers/{id}" , Person .class , 42 );
145
144
@@ -152,19 +151,25 @@ public void performGetWithResponseBodyFromFile() {
152
151
@ Test
153
152
public void verify () {
154
153
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 ));
157
159
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 ));
160
162
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 ));
163
165
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"
165
169
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"
168
173
169
174
try {
170
175
this .mockServer .verify ();
@@ -187,7 +192,7 @@ public void repeatedAccessToResponseViaResource() {
187
192
.bufferContent () // enable repeated reads of response body
188
193
.build ();
189
194
190
- mockServer .expect (requestTo ("/composers/42" )).andExpect (method (GET ))
195
+ mockServer .expect (requestTo ("/composers/42" )).andExpect (method (HttpMethod . GET ))
191
196
.andRespond (withSuccess (resource , MediaType .APPLICATION_JSON ));
192
197
193
198
restTemplate .getForObject ("/composers/{id}" , Person .class , 42 );
@@ -207,7 +212,7 @@ private ContentInterceptor(Resource resource) {
207
212
208
213
@ Override
209
214
public ClientHttpResponse intercept (HttpRequest request , byte [] body ,
210
- ClientHttpRequestExecution execution ) throws IOException {
215
+ ClientHttpRequestExecution execution ) throws IOException {
211
216
212
217
ClientHttpResponse response = execution .execute (request , body );
213
218
byte [] expected = FileCopyUtils .copyToByteArray (this .resource .getInputStream ());
0 commit comments