我正在编写一个类的单元测试,但我得到了一个错误
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)
弄明白了,getReportsList是静态的,所以它无法识别它是从MockService调用的。移除静态工作。
不幸的是: TweetServiceTest.java Hibernateutil.java
当我尝试运行代码时,它会导致我遇到一个异常。 LoginController.java sampleClass.java MissingMethodInvocationException:when()需要一个必须是“mock上的方法调用”的参数。例如:when(mock.getarticles()).ThenReturn(articles); 此外,出现此错误的原因可能是:1。您使用final/p
我没有使用任何框架 编辑测试代码 saveServlet.java
问题内容: 我有一个使用Mockito和Spring Test框架的非常简单的测试用例。当我做 我得到这个例外。 我尝试使用不同的方法,但继续收到此错误消息。我在Mockito中使用Spring 3.1.0.RELEASE。请分享并指导我正确的方向。 问题答案: 您需要先创建pcUserService的MOCK,然后使用该模拟。
编辑测试代码 保存服务器.java patient.java
在我的网站上,我允许人们上传图片画廊。当他们点击图像时,底部有一个下一个和上一个按钮,这样他们就可以很容易地在图像中来回滚动。 我在位于/opt/cpanel/ea-php72/root/usr/var/log/php-fpm的日志中得到以下错误/ 这是关于我代码中的以下行: 下面是该行附带的其他代码: 基本上,这段代码使用get_字段(“gallery”)获取gallery中的照片总数,并将该数