ElmTransport { public void sendData(String data) { log.debug("Sending bytes '{}'", data); writeBytes(data); } ElmTransport(InputStream inputStream, OutputStream outputStream); String sendDataAndReadResponse(String data); void sendData(String data); String readData();  }
@Test public void sendDataShouldWriteDataToTheOutputStream() throws Exception { String expectedData = getRandomStreamData(); sut.sendData(expectedData); InOrder inOrder = inOrder(osMock); inOrder.verify(osMock).write(expectedData.getBytes()); inOrder.verify(osMock).flush(); }
@Test @SuppressWarnings("unchecked") public void sendDataShouldWrapExceptionsDuringWriteWithRuntimeException() throws Exception { String expectedData = getRandomStreamData(); doThrow(IOException.class).when(osMock).write(expectedData.getBytes()); catchException(sut).sendData(expectedData); Exception exception = caughtException(); assertThat(exception).isExactlyInstanceOf(ObdCommunicationException.class); }
@Test @SuppressWarnings("unchecked") public void sendDataShouldWrapExceptionsDuringFlushWithRuntimeException() throws Exception { String expectedData = getRandomStreamData(); doThrow(IOException.class).when(osMock).flush(); catchException(sut).sendData(expectedData); Exception exception = caughtException(); assertThat(exception).isExactlyInstanceOf(ObdCommunicationException.class); }