【发布时间】:2018-01-10 22:24:02
【问题描述】:
主类
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;
}
}
实用类
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);
}
}
测试类
@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 方法调用,但不起作用。有人可以帮我解决问题吗?我在我们的应用程序中遇到了类似的功能。
【问题讨论】:
-
1.此测试将始终失败。 2. 你需要模拟你的班级
标签: java junit mockito powermock