【问题标题】:PowerMockito VerifyStatic not working in 2.0.0-beta5PowerMockito VerifyStatic 在 2.0.0-beta5 中不起作用
【发布时间】:2019-03-27 21:05:40
【问题描述】:

当我调用不同的(也是静态的)方法时,我使用 powermockito (2.0.0-beta5) 验证静态方法被调用了一定次数时遇到问题。这些类在我的测试文件顶部准备测试相关代码sn -p是:

mockStatic(Tester.class);
when(Tester.staticMethod(anyString(), anyString())).thenAnswer(new FirstResponseWithText());
OtherClass.methodThatCallsTesterStaticMethod("", "", "", false, "");
verifyStatic(Tester.class, times(3));
Tester.sendFaqRequest(anyString(), anyString());

FirstResponseWithText 是一个扩展 Answer 的类,它控制响应的顺序。我在其他地方用过,效果很好。

我在verifyStatic 行收到以下错误:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type Class and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMethod();

将课程传递给verifyStatic 的正确方法是什么?我可以在网上找到的所有示例都是针对 2.x.x 之前的版本,其中 verifyStatic 没有采用类参数。

【问题讨论】:

    标签: java junit mockito powermockito


    【解决方案1】:

    我认为 PowerMockito 版本不是问题。我用版本测试了以下代码

    • 1.7.3,
    • 2.0.0-beta.5(您的版本),
    • 2.0.2.

    应用程序类:

    package de.scrum_master.stackoverflow.q52952222;
    
    public class Tester {
      public static String sendFaqRequest(String one, String two) {
        return "real response";
      }
    }
    
    package de.scrum_master.stackoverflow.q52952222;
    
    public class OtherClass {
      public static void methodThatCallsTesterStaticMethod(String one, String two, String three, boolean four, String five) {
        System.out.println(Tester.sendFaqRequest("A", "B"));
        System.out.println(Tester.sendFaqRequest("C", "D"));
        System.out.println(Tester.sendFaqRequest("E", "F"));
      }
    }
    

    测试类:

    package de.scrum_master.stackoverflow.q52952222;
    
    import org.mockito.invocation.InvocationOnMock;
    import org.mockito.stubbing.Answer;
    
    import java.util.Arrays;
    
    public class FirstResponseWithText implements Answer {
      @Override
      public Object answer(InvocationOnMock invocation) throws Throwable {
        Object[] args = invocation.getArguments();
        String methodName = invocation.getMethod().getName();
        return methodName + " called with arguments: " + Arrays.toString(args);
      }
    }
    
    package de.scrum_master.stackoverflow.q52952222;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    
    import static org.mockito.Mockito.anyString;
    import static org.mockito.Mockito.times;
    import static org.powermock.api.mockito.PowerMockito.*;
    
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ Tester.class })
    public class MyTest {
      @Test
      public void myTest() {
        // Tell PowerMockito that we want to mock static methods in this class
        mockStatic(Tester.class);
        // Stub return value for static method call
        when(Tester.sendFaqRequest(anyString(), anyString())).thenAnswer(new FirstResponseWithText());
        // Call method which calls our static method 3x
        OtherClass.methodThatCallsTesterStaticMethod("", "", "", false, "");
        // Verify that Tester.sendFaqRequest was called 3x
        verifyStatic(Tester.class, times(3));
        Tester.sendFaqRequest(anyString(), anyString());
      }
    }
    

    因此,如果这对您不起作用并且您的 Maven 或 Gradle 依赖项也可以,那么差异可能在于您的 FirstResponseWithText 类。也许你想展示它,这样我们都可以看到你在那里做了什么样的魔法。正如您从我的示例代码中看到的那样,我不得不做出几个有根据的猜测,因为您只分享了一点测试代码的 sn-p,而不是像您应该的那样 MCVE。这样我只能推测,实际上我不喜欢,因为这可能对你和我自己都是浪费时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2019-11-16
      相关资源
      最近更新 更多