1+ package com .baeldung ;
2+
3+ import java .io .IOException ;
4+ import java .util .HashMap ;
5+ import java .util .Map ;
6+
7+ import org .junit .runner .RunWith ;
8+ import org .springframework .boot .test .IntegrationTest ;
9+ import org .springframework .boot .test .SpringApplicationContextLoader ;
10+ import org .springframework .http .HttpMethod ;
11+ import org .springframework .http .client .ClientHttpResponse ;
12+ import org .springframework .test .context .ContextConfiguration ;
13+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
14+ import org .springframework .test .context .web .WebAppConfiguration ;
15+ import org .springframework .web .client .ResponseErrorHandler ;
16+ import org .springframework .web .client .ResponseExtractor ;
17+ import org .springframework .web .client .RestTemplate ;
18+
19+
20+ //@RunWith(SpringJUnit4ClassRunner.class)
21+ @ ContextConfiguration (classes = SpringDemoApplication .class , loader = SpringApplicationContextLoader .class )
22+ @ WebAppConfiguration
23+ @ IntegrationTest
24+ public class SpringIntegrationTest {
25+ protected static ResponseResults latestResponse = null ;
26+
27+ protected RestTemplate restTemplate = null ;
28+
29+ protected void executeGet (String url ) throws IOException {
30+ final Map <String ,String > headers = new HashMap <>();
31+ headers .put ("Accept" ,"application/json" );
32+ final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback (headers );
33+ final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler ();
34+
35+ if (restTemplate == null ){
36+ restTemplate = new RestTemplate ();
37+ }
38+
39+ restTemplate .setErrorHandler (errorHandler );
40+ latestResponse = restTemplate .execute (url ,
41+ HttpMethod .GET ,
42+ requestCallback ,
43+ new ResponseExtractor <ResponseResults >(){
44+ @ Override
45+ public ResponseResults extractData (ClientHttpResponse response ) throws IOException {
46+ if (errorHandler .hadError ){
47+ return (errorHandler .getResults ());
48+ } else {
49+ return (new ResponseResults (response ));
50+ }
51+ }
52+ });
53+
54+ }
55+
56+ protected void executePost (String url ) throws IOException {
57+ final Map <String ,String > headers = new HashMap <>();
58+ headers .put ("Accept" ,"application/json" );
59+ final HeaderSettingRequestCallback requestCallback = new HeaderSettingRequestCallback (headers );
60+ final ResponseResultErrorHandler errorHandler = new ResponseResultErrorHandler ();
61+
62+ if (restTemplate == null ){
63+ restTemplate = new RestTemplate ();
64+ }
65+
66+ restTemplate .setErrorHandler (errorHandler );
67+ latestResponse = restTemplate .execute (url ,
68+ HttpMethod .POST ,
69+ requestCallback ,
70+ new ResponseExtractor <ResponseResults >(){
71+ @ Override
72+ public ResponseResults extractData (ClientHttpResponse response ) throws IOException {
73+ if (errorHandler .hadError ){
74+ return (errorHandler .getResults ());
75+ } else {
76+ return (new ResponseResults (response ));
77+ }
78+ }
79+ });
80+
81+ }
82+
83+ private class ResponseResultErrorHandler implements ResponseErrorHandler {
84+ private ResponseResults results = null ;
85+ private Boolean hadError = false ;
86+
87+ private ResponseResults getResults (){
88+ return results ;
89+ }
90+
91+ @ Override
92+ public boolean hasError (ClientHttpResponse response ) throws IOException {
93+ hadError = response .getRawStatusCode () >= 400 ;
94+ return hadError ;
95+ }
96+
97+ @ Override
98+ public void handleError (ClientHttpResponse response ) throws IOException {
99+ results = new ResponseResults (response );
100+ }
101+ }
102+ }
0 commit comments