2

我是这样绑的:

byte[] mockByteArray = PowerMock.createMockAndExpectNew(byte[].class, 10);

但是我遇到了运行时异常:找不到对象方法!如何解决?

[编辑] 我想模拟一个RandomAccessFile.read(byte[] buffer)

byte[] fileCutter(RandomAccessFile randomAccessFile, long position, int filePartSize) throws IOException{ 
     byte[] buffer = new byte[filePartSize];
     randomAccessFile.seek(position); 
     randomAccessFile.read(buffer);
     return buffer;
}
4

1 回答 1

3

如果你想测试fileCutter方法,你不需要模拟一个byte数组。你必须嘲笑RandomAccessFile. 例如,像这样(对不起,小的语法错误,我现在无法检查):

RandomAccessFile raf = EasyMock.createMock(RandomAccessFile.class);
// replace the byte array by what you expect
byte[] expectedRead = new byte[] { (byte) 129, (byte) 130, (byte) 131};
EasyMock.expect(raf.seek(EasyMock.anyInt()).once();
EasyMock.expect(raf.read(expectedRead)).once();

// If you don't care about the content of the byte array, you can do:
// EasyMock.expect(raf.read((byte[]) EasyMock.anyObject())).once();

myObjToTest.fileCutter(raf, ..., ...);
于 2013-09-03T06:45:46.277 回答