实际上,我的问题是在标题中定义的。我正在与Junit,PowerMock&Mockito。我在嘲笑这样一个班,
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()));
如何区分这两个"何时"条件?当我运行这个测试时,两行"System.out.."都会打印"Some Str 2"。
any*
系列匹配器不进行类型检查,您应该使用isA(Class<T>)
:
PowerMockito.when(mockUser.enter(Mockito.isA(Class1ImplementsSameAbstract.class)))
.thenReturn("Some Str 1");
或者,使用实际参数,例如:
final Class1ImplementsSameAbstract klass1 = new Class1ImplementsSameAbstract();
PowerMockito.when(mockUser.enter(klass1)).thenReturn("Some Str 1");