gpt4 book ai didi

java - Mockito 测试用例失败

转载 作者:行者123 更新时间:2023-12-01 09:13:15 25 4
gpt4 key购买 nike

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

这是

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

此方法检索路径并将其通过接口(interface)传递给 View

这是测试

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() 我希望获得我在此行之前设置的值 - "path"

但我没有得到这样的错误

java.lang.NullPointerException at 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)

编辑2

enter image description here

编辑3

我现在的测试

@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

最佳答案

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

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

您收到该消息是因为 doReturn 语法开始为您准备一个自定义对象(该对象已禁用所有 stub ),而不是调用 when 然后调用 when(model) 返回的代理对象上的方法,您调用原始模拟 model 上的方法。 stub 操作没有按照 Mockito 希望的方式完成,因此 Mockito 会抛出 UnfinishedStubbingException。

关于java - Mockito 测试用例失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40784821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com