简体   繁体   English

电源mockito验证static调用实方法

[英]Power mockito verify static calls real method

I am trying to verify a static method was never called while testing a service method with powerMockito 1.6.4我正在尝试验证在使用 powerMockito 1.6.4 测试服务方法时从未调用过 static 方法

I followed This answer to do the same.我按照这个答案做同样的事情。

following is my code.以下是我的代码。

@RunWith ( PowerMockRunner.class)
@PrepareForTest ( MyClass.class)
@PowerMockIgnore ( "javax.net.ssl.*")
public class SomeTests
{
 @Test
    public void testMyMethodIsNotCalled() throws Exception
    {
        PowerMockito.mockStatic(MyClass.class);
        underTest.testMethod();
        PowerMockito.verifyStatic(Mockito.never());
        MyClass.myMethod(Mockito.any());
    }
}

The problem I am facing now is that, MyClass.myMethod(Mockito.any());我现在面临的问题是MyClass.myMethod(Mockito.any()); calls the real myMethod and gives a nullPointerException.调用真正的myMethod并给出 nullPointerException。

My assumption is that MyClass.myMethod(Mockito.any());我的假设是MyClass.myMethod(Mockito.any()); works with PowerMockito.verifyStatic(Mockito.never());适用于PowerMockito.verifyStatic(Mockito.never()); in order to specify the static method to be verified.为了指定要验证的static方法。

Am I missing something?我错过了什么吗?

you have to mock the static method behaviour also您还必须模拟 static 方法行为

ie something like this即这样的东西

PowerMockito.mockStatic(NameOfClass.class);
expect( NameOfClass.nameOfMethod((URL)Mockito.any(),Mockito.anyString())).andReturn(actualOutput);

refer Mock method with parameters参考带参数的 Mock 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM