wanderingstu
wanderingstu

Reputation: 325

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

I am writing a unit test for a class but I am getting an error

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

even though I am calling when() using a mock object. Here is the code.

package assign.resources;
import static org.junit.Assert.*;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import javax.ws.rs.core.StreamingOutput;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

import assign.domain.*;
import assign.services.*;

public class TestTrafficCameraReportsResource {
    @Mock
    TrafficCameraReportsService mockService = null;

    TrafficCameraReportsResource reportsResource = new TrafficCameraReportsResource();


    @Before
    public void setUp() {
        mockService = mock(TrafficCameraReportsService.class);
        reportsResource.setReportService(mockService);
    }

    @Test
    public void unitTest1() throws Exception {

        when(mockService.getReportsList()).thenReturn(null); //this is the line causing the error

        StreamingOutput allReports = reportsResource.getTrafficCameraReports();
        assertEquals(null, allReports);

    }

When I looked this error up online, people were getting it because they weren't using a mock object, but I am using a mock object, mockService. Any ideas as to why this is happening?

Error:

TestTrafficCameraReportsResource
assign.resources.TestTrafficCameraReportsResource
unitTest1(assign.resources.TestTrafficCameraReportsResource)
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.
3. the parent of the mocked class is not public.
   It is a limitation of the mock engine.


    at assign.resources.TestTrafficCameraReportsResource.unitTest1(TestTrafficCameraReportsResource.java:29)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.base/java.lang.reflect.Method.invoke(Method.java:567)

    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)

    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)

    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)

    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

Upvotes: 3

Views: 5064

Answers (2)

wanderingstu
wanderingstu

Reputation: 325

Figured it out, getReportsList was static so it didn't recognize that it was being called from mockService. Removing static worked.

Upvotes: 1

Nir Levy
Nir Levy

Reputation: 12953

You need to annotate your class with @RunWith(MockitoJUnitRunner.class) (or any other runner if you're using spring or some other framework).

Also, you have both a @Mock annotation on your field, and assigning it with mock() method. no need for both, you can leave only one (and if it's the annotation, remove the = null;)

Upvotes: 0

Related Questions