ElmTransport { public String readData() { log.debug("waiting for response"); StringBuilder res = new StringBuilder(); char c; while ((c = readChar()) != ELM_PROMPT) { log.debug("read char '{}'", c); res.append(c); } log.debug("reading end"); String data = res.toString().trim(); log.debug("Read raw data '{}'", data); return data; } ElmTransport(InputStream inputStream, OutputStream outputStream); String sendDataAndReadResponse(String data); void sendData(String data); String readData();  }
@Test public void readDataShouldReadInputStreamToTheElmPrompt() throws Exception { String expectedData = getRandomStreamData(); String streamData = " " + expectedData + ">"; when(isMock.read()).thenAnswer(new InputStreamAnswer(streamData)); String readData = sut.readData(); assertThat(readData).isEqualTo(expectedData); }
@Test @SuppressWarnings("unchecked") public void readDataShouldWrapExceptionsWithRuntimeException() throws Exception { when(isMock.read()).thenThrow(IOException.class); catchException(sut).readData(); Exception exception = caughtException(); assertThat(exception).isExactlyInstanceOf(ObdCommunicationException.class); }