PowerMockito VerifyStatic在2.0.0-beta5中不起作用

时间:2018-10-23 15:01:45

标签: java junit mockito powermockito

我在使用powermockito(2.0.0-beta5)验证静态方法时遇到问题,当我调用其他方法(也是静态方法)时,该方法被调用了一定次数。这些类已准备好在我的测试文件顶部进行测试,相关代码段为:

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没有使用类参数。

1 个答案:

答案 0 :(得分:0)

我认为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类中的区别。也许您想展示它,以便我们所有人都可以看到您在其中进行的魔术。从示例代码中可以看到,我不得不作一些有根据的猜测,因为您只共享了一些测试代码片段,而不是您应该的MCVE。这样,我只能推测,实际上我不喜欢,因为这可能会浪费您和我自己的时间。