【问题标题】:mockito UnfinishedStubbingException missing thenReturn or you are trying to stub a final methodmockito UnfinishedStubbingException 缺少 thenReturn 或者您正在尝试存根最终方法
【发布时间】:2018-03-08 14:30:14
【问题描述】:

我正在尝试在 Java7 上使用 TestNGMockito 运行以下单元测试 - TestDummy

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import junit.framework.Assert;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.annotations.Test;

@PrepareForTest({TestA.class, TestB.class, Result.class, C.class})
public class TestDummy {

    @Test
    public void testIt() throws Exception {
        mockStatic(TestB.class);
        Result r = mock(Result.class);
        r.res = 2;
        TestB tB = mock(TestB.class);
        doReturn(tB).when(TestB.class, "get");
        when(tB.doSome(any(C.class))).thenReturn(r);

        Result rA = TestA.run();

        Assert.assertEquals(2, rA.res);
    }
}

以下是我尝试运行上述单元测试的源代码 -

class TestA {

    public static Result run() {
        TestB tB=TestB.get();
        return tB.doSome(new C());
    }
}

class Result {
    int res;
}

class TestB {

    static final TestB INS = new TestB();
    public static TestB get() {
        return INS;
    }

    public Result doSome(C c) {

        Result r = new Result();
        r.res=1;
        return r;
    }
}

class C {

}

但因以下错误而失败 -

Running TestDummy
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.268 sec <<< FAILURE! - in TestDummy
testIt(TestDummy)  Time elapsed: 0.527 sec  <<< FAILURE!
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

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 TestDummy.testIt(TestDummy.java:25)

看起来是一个微不足道的问题,但在这里停留了一段时间。希望在不修改源代码的情况下解决此问题的任何输入(修改单元测试 - TestDummy 应该没问题)。我看到很多关于类似/相同问题的帖子,但是这些建议似乎在这里不起作用。

【问题讨论】:

    标签: java unit-testing mockito testng


    【解决方案1】:

    对于 Junit,您使用 @RunWith,但对于 TestNG,您需要在测试用例中指定:

    @ObjectFactory public IObjectFactory getObjectFactory() {
         return new org.powermock.modules.testng.PowerMockObjectFactory(); 
    } 
    

    记得在你的 pom 中添加这个依赖:

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-testng</artifactId>
        <version>1.7.3</version>
        <scope>test</scope>
    </dependency>
    

    【讨论】:

    • 非常感谢,让我开心:)
    【解决方案2】:

    这个问题被标记为junit4,但提供的代码(这是一个非常有用的 MCVE,谢谢 :) 表明您正在使用 testng,TestDummy 包含此导入

    org.testng.annotations.Test
    

    所以,我不确定您使用的是哪个。

    不管怎样,你的代码看起来很可靠,我做了一个简单的改变就成功地运行了它;将@RunWith(PowerMockRunner.class) 添加到TestDummy

    这已使用以下方法进行了验证:

    • JUnit 4.12
    • Powermock 1.7
    • Mockito 2.7.19。

    【讨论】:

    • 错误地标记为junit4而不是testng,我的错!
    • 没问题,我刚刚更新了交换标签的问题,我看到您在另一个答案中已经有了解决方案:)
    猜你喜欢
    • 2014-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多