【问题标题】:Power mockito verify static calls real methodPower mockito 验证静态调用真实方法
【发布时间】:2019-11-07 15:34:58
【问题描述】:

我正在尝试验证在使用 powerMockito 1.6.4 测试服务方法时从未调用过静态方法

我跟着This answer 做了同样的事情。

以下是我的代码。

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

我现在面临的问题是,MyClass.myMethod(Mockito.any()); 调用了真正的myMethod 并给出了一个 nullPointerException。

我的假设是MyClass.myMethod(Mockito.any());PowerMockito.verifyStatic(Mockito.never()); 一起使用以指定要验证的静态方法。

我错过了什么吗?

【问题讨论】:

  • 你必须模拟 MyClass.myMethod 方法的行为

标签: java junit powermockito


【解决方案1】:

你还必须模拟静态方法的行为

即像这样的

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

参考Mock method with parameters

【讨论】:

    猜你喜欢
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多