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 に答える