### Question:
HttpClient { public String post(Map<String, String> parameters) { OutputStreamWriter wr = null; BufferedReader rd = null; String response = ""; StringBuilder data = new StringBuilder(); try { for (Map.Entry<String, String> entry : parameters.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { continue; } data.append("&"); data.append(URLEncoder.encode(entry.getKey(), "UTF-8")); data.append("="); data.append(URLEncoder.encode(entry.getValue(), "UTF-8")); } HttpURLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Length", String.valueOf(data.length())); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); wr = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); wr.write(data.toString()); wr.flush(); wr.close(); rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); String line; while ((line = rd.readLine()) != null) { response = String.format("%s%s%n", response, line); } } catch (Exception e) { log.warn("Exception while posting to : " + this.url, e); log.warn("Reponse from server was: " + response); } finally { if (wr != null) { try { wr.close(); } catch (Exception e) { } } if (rd != null) { try { rd.close(); } catch (Exception e) { } } } return response; } HttpClient(String url);  HttpClient(HttpUrl url); String post(Map<String, String> parameters);  }### Answer:
@Test public void post_shouldPostUrlParametersAndGetResponse() throws IOException { Map<String, String> parameters = new TreeMap<>(); parameters.put("one", "one"); parameters.put("two", "two"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); when(connection.getOutputStream()).thenReturn(stream); when(connection.getInputStream()).thenReturn(new ByteArrayInputStream("response".getBytes())); String response = client.post(parameters); verify(connection).setDoOutput(true); verify(connection).setDoInput(true); verify(connection).setRequestMethod("POST"); verify(connection).setRequestProperty("Content-Length", String.valueOf(16)); verify(connection).setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); assertThat(stream.toString(), is("&one=one&two=two")); assertThat(response, CoreMatchers.containsString("response")); }