Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito gives UnfinishedVerificationException when it seems OK

Mockito appears to be throwing an UnfinishedVerificationException when I think I've done everything correctly. Here's my partial test case:

HttpServletRequest req = mock(HttpServletRequest.class); when(req.getHeader("Authorization")).thenReturn("foo");  HttpServletResponse res = mock(HttpServletResponse.class);  classUnderTest.doMethod(req, res); // Use the mock  verify(res, never()); verify(req).setAttribute(anyString(), anyObject()); 

And here's the partial class and method:

class ClassUnderTest extends AnotherClass {     @Override     public String doMethod(ServletRequest req, ServletRequest res) {         // etc.         return "someString";     } } 

Ignoring the fact that you should never mock interfaces you don't own, why is Mockito giving me the following message?

org.mockito.exceptions.misusing.UnfinishedVerificationException:  Missing method call for verify(mock) here: -> at (redacted)  Example of correct verification:     verify(mock).doSomething()  Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods. Those methods *cannot* be stubbed/verified.  at [test method name and class redacted] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) ... etc 
like image 976
Jonathan Avatar asked Apr 09 '13 14:04

Jonathan


1 Answers

This might also be caused if you try to verify a method which expects primitive arguments with any():

For example, if our method has this signature:

method(long l, String s); 

And you try to verify it like this, it will fail with aforementioned message:

verify(service).method(any(), anyString()); 

Change it to anyLong() and it will work:

verify(service).method(anyLong(), anyString()); 
like image 129
Babken Vardanyan Avatar answered Sep 17 '22 17:09

Babken Vardanyan