Java 使用 powermockito 验证静态 void 方法的调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22180773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:09:56  来源:igfitidea点击:

Verifying a call of a static void method with powermockito

javaunit-testingjunitmockitopowermock

提问by AdamSpurgin

I am trying to capture the 'logError' method in a static class (in the sense of every method/field is static), and verify it has been called some number of times by other methods in the same class.

我试图在静态类中捕获“logError”方法(每个方法/字段都是静态的),并验证它已被同一类中的其他方法调用了多次。

this method is:

这种方法是:

public static void logError(Object message){
    LOGGER.error(message); // static logger
}

my attempt to test it:

我试图测试它:

@Test
public void errLogTest() throws Exception{
    PowerMockito.mockStatic(X.class);
    PowerMockito.doNothing().when(X.class);
    X.logError(Mockito.anyString());
    X.open();
    X.open(); //should log error for opening twice
    PowerMockito.verifyStatic(Mockito.times(1));
}

My problem is, no matter how many times I specify, it passes. I removed mocking behavior and know for a fact the logger is called once, but I can have PowerMockito.verifyStatic(Mockito.times(9001));instead and it still passes. How do I test this?

我的问题是,无论我指定多少次,它都会通过。我删除了模拟行为,并且知道记录器被调用一次的事实,但我可以PowerMockito.verifyStatic(Mockito.times(9001));改为并且它仍然通过。我该如何测试?

回答by Matt Lachman

You're missing a line of code after verifyStatic. You're not telling PowerMock whatto verify. You're also mocking allstatic methods of the class instead of just the one you don't want called.

您在 之后缺少一行代码verifyStatic。您没有告诉 PowerMock要验证什么。您还模拟了该类的所有静态方法,而不仅仅是您不想调用的方法。

@Test
public void errLogTest() throws Exception{
    PowerMockito.spy(X.class); //Used to be: PowerMockito.mockStatic(X.class);
    PowerMockito.doNothing().when(X.class);
    X.logError(Mockito.anyString());
    X.open();
    X.open(); //should log error for opening twice
    PowerMockito.verifyStatic(Mockito.times(1));
    X.logError(Mockito.anyString()); //Now verifyStatic knows what to verify.
}

You may still need to do some debugging because, in my experience, setting up the expectations sometimes calls the underlying method anyway.

您可能仍然需要进行一些调试,因为根据我的经验,设置期望值有时会调用底层方法。

Here's the javadoc for spy: http://static.javadoc.io/org.powermock/powermock-api-mockito/1.5.4/org/powermock/api/mockito/PowerMockito.html#spy(java.lang.Class)

这是用于的 javadoc spyhttp: //static.javadoc.io/org.powermock/powermock-api-mockito/1.5.4/org/powermock/api/mockito/PowerMockito.html#spy(java.lang.Class )

Here's the javadoc for verifyStatic: http://static.javadoc.io/org.powermock/powermock-api-mockito/1.5.4/org/powermock/api/mockito/PowerMockito.html#verifyStatic(org.mockito.verification.VerificationMode)

这里是 javadoc verifyStatichttp: //static.javadoc.io/org.powermock/powermock-api-mockito/1.5.4/org/powermock/api/mockito/PowerMockito.html#verifyStatic(org.mockito.verification.VerificationMode )