Mockito Spy doReturn调用原始方法

时间:2019-01-29 15:30:22

标签: java junit mockito junit4

我是Mockito的新手。我知道间谍对象永远不会为doReturn调用原始方法,但是在我的代码中,这没有发生

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.spy;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

class MyClass {
    public int method1(){
        int x =10;
        x = method2(2);
        return x;
    }

    public int method2(int y){
        method3();
        return 20;
    }

    public int method3(){
       return 30;
    }
}

@RunWith(MockitoJUnitRunner.class)
public class Method1Test {
    private MyClass myClass = new MyClass();

    @Before
    public void setup(){}

    @Test
    public void test01(){
        MyClass spyMyClass = spy(myClass);
        doReturn(28).when(spyMyClass).method2(any());
        int a = spyMyClass.method1();
        assertTrue("We did it!!!",a==20);
    }
}

以下是错误堆栈跟踪:

  

java.lang.NullPointerException         在com.Method1Test.test01(Method1Test.java:45)         在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处         在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)         在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)         在java.lang.reflect.Method.invoke(未知来源)         在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)         在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)         在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)         在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)         在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)         在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)         在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)         在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)         在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)         在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)         在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)         在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)         在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)         在org.junit.runners.ParentRunner.run(ParentRunner.java:363)         在org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)         在org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)         在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)         在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)         在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here:

-> at com.Method1Test.test01(Method1Test.java:45)

请有人帮我解决上面的问题,并为我提供上述问题的解决方法。

1 个答案:

答案 0 :(得分:1)

此问题是由传递给method2的参数类型不匹配引起的。那就是异常消息的这一部分告诉你的:

  

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:   在此处检测到错误的参数匹配器:

以下测试通过:

@Test
public void test01(){
    MyClass spyMyClass = spy(myClass);
    doReturn(28).when(spyMyClass).method2(anyInt());
    int a = spyMyClass.method1();
    assertTrue("We did it!!!",a==28);
}

这里有两个变化:

  1. 使用anyInt()代替any()
  2. 声明a==28是因为您告诉存根返回28