Java Mockito UnfinishedStubbingException with when().thenReturn()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24390146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:55:11  来源:igfitidea点击:

Mockito UnfinishedStubbingException with when().thenReturn()

javajdbcprepared-statementmockito

提问by DanGordon

I am getting the error message Unfinished Stubbing detected here, when running the following code:

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

The last line throws the error. It doesn't really make sense, as I have done the same code before. Why am I getting this error?

最后一行抛出错误。这真的没有意义,因为我以前做过相同的代码。为什么我收到这个错误?

EDIT: I am using powerMockito in order to use the Whitebox.invokeMethod()method, but I also want to use regular Mockito in the rest of the program. Could that be a problem?

编辑:我正在使用 powerMockito 来使用该Whitebox.invokeMethod()方法,但我也想在程序的其余部分使用常规 Mockito。这可能是个问题吗?

Stack Trace:

堆栈跟踪:

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

采纳答案by Jeff Bowman

Seems like the error is pretty clear.

似乎错误很明显。

PowerMockito.when(task, "saveDoPatchFile");

...is missing a thenReturn, right?

...缺少一个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();

So why is the exception down in your test method? Neither PowerMock nor Mockito can flag a failure to call thenReturnuntil the next time you interact with (Power)Mockito or a mock. After all, Mockito and PowerMockito aren't notified that your @Beforemethod ends, and have to accept the "waiting for thenReturn" state. (You're allowed to call mockbefore thenReturnto allow thenReturn(mock(Foo.class)).)

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

回答by Schmick

I wanted to add a note that I got this exact same exception, even though my code was written properly. It turned out to be entirely a runtime problem.

我想添加一个注释,说明我遇到了完全相同的异常,即使我的代码编写正确。结果证明这完全是一个运行时问题。

My code:

我的代码:

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

The exception:

例外:

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!

It turned out that the following call was throwing an exception, which then triggered the Mockito exception about unfinished stubbing.

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

championList.getChampion(1)