Mockito 1.8.5を使用してメソッドをスタブしようとしていますが、これを行うと例外をスローする実際のメソッド実装。パッケージの保護されたメソッドをスタブしようとすると実際のメソッド実装を呼び出すMockitoモック
package background.internal; //located in trunk/tests/java/background/internal
public class MoveStepTest {
@Test
public void testMoveUpdate() {
final String returnValue = "value";
final FileAttachmentContainer file = mock(FileAttachmentContainer.class);
doReturn(returnValue).when(file).moveAttachment(anyString(), anyString(), anyString());
//this also fails
//when(file.moveAttachment(anyString(), anyString(), anyString())).thenReturn(returnValue);
final AttachmentMoveStep move = new AttachmentMoveStep(file);
final Action moveResult = move.advance(1, mock(Context.class));
assertEquals(Action.done, moveResult);
}
}
私が模倣しようとしている方法は、このように見えます。最終的なメソッドやクラスはありません。
package background.internal; //located in trunk/src/background/internal
public class FileAttachmentContainer {
String moveAttachment(final String arg1, final String arg2, final String arg3)
throws CustomException {
...
}
String getPersistedValue(final Context context) {
...
}
}
とクラス私はこのようなモックルックスを渡している:
package background.internal; //located in trunk/src/background/internal
public class AttachmentMoveStep {
private final FileAttachmentContainer file;
public AttachmentMoveStep(final FileAttachmentContainer file) {
this.file = file;
}
public Action advance(final double acceleration, final Context context) {
try {
final String attachmentValue = this.file.getPersistedValue(context);
final String entryId = this.file.moveAttachment(attachmentValue, "attachment", context.getUserName());
//do some other stuff with entryId
} catch (CustomException e) {
e.log(context);
}
return Action.done;
}
}
実際の実装が呼び出されると、どのように私はそれを防ぐことができます原因は何?
実際にテストを実行しているコードを追加できますか? – jhericks
'FileAttachmentContainer'または' moveAttachment() 'メソッドは最終的ですか? Mockitoは最終的な方法をモックできません。 –
実際にmoveAttachmentはanyString()の値で呼び出されますか? – markus