Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito Exception - when() requires an argument which has to be a method call on a mock

I have a very simple test case that is using Mockito and Spring Test framework. When I do

when(pcUserService.read("1")).thenReturn(pcUser); 

I get this exception.

org.mockito.exceptions.misusing.MissingMethodInvocationException:  when() requires an argument which has to be 'a method call on a mock'. For example:     when(mock.getArticles()).thenReturn(articles);  Also, this error might show up because: 1. you stub either of: final/private/equals()/hashCode() methods.    Those methods *cannot* be stubbed/verified. 2. inside when() you don't call method on mock but on some other object.      at com.project.cleaner.controller.test.PcUserControllerTest.shouldGetPcUser(PcUserControllerTest.java:93)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 

I have tried with different methods but keep on getting this error message. I am using Spring 3.1.0.RELEASE with Mockito. Please share and guide me in the right direction.

like image 298
jsf Avatar asked Feb 08 '12 01:02

jsf


People also ask

When () requires an argument which has?

when() requires an argument which has to be 'a method call on a mock'. For example: when(mock. getArticles()). thenReturn(articles); Also, this error might show up because: 1.

Which Mockito method is used to handle exception in case method?

Mockito provides the capability to a mock to throw exceptions, so exception handling can be tested. Take a look at the following code snippet. //add the behavior to throw exception doThrow(new Runtime Exception("divide operation not implemented")) .

Does Mockito verify call the method?

Mockito Verify methods are used to check that certain behavior happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called.

Can we mock final methods using Mockito?

Configure Mockito for Final Methods and ClassesBefore we can use Mockito for mocking final classes and methods, we have to configure it. Mockito checks the extensions directory for configuration files when it is loaded. This file enables the mocking of final methods and classes.


2 Answers

You need to create a MOCK of pcUserService first, and then use that mock.

PcUserService mock = org.mockito.Mockito.mock(PcUserService.class); when(mock.read("1")).thenReturn(pcUser); 
like image 54
Ralph Avatar answered Sep 16 '22 23:09

Ralph


In case others hit this issue....

It could also be the case that the method you are trying to mock out,pcUserService.read, is declared as a final method. From what I've noticed this appears to cause issues with Mockito.

like image 22
djkelly99 Avatar answered Sep 17 '22 23:09

djkelly99