@Test public void call_server() throws Exception { String expectedSentence = "Hello Filibuster!"; Client httpClient = mock(Client.class); WebTarget webTarget = mock(WebTarget.class); Invocation.Builder builder = mock(Invocation.Builder.class); String json = "{\"sound\":\"SGVsbG8gRmlsaWJ1c3RlciE=\"}"; when(httpClient.target(anyString())).thenReturn(webTarget); when(webTarget.path(anyString())).thenReturn(webTarget); when(webTarget.queryParam(anyString(), anyString())).thenReturn(webTarget); when(webTarget.request(any(MediaType.class))).thenReturn(builder); when(builder.get(String.class)).thenReturn(json); String host = "localhost"; int port = 80; SpeechClient client = new SpeechClient(httpClient, new ObjectMapper(), host, port); SynthesizedSound actual = client.synthesise(expectedSentence); assertThat(actual.getSound(), is(expectedSentence.getBytes())); verify(httpClient).target("http://localhost:80"); verify(webTarget).path("synthesize"); String encoded = Base64.getEncoder().encodeToString(expectedSentence.getBytes("UTF-8")); verify(webTarget).queryParam("sentence", encoded); verify(webTarget).request(MediaType.APPLICATION_JSON_TYPE); verify(builder).get(String.class); }
@Test public void write_error_to_system_err() throws IOException { Client httpClient = getMockedClient(); ObjectMapper mapper = getMockedMapper(); String host = "not important"; int notImportant = -1; SpeechClient client = new SpeechClient(httpClient, mapper, host, notImportant); PrintStream stdErrMock = mock(PrintStream.class); System.setErr(stdErrMock); try { client.synthesise("anything"); } catch (Exception e) { verify(stdErrMock).println(anyString()); } //noinspection PointlessBooleanExpression assertThat(true, is(!false)); // pmd requires at least one assert... }