当使用内部 void 方法调用的方法调用时,Mockito doNothing 不起作用
Mockito doNothing doesn't work when a Method called with void method call inside
主要Class
public class BootSample {
public int call(int m) {
System.out.println("Entering into Call Method");
int n = m*10;
TestUtil testUtil = new TestUtil();
testUtil.add(m, n);
System.out.println("End of Call Method Value n : " + n);
return n;
}
}
利用Class
public class TestUtil {
public void add(int a, int b) {
System.out.println(" Entering into TestUtil Method ");
int c = a +b;
System.out.println(" End of TestUtil Method Value : " + c);
}
}
测试Class
@RunWith(MockitoJUnitRunner.class)
public class BootSampleTest {
@Mock
TestUtil testUtil;
@Before
public void setup() {
}
@Test
public void utilSuccess() throws Exception {
BootSample bootSample = new BootSample();
doNothing().when(testUtil).add(any(Integer.class),any(Integer.class));
int result = bootSample.call(10);
assertEquals(result,100);
}
}
输出:
Entering into Call Method
Entering into TestUtil Method
End of TestUtil Method Value : 110
End of Call Method Value n : 100
我正在尝试使用 doNothing 模拟 util void 方法调用,但是 work.Can 没有人帮我解决吗?我在我们的应用程序中遇到了类似的功能。
问题是您的 call
方法负责创建一个 TestUtil
对象,而该对象不能被模拟。尝试将 TestUtil 添加为构造函数参数,如下所示:
public class BootSample {
private TestUtil testUtil;
public BootSample(TestUtil testUtil) {
this.testUtil = testUtil;
}
public int call(int m) {
System.out.println("Entering into Call Method");
int n = m*10;
testUtil.add(m, n);
System.out.println("End of Call Method Value n : " + n);
return n;
}
}
然后您需要模拟 TestUtil
class 并将模拟传递给 BootSample
class:
BootSample bootSample = new BootSample(testUtil);
如果您从 TestUtil class 中看到 System.out.printlns,那么它没有被模拟。看起来您缺少 BootSample 上的 @InjectMocks 来告诉 Mockito 将模拟的 TestUtil 注入其中。
在此处查看文档中的示例:http://static.javadoc.io/org.mockito/mockito-core/2.13.0/org/mockito/InjectMocks.html
您可以使用 Mockito.anyInt()
而不是 Integer.class
,
代码示例:
Mockito.doNothing().when(testUtil).add(Mockito.anyInt(),Mockito.anyInt());
主要Class
public class BootSample {
public int call(int m) {
System.out.println("Entering into Call Method");
int n = m*10;
TestUtil testUtil = new TestUtil();
testUtil.add(m, n);
System.out.println("End of Call Method Value n : " + n);
return n;
}
}
利用Class
public class TestUtil {
public void add(int a, int b) {
System.out.println(" Entering into TestUtil Method ");
int c = a +b;
System.out.println(" End of TestUtil Method Value : " + c);
}
}
测试Class
@RunWith(MockitoJUnitRunner.class)
public class BootSampleTest {
@Mock
TestUtil testUtil;
@Before
public void setup() {
}
@Test
public void utilSuccess() throws Exception {
BootSample bootSample = new BootSample();
doNothing().when(testUtil).add(any(Integer.class),any(Integer.class));
int result = bootSample.call(10);
assertEquals(result,100);
}
}
输出:
Entering into Call Method
Entering into TestUtil Method
End of TestUtil Method Value : 110
End of Call Method Value n : 100
我正在尝试使用 doNothing 模拟 util void 方法调用,但是 work.Can 没有人帮我解决吗?我在我们的应用程序中遇到了类似的功能。
问题是您的 call
方法负责创建一个 TestUtil
对象,而该对象不能被模拟。尝试将 TestUtil 添加为构造函数参数,如下所示:
public class BootSample {
private TestUtil testUtil;
public BootSample(TestUtil testUtil) {
this.testUtil = testUtil;
}
public int call(int m) {
System.out.println("Entering into Call Method");
int n = m*10;
testUtil.add(m, n);
System.out.println("End of Call Method Value n : " + n);
return n;
}
}
然后您需要模拟 TestUtil
class 并将模拟传递给 BootSample
class:
BootSample bootSample = new BootSample(testUtil);
如果您从 TestUtil class 中看到 System.out.printlns,那么它没有被模拟。看起来您缺少 BootSample 上的 @InjectMocks 来告诉 Mockito 将模拟的 TestUtil 注入其中。
在此处查看文档中的示例:http://static.javadoc.io/org.mockito/mockito-core/2.13.0/org/mockito/InjectMocks.html
您可以使用 Mockito.anyInt()
而不是 Integer.class
,
代码示例:
Mockito.doNothing().when(testUtil).add(Mockito.anyInt(),Mockito.anyInt());