当前位置: 首页 > 知识库问答 >
问题:

Mockito when()需要一个参数,该参数必须是“mock上的method call”错误

慎志国
2023-03-14

我正在编写一个类的单元测试,但我得到了一个错误

when()需要一个参数,该参数必须是模拟上的方法调用

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

    }

错误:

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)

共有1个答案

宰父疏珂
2023-03-14

弄明白了,getReportsList是静态的,所以它无法识别它是从MockService调用的。移除静态工作。

 类似资料: