java - Mockito 的 spy 功能失败

标签 java junit mockito

我有一个 public class MClass,它包含一个 public static 方法 getSysTime()。在我的测试中,我试图模拟这个类的一个特定方法。我基本上需要在测试中每当调用此类的这个方法时,它都会返回一个字符串“FakeTimestamp”。 MClass 类没有任何父类。以下是我的代码:

MClass m = Mockito.spy(new MClass());
Mockito.when(m.getSysTime()).thenReturn("FakeTimestamp");

但是,我在测试时遇到以下错误:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.


    at com.scb.edmhdpif.filevalidator.CDCRowCountValidatorTest.CDCRowCountFileDriverTest(CDCRowCountValidatorTest.java:77)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


Process finished with exit code -1

我正在使用 IntelliJ Idea 15.0.2。 有什么办法可以解决这个错误,或者是否有另一个模拟引擎可以帮助实现我的需要?

最好的问候

最佳答案

我认为有问题的部分是您试图监视 static 方法。 IE。它适用于非静态实例:

public class MClass {

    public String getSysTime() { // this is the tricky one
        return "salala";
    }
}

public class MTest {

    @Test
    public void testSpying() {
        MClass m = Mockito.spy(new MClass());
        Mockito.when(m.getSysTime()).thenReturn("FakeTimestamp");
    }
}

this Q&A他们解释了一些处理此类问题的技巧。

关于java - Mockito 的 spy 功能失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37584518/

相关文章:

Java - 当没有用户输入时防止出现错误消息 - 作业

java - boolean 方法的 Junit 测试

android - 无法使用 Robotium 单击全屏

java - 如何对使用 SimpleJdbcCall 的类进行单元测试

testing - Spring jmsTemplate 发送单元测试不起作用

java - Java 中的包装类之间可以进行类型转换吗?

java - 扫描器无法从字符串中读取最后一个 int

java - 当我使用 doReturn(..).when(....) 时,PowerMockito 正在调用该方法

java - 消除数组中的重复项

java - JUnit测试用@SpringApplicationConfiguration自动启动Job