@Test public void shouldBeAbleToWriteToThenReadFromChannel() throws Throwable { // Given RecordingByteChannel channel = new RecordingByteChannel(); // When byte[] data = new byte[]{1, 2, 3, 4, 5}; channel.write( ByteBuffer.wrap( data ) ); ByteBuffer buffer = ByteBuffer.allocate( 10 ); int bytesRead = channel.read( buffer ); // Then assertThat( bytesRead, equalTo( 5 ) ); assertThat( buffer.array(), equalTo( new byte[]{1, 2, 3, 4, 5, 0, 0, 0, 0, 0} ) ); }