### Question:
GlusterFileChannel extends FileChannel { @Override public long size() throws IOException { stat stat = new stat(); int retval = GLFS.glfs_fstat(fileptr, stat); if (0 != retval) { throw new IOException("fstat failed"); } return stat.st_size; }  @Override int read(ByteBuffer byteBuffer); @Override long read(ByteBuffer[] byteBuffers, int offset, int length); @Override int write(ByteBuffer byteBuffer); @Override long write(ByteBuffer[] byteBuffers, int offset, int length); @Override long position(); @Override FileChannel position(long offset); @Override long size(); @Override FileChannel truncate(long l); @Override void force(boolean b); @Override long transferTo(long l, long l2, WritableByteChannel writableByteChannel); @Override long transferFrom(ReadableByteChannel readableByteChannel, long l, long l2); @Override int read(ByteBuffer byteBuffer, long position); @Override int write(ByteBuffer byteBuffer, long position); @Override MappedByteBuffer map(MapMode mapMode, long l, long l2); @Override FileLock lock(long l, long l2, boolean b); @Override FileLock tryLock(long l, long l2, boolean b); static final Map<StandardOpenOption, Integer> optionMap; static final Map<PosixFilePermission, Integer> perms; }### Answer:
@Test public void testSize() throws Exception { long fileptr = 1234l; channel.setFileptr(fileptr); long actualSize = 321l; stat stat = new stat(); stat.st_size = actualSize; PowerMockito.whenNew(stat.class).withNoArguments().thenReturn(stat); mockStatic(GLFS.class); when(GLFS.glfs_fstat(fileptr, stat)).thenReturn(0); long size = channel.size(); assertEquals(actualSize, size); verifyStatic(); GLFS.glfs_fstat(fileptr, stat); }
@Test(expected = IOException.class) public void testSize_whenFailing() throws Exception { long fileptr = 1234l; channel.setFileptr(fileptr); long actualSize = 321l; stat stat = new stat(); stat.st_size = actualSize; PowerMockito.whenNew(stat.class).withNoArguments().thenReturn(stat); mockStatic(GLFS.class); when(GLFS.glfs_fstat(fileptr, stat)).thenReturn(-1); long size = channel.size(); }