mockito.veify() 给出了错误的调用计数
mockito.veify() is giving wrong invocation count
我正在尝试 mockito.verify() 功能,方法调用只有一次,但验证显示调用 3 次。单元测试只有一个测试,以确保此测试不会受到其他方式的影响。
Mockito.verify(utilClass, Mockito.times(1)).hello(); /// 但是调用了3次,是error
我试过使用日志,它显示调用是一次。多次添加故意调用方法 utilClass.hello(),然后在调用一次的给定代码中显示 invocations.but 的正确编号,显示为调用 3 次。
Class待测:
@Component
public class UtilClass {
private static final Logger logger = LoggerFactory.getLogger(UtilClass.class);
public String hello() {
logger.info("hello method invoked");
return "Hello World,"+ hi();
}
private String hi() {
logger.info("hi method invoked");
return "Hi All";
}
}
@Component
public class ClassWithDependency {
@Autowired
private UtilClass utilClass;
public String welcome() {
String msg = utilClass.hello();
return msg;
}
}
测试用例
@InjectMocks
private ClassWithDependency classWithDependency;
@Mock
private UtilClass utilClass;
@Before
public void init() throws Exception {
PowerMockito.when(utilClass,"hi").thenReturn("Hi Java");
PowerMockito.when(utilClass.hello()).thenCallRealMethod();
}
@Test
public void testMock() throws Exception {
final String msg = classWithDependency.welcome();
final String msgExpected = "Hello World,Hi Java";
logger.info("welcome method called:{}",msg);
PowerMockito.verifyPrivate(utilClass, Mockito.times(1)).invoke("hi");
Mockito.verify(utilClass, Mockito.times(1)).hello();
Assert.assertTrue(msg.equals(msgExpected));
}
我期待 mockito.verify 成功,但出现错误
org.mockito.exceptions.verification.TooManyActualInvocations:
utilClass.hello();
Wanted 1 time:
-> at com.sd.demo.powermock.test.injectmock.InjectMockTest.testMock(InjectMockTest.java:54)
But was 3 times:
-> at com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13)
-> at com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13)
-> at com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13)
at com.sd.demo.powermock.test.injectmock.InjectMockTest.testMock(InjectMockTest.java:54)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:89)
.............
最终使测试工作,解决方案是 'SPY'.. 监视 util class,这样就不知道真正调用哪个方法了。我不确定如何注入间谍,所以使用了 setter..
@InjectMocks
私人 ClassWithDependency classWithDependency;
private UtilClass mockUtil;
@Before
public void init() throws Exception {
UtilClass utilClass = new UtilClass();
mockUtil = PowerMockito.spy(utilClass);
classWithDependency.setUtilClass(mockUtil);
}
@Test
public void testMock() throws Exception {
PowerMockito.when(mockUtil,"hi").thenReturn("Hi Java");
final String msg = classWithDependency.welcome();
final String msgExpected = "Hello World,Hi Java";
logger.info("welcome method called:{}",msg);
PowerMockito.verifyPrivate(mockUtil, Mockito.times(1)).invoke("hi");
Mockito.verify(mockUtil, Mockito.times(1)).hello();
Assert.assertTrue(msg.equals(msgExpected));
}
我正在尝试 mockito.verify() 功能,方法调用只有一次,但验证显示调用 3 次。单元测试只有一个测试,以确保此测试不会受到其他方式的影响。 Mockito.verify(utilClass, Mockito.times(1)).hello(); /// 但是调用了3次,是error
我试过使用日志,它显示调用是一次。多次添加故意调用方法 utilClass.hello(),然后在调用一次的给定代码中显示 invocations.but 的正确编号,显示为调用 3 次。
Class待测:
@Component
public class UtilClass {
private static final Logger logger = LoggerFactory.getLogger(UtilClass.class);
public String hello() {
logger.info("hello method invoked");
return "Hello World,"+ hi();
}
private String hi() {
logger.info("hi method invoked");
return "Hi All";
}
}
@Component
public class ClassWithDependency {
@Autowired
private UtilClass utilClass;
public String welcome() {
String msg = utilClass.hello();
return msg;
}
}
测试用例
@InjectMocks
private ClassWithDependency classWithDependency;
@Mock
private UtilClass utilClass;
@Before
public void init() throws Exception {
PowerMockito.when(utilClass,"hi").thenReturn("Hi Java");
PowerMockito.when(utilClass.hello()).thenCallRealMethod();
}
@Test
public void testMock() throws Exception {
final String msg = classWithDependency.welcome();
final String msgExpected = "Hello World,Hi Java";
logger.info("welcome method called:{}",msg);
PowerMockito.verifyPrivate(utilClass, Mockito.times(1)).invoke("hi");
Mockito.verify(utilClass, Mockito.times(1)).hello();
Assert.assertTrue(msg.equals(msgExpected));
}
我期待 mockito.verify 成功,但出现错误
org.mockito.exceptions.verification.TooManyActualInvocations: utilClass.hello(); Wanted 1 time: -> at com.sd.demo.powermock.test.injectmock.InjectMockTest.testMock(InjectMockTest.java:54) But was 3 times: -> at com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13) -> at com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13) -> at com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13) at com.sd.demo.powermock.test.injectmock.InjectMockTest.testMock(InjectMockTest.java:54) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:89) .............
最终使测试工作,解决方案是 'SPY'.. 监视 util class,这样就不知道真正调用哪个方法了。我不确定如何注入间谍,所以使用了 setter..
@InjectMocks 私人 ClassWithDependency classWithDependency;
private UtilClass mockUtil;
@Before
public void init() throws Exception {
UtilClass utilClass = new UtilClass();
mockUtil = PowerMockito.spy(utilClass);
classWithDependency.setUtilClass(mockUtil);
}
@Test
public void testMock() throws Exception {
PowerMockito.when(mockUtil,"hi").thenReturn("Hi Java");
final String msg = classWithDependency.welcome();
final String msgExpected = "Hello World,Hi Java";
logger.info("welcome method called:{}",msg);
PowerMockito.verifyPrivate(mockUtil, Mockito.times(1)).invoke("hi");
Mockito.verify(mockUtil, Mockito.times(1)).hello();
Assert.assertTrue(msg.equals(msgExpected));
}