Q:

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. 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".

A:

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, e.g.:
final Class1ImplementsSameAbstract klass1 = new Class1ImplementsSameAbstract();

PowerMockito.when(mockUser.enter(klass1)).thenReturn("Some Str 1");

