Java Powermock 在非静态方法中验证私有静态方法调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16515809/
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-16 07:14:02  来源:igfitidea点击:

Powermock verify private static method call in non static method

javamockitopowermocknon-static

提问by Malvin

Dear stackoverflow comrades, I again have a problem in getting a specific PowerMock / Mockito case to work. The issue is, that I need to verify the call of a private static method, which is called from a public non-static method. A similar example I posted previously on How to suppress and verify private static method calls?

亲爱的 stackoverflow 同志们,我在让特定的 PowerMock / Mockito 案例工作时再次遇到问题。问题是,我需要验证私有静态方法的调用,该方法是从公共非静态方法调用的。我之前在如何抑制和验证私有静态方法调用上发布了一个类似的示例

This is my code:

这是我的代码:

class Factory {

        public String factorObject() throws Exception {
            String s = "Hello Mary Lou";
            checkString(s);
            return s;
        }

        private static void checkString(String s) throws Exception {
            throw new Exception();
        }
    }

And this is my testclass:

这是我的测试类:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Factory.class)
public class Tests extends TestCase {

    public void testFactory() throws Exception {

        Factory factory = mock(Factory.class);
        suppress(method(Factory.class, "checkString", String.class));
        String s = factory.factorObject();
        verifyPrivate(factory, times(8000)).invoke("checkString", anyString());
    }
}

The problem here is, that the Test is successful, but it shouldn't be. It shouldn't be because the private static method should be called exactly 1 times. But no matter what value I put in times(), it always verifies it as true... halp :(

这里的问题是,测试成功了,但它不应该成功。这不应该是因为私有静态方法应该被精确调用 1 次。但是无论我在 times() 中放入什么值,它总是会验证它为真... halp :(

采纳答案by Malvin

Ok, I think I found the answer, but it was a headache. Rudy gave me the final hint with using using a spy, but it was still not trivial (although the solution looks "baby-easy"). Here is the complete solution:

好的,我想我找到了答案,但是很头疼。Rudy 给了我使用间谍的最后提示,但这仍然不是微不足道的(尽管解决方案看起来“很简单”)。这是完整的解决方案:

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.spy;

@RunWith(PowerMockRunner.class)
@PrepareForTest(Factory.class)
public class Tests extends TestCase {

    public void testFactory() throws Exception {

        Factory factorySpy = spy(new Factory());
        String s = factorySpy.factorObject();
        doNothing().when(factorySpy, "checkString", anyString());
        verifyPrivate(factorySpy, times(1)).invoke("checkString", anyString()); 
    }
}