【发布时间】:2018-05-15 09:23:12
【问题描述】:
我正在使用 JUnit 4.12 和 Mockito 1.10.19,但遇到了一个无法解释的 Mockito 问题。
我正在创建一个抽象类(就像我过去所做的那样),它在 BaseFunction 类型的对象上设置了一个 spy(我的项目中有一个抽象类)
public abstract class AbstractFunctionTestCase<F extends BaseFunction> {
@Rule
public JUnitSoftAssertions softly = new JUnitSoftAssertions();
protected F function;
/**
* Return the real function here
* @return
*/
protected abstract F functionInstance();
@Before
public void setUp() {
function = Mockito.spy(functionInstance());
Mockito.doReturn("Bla").when(function).getDescription();
}
}
然后我将抽象类扩展如下
@RunWith(MockitoJUnitRunner.class)
public class MyFunctionTest extends AbstractFunctionTestCase<MyFunction> {
@Override
protected MyFunction functionInstance() {
return new MyFunction();
}
@Test
public void myEmptyTest() {
// Nothing here, for real!
}
}
当我在 IntelliJ 中运行方法 myEmptyTest 时,我得到了
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.company.functions.AbstractFunctionTestCase.setUp(AbstractFunctionTestCase.java:31)
对我来说很奇怪,我通过调用
检查了setUp() 方法中是否有间谍
new MockUtil().isSpy(function)
有什么想法吗?
【问题讨论】: