Mockito测试用例失败

时间:2016-11-24 11:07:02

标签: java android junit mockito

我使用mvp模式和treid来测试简短的演示者方法

这是

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

此方法检索路径并通过接口将其传递给视图

这是测试

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);
}

在第一行中我调用该方法,然后执行when()指出如果getImageFilePath()将被调用,则返回"path"作为值。

在第三行中,我调用getImageFilePath()我希望得到我在此行之前设置的valeu - "path"

但不是这样我得到了这样的错误

  

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

我在处理model.getImageFilePath();

期间收到错误消息

根据日志文件测试,不要返回我设置的值

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

而不是这个,它试图从方法中获取价值,当然没有价值,因为它是测试......

模型如何与演示者联系

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

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

型号代码

public class ModelAcceptNotAccept {
private IPresenterAcceptNotAccept iPresenterAcceptNotAccept;

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

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

}

我做错了什么?

修改

@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);
}

给我这样的错误

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

我现在的考试

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

错误

enter image description here

3 个答案:

答案 0 :(得分:1)

对于您最近的更新,请记住doVerb().when()语法不同:与when().thenVerb()语法不同,您不要将整个方法调用放在when参数中,只是有问题的模拟。

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

您收到该消息是因为doReturn语法开始为您准备一个自定义对象 - 一个禁用所有存根的对象 - 而不是调用when然后在when(model)返回的代理对象上调用方法时,可以在原始模拟model上调用方法。存根动作并没有像Mockito那样完成,所以Mockito会抛出一个UnfinishedStubbingException。

答案 1 :(得分:0)

您的代码存在一些问题:

(1)添加@InjectMocks以将模拟对象注入PresenterActivityAcceptNotAccept

(2)在调用实际方法之前先调用方法调用(@Mock个对象)。

(3)使用doNothing()模拟void返回方法调用

public class PresenterActivityAcceptNotAcceptTest {

   @InjectMocks
   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 {
        //mock the method call
        when(model.getImageFilePath()).thenReturn("path");

        //doNothing for void call
        doNothing().when(iActivityAcceptNotAccept).setPreviewImage("path");


        //verify the method call
        verify(iActivityAcceptNotAccept).setPreviewImage("path");
    }

}

您可以查看here有关如何使用Mockito的示例测试。

答案 2 :(得分:0)

使用Mockito.doReturn(...).when(...);,因为这不会调用模拟方法并立即返回模拟值。