【问题标题】:Mockito UnfinishedStubbingException with when().thenReturn()使用 when().thenReturn() 模拟 UnfinishedStubbingException
【发布时间】:2014-06-24 15:05:45
【问题描述】:

运行以下代码时,我收到错误消息Unfinished Stubbing detected here

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

@RunWith(PowerMockRunner.class)
public class PatchWriterTaskTest {
@Before
public void before() throws Exception {
    filePath = getFilePath();
    task = PowerMockito.spy(new PatchWriterTask());
    patchFilesName = new HashMap<Integer, String>();

    patchFilesName.put(1, "TCE_RDF_COVERED_DATA_REGION.sql");

    scriptPath = new File(filePath + "do_patch_rdf.sql");

    PowerMockito.when(task, "getLogger").thenReturn(logger);
    PowerMockito.when(task, "getPatchFilesName").thenReturn(patchFilesName);
    PowerMockito.when(task, "getDirectoryPath").thenReturn(filePath);
    PowerMockito.when(task, "saveDoPatchFile");
}
@Test
public void testUpdateIssuesTable() throws Exception {
        PatchWriterTask task = new PatchWriterTask();
        Connection conn = mock(Connection.class);
        PreparedStatement updateStatement = mock(PreparedStatement.class);
        String sql = "update val_issues set PATCH_CREATION_INFO = ? where VAL_ISSUE_ID = ?";
        when(conn.prepareStatement(sql)).thenReturn(updateStatement);

最后一行抛出错误。这真的没有意义,因为我之前做过相同的代码。为什么会出现此错误?

编辑:我正在使用 powerMockito 以使用 Whitebox.invokeMethod() 方法,但我也想在程序的其余部分使用常规 Mockito。会不会有问题?

堆栈跟踪:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:426)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

    at com.navteq.rdf.base.task.PatchWriterTaskTest.testUpdateIssuesTable(PatchWriterTaskTest.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

【问题讨论】:

  • 发布完整的堆栈跟踪。
  • 您是否从不同的类中静态导入whenthenReturn?例如。一个来自 PowerMock,一个来自 Mockito?
  • 检查我的导入语句,编辑我的帖子。
  • 看起来您可能会遇到这个问题:Mockito UnfinishedStubbingException,因为您还从 thenReturn 调用中返回了一个模拟对象。您使用的是哪个版本的 Mockito?
  • 不确定,我该如何找到?

标签: java jdbc prepared-statement mockito


【解决方案1】:

我想补充一点,即使我的代码编写正确,我也遇到了同样的异常。结果证明这完全是一个运行时问题。

我的代码:

when(service.getChampionById(1, region, null, null)).thenReturn(championList.getChampion(1));

例外:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at ResourceV1bTest.setUpResources(ResourceV1bTest.java:2168)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, you naughty developer!

原来下面的调用抛出了一个异常,然后触发了关于未完成存根的 Mockito 异常。

championList.getChampion(1)

【讨论】:

  • 也刚好碰到这个。值得一提的是,您 championList 必须在前面用 Mockito.spy() 包裹才能发生此错误。使用非包装对象时,不会发生错误。此外,您可以将getChampion(1) 的结果存储在变量中以规避触发的错误。
  • 无论如何我相信这是一个未记录的错误。刚刚尝试使用 Mockito 4.3.1 并且问题仍然存在。或者任何人都可以说这是否是实际记录的行为或存在问题条目?
  • 实际上有一个excellent writeup on this behavior,但它没有提到这是否真的是 Mockito 创作者的想法。即使这是预期的行为,在有关此特殊情况的错误消息中添加提示也可能非常有帮助。
【解决方案2】:

似乎错误很明显。

PowerMockito.when(task, "saveDoPatchFile");

...缺少thenReturn,对吗?

E.g. thenReturn() may be missing. Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();

那么为什么您的测试方法中出现异常? PowerMock 和 Mockito 都不能标记调用 thenReturn 的失败,直到下次您与 (Power)Mockito 或模拟交互时。毕竟,Mockito 和 PowerMockito 不会收到您的 @Before 方法结束的通知,并且必须接受“等待 thenReturn”状态。 (您可以在thenReturn 之前调用mock 以允许thenReturn(mock(Foo.class))。)

【讨论】:

  • 哇是的,这正是问题所在。不敢相信我错过了。不知道是谁写的代码。奇怪的是后来出现了异常,但是当我注释掉PowerMockito.when(task, "saveDoPatchFile");时,就没有错误了
猜你喜欢
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
相关资源
最近更新 更多