gpt4 book ai didi

java - 从 method::org.mockito.exceptions.misusing.MissingMethodInitationException 调用另一个方法

转载 作者:行者123 更新时间:2023-11-30 05:36:05 24 4
gpt4 key购买 nike

我想使用 Mockito 来模拟我想要测试的方法内的方法调用。这样我就可以返回我想要的值以用于方法的进一步处理。

代码:

DAOFactory dao = org.mockito.Mockito.mock(DAOFactory.class);
Mockito.when(dao.getDAOFactory().getCountryDAO()).thenReturn((CountryDAO)iMockCountryDAO);

但是我在控制台中收到以下错误:

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.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

at aero.sita.pts.timatic.datamanager.app.junit.ParagraphRuleCheckerTest.testReworkCO069Scenario1(ParagraphRuleCheckerTest.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

您可以实现您想要实现的目标,但需要分两步完成:

DAOFactory dao = org.mockito.Mockito.mock(DAOFactory.class);
SomeClass factoryMock = mock(SomeClass.class);
when(dao.getDAOFactory()).thenReturn(factoryMock);
when(factoryMock.getCountryDAO()).thenReturn((CountryDAO)iMockCountryDAO);

如果您想使用模拟对象链,则需要单独创建每个模拟对象并将其链接在一起。

关于java - 从 method::org.mockito.exceptions.misusing.MissingMethodInitationException 调用另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56593053/

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