My problem is defined in header, actually. I am writing unit tests with 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".
The any*
family of matchers do not do type checking, you should use isA(Class<T>)
instead:
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.