11package org .baeldung .web .test ;
22
3+ import static org .hamcrest .Matchers .notNullValue ;
4+ import static org .junit .Assert .assertThat ;
5+
36import java .util .ArrayList ;
47import java .util .Arrays ;
58import java .util .List ;
1316import org .springframework .http .MediaType ;
1417import org .springframework .http .ResponseEntity ;
1518import org .springframework .http .converter .HttpMessageConverter ;
19+ import org .springframework .http .converter .json .MappingJackson2HttpMessageConverter ;
1620import org .springframework .http .converter .xml .MarshallingHttpMessageConverter ;
1721import org .springframework .oxm .xstream .XStreamMarshaller ;
1822import org .springframework .web .client .RestTemplate ;
@@ -29,20 +33,17 @@ public class SpringHttpMessageConvertersIntegrationTestsCase {
2933 * server (in this case json)
3034 */
3135 @ Test
32- public void testGetFoo () {
36+ public void whenRetrievingAFoo_thenCorrect () {
3337 final String URI = BASE_URI + "foos/{id}" ;
3438
3539 final RestTemplate restTemplate = new RestTemplate ();
3640 final Foo resource = restTemplate .getForObject (URI , Foo .class , "1" );
3741
38- Assert . assertEquals ( 1l , resource . getId ());
42+ assertThat ( resource , notNullValue ());
3943 }
4044
41- /**
42- * Specifying Accept Header with application/xml for getting the xml response from the server.
43- */
4445 @ Test
45- public void testGetFooAcceptXML () {
46+ public void givenConsumingXml_whenReadingTheFoo_thenCorrect () {
4647 final String URI = BASE_URI + "foos/{id}" ;
4748
4849 final RestTemplate restTemplate = new RestTemplate ();
@@ -55,22 +56,35 @@ public void testGetFooAcceptXML() {
5556 final ResponseEntity <Foo > response = restTemplate .exchange (URI , HttpMethod .GET , entity , Foo .class , "1" );
5657 final Foo resource = response .getBody ();
5758
58- Assert .assertEquals (1l , resource .getId ());
59+ assertThat (resource , notNullValue ());
60+ }
61+
62+ @ Test
63+ public void givenConsumingJson_whenReadingTheFoo_thenCorrect () {
64+ final String URI = BASE_URI + "foos/{id}" ;
65+
66+ final RestTemplate restTemplate = new RestTemplate ();
67+ restTemplate .setMessageConverters (getMessageConverters ());
68+
69+ final HttpHeaders headers = new HttpHeaders ();
70+ headers .setAccept (Arrays .asList (MediaType .APPLICATION_JSON ));
71+ final HttpEntity <String > entity = new HttpEntity <String >(headers );
72+
73+ final ResponseEntity <Foo > response = restTemplate .exchange (URI , HttpMethod .GET , entity , Foo .class , "1" );
74+ final Foo resource = response .getBody ();
5975
76+ assertThat (resource , notNullValue ());
6077 }
6178
62- /**
63- * Specifying Accept Header with application/xml for getting the xml response from the server.
64- */
6579 @ Test
66- public void testPUTFooXML () {
80+ public void givenConsumingXml_whenWritingTheFoo_thenCorrect () {
6781 final String URI = BASE_URI + "foos/{id}" ;
6882 final RestTemplate restTemplate = new RestTemplate ();
6983 restTemplate .setMessageConverters (getMessageConverters ());
7084
71- final Foo resource = new Foo (4 , "andres " );
85+ final Foo resource = new Foo (4 , "jason " );
7286 final HttpHeaders headers = new HttpHeaders ();
73- headers .setAccept (Arrays .asList (MediaType .APPLICATION_XML ));
87+ headers .setAccept (Arrays .asList (MediaType .APPLICATION_JSON ));
7488 headers .setContentType ((MediaType .APPLICATION_XML ));
7589 final HttpEntity <Foo > entity = new HttpEntity <Foo >(resource , headers );
7690
@@ -82,17 +96,16 @@ public void testPUTFooXML() {
8296
8397 // UTIL
8498
85- /**
86- * Configures Message Converters.
87- */
8899 private List <HttpMessageConverter <?>> getMessageConverters () {
89100 final List <HttpMessageConverter <?>> converters = new ArrayList <HttpMessageConverter <?>>();
90- // adds XML converter using XStreamMarshaller
91- final XStreamMarshaller marshaller = new XStreamMarshaller ();
92- marshaller .setAnnotatedClasses (Foo .class );
93101
94- final MarshallingHttpMessageConverter marshallingConverter = new MarshallingHttpMessageConverter (marshaller );
95- converters .add (marshallingConverter );
102+ final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter ();
103+ final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller ();
104+ xmlConverter .setMarshaller (xstreamMarshaller );
105+ xmlConverter .setUnmarshaller (xstreamMarshaller );
106+
107+ converters .add (xmlConverter );
108+ converters .add (new MappingJackson2HttpMessageConverter ());
96109
97110 return converters ;
98111 }
0 commit comments