I'm trying to stub a method using Mockito 1.8.5, but doing so calls the real method implementation (with "" as parm values) which throws an exception.
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);
}
}
The method I'm trying to mock looks like this. There are no final method or classes.
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) {
...
}
}
And the class I'm passing the mock looks like this:
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;
}
}
What is causing the real implementation to be invoked and how can I prevent it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…