我这样就被打扰了:
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;
}
答案 0 :(得分:2)
如果要测试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, ..., ...);