Aleksey Timoshchenko Aleksey Timoshchenko - 11 days ago 5
Android Question

Mockito test case failing

I work with mvp pattern and treid to test short presenter method

This is

final public void setOriginPreviewImage() {
final String path = model.getImageFilePath();
iActivityAcceptNotAccept.setPreviewImage(path);
}


This method retrieves the path and pass it via interface to view

This is test

public class PresenterActivityAcceptNotAcceptTest {

private PresenterActivityAcceptNotAccept presenter;

@Mock ModelAcceptNotAccept model;
@Mock IActivityAcceptNotAccept iActivityAcceptNotAccept;

@Before public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
presenter = new PresenterActivityAcceptNotAccept(iActivityAcceptNotAccept);
}

@Test public void setOriginPreviewImage() throws Exception {
presenter.setOriginPreviewImage();
when(model.getImageFilePath()).thenReturn("path");
String path = model.getImageFilePath();
verify(iActivityAcceptNotAccept).setPreviewImage(path);
}


here in the first line I invoke the method, then do
when()
to point that if
getImageFilePath()
will been invoke then return
"path"
as a value.

In the third line I invoke
getImageFilePath()
I expect to get valeu that I had set up prior to this line -
"path"
.

But instead of this I get such error


java.lang.NullPointerException
at com.fittingroom.newtimezone.tools.cameraTools.ImageSaver.getImageFilePath(ImageSaver.java:37)
at com.fittingroom.newtimezone.model.ModelAcceptNotAccept.getImageFilePath(ModelAcceptNotAccept.java:14)


I get the error during processing
model.getImageFilePath();


according to the log file test don't return back the value that I had set up hire

when(model.getImageFilePath()).thenReturn("path");


instead of this it is trying to get value from the method, of course there is no value because it is test...

How model assosiated with presenter

public final class PresenterActivityAcceptNotAccept implements IPresenterAcceptNotAccept {
private IActivityAcceptNotAccept iActivityAcceptNotAccept;
private ModelAcceptNotAccept model;

public PresenterActivityAcceptNotAccept(IActivityAcceptNotAccept iActivityAcceptNotAccept) {
this.iActivityAcceptNotAccept = iActivityAcceptNotAccept;
this.model = new ModelAcceptNotAccept(this);
}


Model code

public class ModelAcceptNotAccept {
private IPresenterAcceptNotAccept iPresenterAcceptNotAccept;

public ModelAcceptNotAccept(IPresenterAcceptNotAccept iPresenterAcceptNotAccept) {
this.iPresenterAcceptNotAccept = iPresenterAcceptNotAccept;
}

public String getImageFilePath(){
return ImageSaver.getImageFilePath();
}

}


What am I doing wrong?

EDIT

@Test public void setOriginPreviewImage() throws Exception {
//when(model.getImageFilePath()).thenReturn("path");
doReturn("path").when(model.getImageFilePath());
presenter.setOriginPreviewImage();
String path = model.getImageFilePath();
verify(iActivityAcceptNotAccept).setPreviewImage(path);
}


give me such errors

org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.fittingroom.newtimezone.presenters.PresenterActivityAcceptNotAcceptTes t.setOriginPreviewImage(PresenterActivityAcceptNotAcceptTest.java:30)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed


at com.fittingroom.newtimezone.presenters.PresenterActivityAcceptNotAcceptTest.setOriginPreviewImage(PresenterActivityAcceptNotAcceptTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


EDIT2

enter image description here

EDIT3

My test now

@Test public void setOriginPreviewImage() throws Exception {
presenter.setOriginPreviewImage();
doReturn("path").when(model).getImageFilePath();
String path = model.getImageFilePath();
verify(contractAcceptNotAccept).setPreviewImage(path);
}


Error

enter image description here

Answer

For your most recent update, bear in mind that doVerb().when() syntax is different: Unlike when().thenVerb() syntax, you don't put the whole method call inside the when argument, just the mock in question.

/*  BAD */ doReturn("path").when(model.getImageFilePath());
//                                    v- see the parens -^
/* GOOD */ doReturn("path").when(model).getImageFilePath();

You're getting that message because the doReturn syntax starts to prepare a custom object for you--one that has all of its stubbing disabled--and rather than calling when and then calling a method on the proxy object returned by when(model), you call a method on the original mock model. The stubbing action isn't finished the way Mockito would like it to be, so Mockito throws an UnfinishedStubbingException.