### Question:
B2InputStreamWithByteProgressListener extends InputStream { @Override public int read() throws IOException { try { int value = inputStream.read(); if (value == EOF) { notifyListenerOfRead(EOF); } else { notifyListenerOfRead(1); } return value; } catch (IOException | RuntimeException e) { listener.hitException(e, nBytesSoFar); throw e; } } B2InputStreamWithByteProgressListener(InputStream inputStream,
                                                 B2ByteProgressListener listener); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b,
                    int off,
                    int len); @Override long skip(long n); @Override int available(); @Override void close();  }### Answer:
@Test public void readByte_getByte() throws Exception { when(wrappedStream.read()).thenReturn((int) 'a'); assertEquals('a', stream.read()); verify(wrappedStream, times(1)).read(); verify(listener, times(1)).progress(1); }
@Test public void readByte_getEof() throws Exception { when(wrappedStream.read()).thenReturn(EOF); assertEquals(EOF, stream.read()); verify(wrappedStream, times(1)).read(); verify(listener, times(1)).reachedEof(0); }
@Test public void readByte_throwsIOException() throws Exception { final IOException exception = new IOException("test"); when(wrappedStream.read()).thenThrow(exception); try { stream.read(); fail("should've thrown!"); } catch (IOException e) { assertEquals("test", e.getMessage()); } verify(wrappedStream, times(1)).read(); verify(listener, times(1)).hitException(eq(exception), eq(0L)); }
@Test public void readByte_throwsRuntimeException() throws Exception { final RuntimeException exception = new RuntimeException("test"); when(wrappedStream.read()).thenThrow(exception); try { stream.read(); fail("should've thrown!"); } catch (RuntimeException e) { assertEquals("test", e.getMessage()); } verify(wrappedStream, times(1)).read(); verify(listener, times(1)).hitException(eq(exception), eq(0L)); }
@Test public void testReadArray() throws Exception { when(wrappedStream.read(V, 0, V.length)).thenReturn(6); assertEquals(6, stream.read(V)); verify(wrappedStream, times(1)).read(V, 0, V.length); verify(listener, times(1)).progress(6); }
@Test public void testReadArray_getEof() throws Exception { when(wrappedStream.read(V, 0, V.length)).thenReturn(EOF); assertEquals(EOF, stream.read(V)); verify(wrappedStream, times(1)).read(V, 0, V.length); verify(listener, times(1)).reachedEof(0); }
@Test public void testReadArrayOffsetAndLength_throwsIOException() throws Exception { final IOException exception = new IOException("test"); when(wrappedStream.read(V, 1, V.length-10)).thenThrow(exception); try { stream.read(V, 1, V.length-10); fail("should've thrown!"); } catch (IOException e) { assertEquals("test", e.getMessage()); } verify(wrappedStream, times(1)).read(V, 1, V.length-10); verify(listener, times(1)).hitException(exception, 0); }
@Test public void testReadArrayOffsetAndLength_throwsRuntimeException() throws Exception { final RuntimeException exception = new RuntimeException("test"); when(wrappedStream.read(V, 1, V.length-10)).thenThrow(exception); try { stream.read(V, 1, V.length-10); fail("should've thrown!"); } catch (RuntimeException e) { assertEquals("test", e.getMessage()); } verify(wrappedStream, times(1)).read(V, 1, V.length-10); verify(listener, times(1)).hitException(exception, 0); }