gpt4 book ai didi

java - Mockito Spy doReturn 调用原始方法

转载 作者:行者123 更新时间:2023-11-30 05:48:23 28 4
gpt4 key购买 nike

我是 Mockito 新手。我知道 spy 对象永远不会调用 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 at com.Method1Test.test01(Method1Test.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at 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)

请有人帮助我解决上述问题,并为我解决上述问题。

最佳答案

该问题是由传递给 method2 的参数类型不匹配引起的。这就是异常消息的这一部分要告诉您的内容:

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

以下测试通过:

@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 因为您告诉 stub 返回 28

关于java - Mockito Spy doReturn 调用原始方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54424421/

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