### Question:
SoapRequestClient { public Element makeSoapRequest(Element requestElement, URI uri) throws SOAPRequestError { return makePost(uri, requestElement).getBody(); } @Inject  SoapRequestClient(SoapMessageManager soapMessageManager, @Named("SoapClient") Client client); Element makeSoapRequest(Element requestElement, URI uri);  }### Answer:
@Test public void makePost_shouldEnsureResponseInputStreamIsClosedWhenResponseCodeIsNot200() throws URISyntaxException { when(response.getStatus()).thenReturn(502); when(webResourceBuilder.post(any(Entity.class))).thenReturn(response); URI matchingServiceUri = new URI("http: try { soapRequestClient.makeSoapRequest(null, matchingServiceUri); fail("Exception should have been thrown"); } catch (SOAPRequestError e) { verify(response).close(); } }
@Test public void makePost_checkUniformInterfaceExceptionIsThrownOnNon200StatusCode() throws IOException, SAXException, ParserConfigurationException, URISyntaxException { when(response.getStatus()).thenReturn(303); when(webResourceBuilder.post(any(Entity.class))).thenReturn(response); Element matchingServiceRequest = XmlUtils.convertToElement("<someElement/>"); URI matchingServiceUri = new URI("http: try { soapRequestClient.makeSoapRequest(matchingServiceRequest, matchingServiceUri); fail("Exception should have been thrown"); } catch (SOAPRequestError ignored) { } }
@Test public void makePost_checkProcessingExceptionIsThrown() throws IOException, SAXException, ParserConfigurationException, URISyntaxException, SOAPRequestError { ProcessingException exception = mock(ProcessingException.class); when(webResourceBuilder.post(any(Entity.class))).thenThrow(exception); Element matchingServiceRequest = XmlUtils.convertToElement("<someElement/>"); URI matchingServiceUri = new URI("http: try { soapRequestClient.makeSoapRequest(matchingServiceRequest, matchingServiceUri); fail("Exception should have been thrown"); } catch (ProcessingException e) { assertThat(e).isEqualTo(exception); } }
@Test public void makePost_checkSOAPRequestErrorIsThrownWhenNotValidXML() throws Exception { when(webResourceBuilder.post(any(Entity.class))).thenReturn(response); when(response.readEntity(Document.class)).thenThrow(new BadRequestException()); when(response.getStatus()).thenReturn(200); Element matchingServiceRequest = XmlUtils.convertToElement("<someElement/>"); URI matchingServiceUri = new URI("http: try { soapRequestClient.makeSoapRequest(matchingServiceRequest, matchingServiceUri); fail("Exception should have been thrown"); } catch (SOAPRequestError ignored) { } finally { verify(response).readEntity(Document.class); } }
@Test public void makePost_aSuccessfulRequest() throws IOException, SAXException, ParserConfigurationException, URISyntaxException, SOAPRequestError { when(response.getStatus()).thenReturn(200); when(webResourceBuilder.post(any(Entity.class))).thenReturn(response); Element matchingServiceRequest = XmlUtils.convertToElement("<someElement/>"); URI matchingServiceUri = new URI("http: final Element element = soapRequestClient.makeSoapRequest(matchingServiceRequest, matchingServiceUri); assertThat(element).isEqualTo(soapElement); }