[英]Mockito test case failing
我使用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"
。
但是相反,我得到了这样的错误
com.fittingroom.newtimezone.tools.cameraTools.ImageSaver.getImageFilePath(ImageSaver.java:37)处的java.lang.NullPointerException com.fittingroom.newtimezone.model.ModelAcceptNotAccept.getImageFilePath(ModelAcceptNotAccept.java:14)处的java.lang.NullPointerException
在处理model.getImageFilePath();
出现错误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)
编辑2
编辑3
我的测试
@Test public void setOriginPreviewImage() throws Exception {
presenter.setOriginPreviewImage();
doReturn("path").when(model).getImageFilePath();
String path = model.getImageFilePath();
verify(contractAcceptNotAccept).setPreviewImage(path);
}
错误
对于您的最新更新,请记住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)添加@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");
}
}
您可以在此处查看有关如何使用Mockito的示例测试。
使用Mockito.doReturn(...).when(...);
因为这不会调用模拟方法并立即返回模拟值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.