[英]How to separate function mocks of two classes that implement the same interface?
My problem is defined in header, actually. 我的问题实际上是在标题中定义的。 I am writing unit tests with Junit, PowerMock&Mockito.
我正在与Junit,PowerMock和Mockito一起编写单元测试。 I am mocking a class like that,
我在嘲笑这样一个班级,
class User{
public final synchronized String enter(AbstractClass ac){
//.....
}
}
// In test function
User mockUser = PowerMockito.mock(User.class);
PowerMockito.when(mockUser.enter( Mockito.any(Class1ImplementsSameAbstract.class)))
.thenReturn("Some Str 1");
PowerMockito.when(mockUser.enter( Mockito.any(Class2ImplementsSameAbstract.class)))
.thenReturn("Some Str 2");
System.out.println(mockUser.enter(new Class1ImplementsSameAbstract()));
System.out.println(mockUser.enter(new Class2ImplementsSameAbstract()));
How can I separate these two "when" conditions? 我怎样才能将这两个“何时”的条件分开? When I run this test, both of the "System.out.." lines print "Some Str 2".
当我运行此测试时,两个“System.out ..”行都打印“Some Str 2”。
The any*
family of matchers do not do type checking, you should use isA(Class<T>)
instead: any*
系列的匹配器不进行类型检查,你应该使用isA(Class<T>)
代替:
PowerMockito.when(mockUser.enter(Mockito.isA(Class1ImplementsSameAbstract.class)))
.thenReturn("Some Str 1");
Or alternatively, use the actual arguments, eg: 或者,使用实际参数,例如:
final Class1ImplementsSameAbstract klass1 = new Class1ImplementsSameAbstract();
PowerMockito.when(mockUser.enter(klass1)).thenReturn("Some Str 1");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.