mockito.veify()给出错误的调用计数

时间:2019-09-03 15:52:36

标签: java junit mockito powermockito

我正在尝试mockito.verify()功能,方法调用仅一次,但是验证显示调用3次。单元测试只有一个测试,以确保该测试不会受到其他影响。 Mockito.verify(utilClass,Mockito.times(1))。hello(); ///但调用是3次,是错误

我尝试使用日志,该日志显示调用一次。多次添加了故意调用方法utilClass.hello(),然后显示正确的调用次数。但是在给定的代码中,一次调用被显示为被调用3次。

要测试的类:

@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();   想要1次:   -> com.sd.demo.powermock.test.injectmock.InjectMockTest.testMock(InjectMockTest.java:54)   但是是3次:   -> com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13)   -> com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13)   -> com.sd.demo.powermock.injectmock.ClassWithDependency.welcome(ClassWithDependency.java:13)       在com.sd.demo.powermock.test.injectmock.InjectMockTest.testMock(InjectMockTest.java:54)       在org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)       在org.junit.internal.runners.MethodRoadie $ 2.run(MethodRoadie.java:89)   .............

1 个答案:

答案 0 :(得分:1)

最后使测试正常工作,解决方案是'SPY'..间谍util类,以便不告诉哪个方法进行真正的调用。我不确定如何注入间谍,所以使用了二传手。

@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));
}