Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMockito verify that a static method is never called

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

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

like image 571
Nivedita Velagaleti Avatar asked Sep 30 '25 09:09

Nivedita Velagaleti


1 Answers

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());
.
.
.

This should succeed if the flow never encounters a call to MyClass.myMethod()

like image 113
Nivedita Velagaleti Avatar answered Oct 02 '25 04:10

Nivedita Velagaleti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!