PowerMockito验证从不调用静态方法 [英] PowerMockito verify that a static method is never called

查看:708
本文介绍了PowerMockito验证从不调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个JUnit测试,以验证是否在方法流中从未调用静态方法(MyClass.myMethod()).我试图做这样的事情:

I am writing a JUnit test to verify that a static method (MyClass.myMethod()) is never invoked in the method flow. I tried doing something like this:

  PowerMockito.verifyStatic(Mockito.never());
  MyClass.myMethod(Mockito.any());

这样做会收到UnfinisedVerificationException.如何测试MyClass.class在方法执行中没有任何交互?

In doing so I receive an UnfinisedVerificationException. How do I test that MyClass.class has no interactions whatsoever in the method execution?

推荐答案

如果尚未模拟该类,但您正在尝试验证其静态方法的调用,则会发生UnfinishedVerificationException.

UnfinishedVerificationException will occur if the Class is not mocked yet but you are trying to verify the invocation of its static method.

PowerMockito.mockStatic(MyClass.class);
underTest.testMethod();
PowerMockito.verifyStatic(Mockito.never());
MyClass.myMethod(Mockito.any());
.
.
.

如果流程从未遇到对MyClass.myMethod()

这篇关于PowerMockito验证从不调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆