gpt4 book ai didi

java - Mockito when() 需要一个必须是 'method call on a mock' 错误的参数

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:43:28 60 4
gpt4 key购买 nike

我正在为一个类编写单元测试,但出现错误

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

即使我使用模拟对象调用 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);

}

当我在网上查看这个错误时,人们得到它是因为他们没有使用模拟对象,但我使用的是模拟对象 mockService。关于为什么会发生这种情况的任何想法?

错误:

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)

最佳答案

弄明白了,getReportsList 是静态的,因此它无法识别它是从 mockService 调用的。去除静电有效。

关于java - Mockito when() 需要一个必须是 'method call on a mock' 错误的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356853/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com