@Test public void read() throws Exception { String body = "<root>Hello World</root>"; MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8")); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willReturn(body); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); String result = (String) converter.read(Object.class, inputMessage); assertEquals("Invalid result", body, result); }
@Test public void canRead() { Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.supports(Integer.class)).willReturn(false); given(unmarshaller.supports(String.class)).willReturn(true); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); assertFalse(converter.canRead(Boolean.class, MediaType.TEXT_PLAIN)); assertFalse(converter.canRead(Integer.class, MediaType.TEXT_XML)); assertTrue(converter.canRead(String.class, MediaType.TEXT_XML)); }
@Test public void readWithMarshallingFailureException() throws Exception { MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]); UnmarshallingFailureException ex = new UnmarshallingFailureException("forced"); Unmarshaller unmarshaller = mock(Unmarshaller.class); given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex); MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); converter.setUnmarshaller(unmarshaller); try { converter.read(Object.class, inputMessage); fail("HttpMessageNotReadableException should be thrown"); } catch (HttpMessageNotReadableException e) { assertTrue("Invalid exception hierarchy", e.getCause() == ex); } }