如何在Mockito Framework中模拟返回任何扩展公共接口但不具体的类型的方法

时间:2016-09-21 07:51:59

标签: unit-testing mocking mockito stub

我想知道一个班级的方法。我想这个stubbed方法返回任何实现公共接口但不具体的接口类型。

例如:

public interface CommonType() {}
public class SubTypeOne implements CommonType {}
public class SubTypeTwo implements CommonType {}
public class SubTypeThree implements CommonType {}
public class SubTypeToNotReturn implements CommonType {}

when(object.methodToStub()).thenReturn( )

当我想要模拟返回任何一类时,我应该在thenReturn()括号中加入什么:

CommonTypeSubTypeOneSubTypeTwoSubTypeThree

但不是SubTypeToNotReturn

我已经尝试过了:

when(object.methodToStub()).thenReturn(not(new SubTypeToNotReturn()))

但它不起作用。我收到一个错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: No matchers found for Not(?).

0 个答案:

没有答案