Ответ 1
Разница заключается в проверке Nulls. IsA возвращает false, когда null, но anyObject возвращает true для null.
import static org.easymock.EasyMock.*;
import org.easymock.EasyMock;
import org.testng.annotations.Test;
public class Tests {
private IInterface createMock(boolean useIsA) {
IInterface testInstance = createStrictMock(IInterface.class);
testInstance.testMethod(
useIsA ? isA(String.class) : anyObject(String.class)
);
expectLastCall();
replay(testInstance);
return testInstance;
}
private void runTest(boolean isACall, boolean isNull) throws Exception {
IInterface testInstance = createMock(isACall);
testInstance.testMethod(isNull ? null : "");
verify(testInstance);
}
@Test
public void testIsAWithString() throws Exception {
runTest(true, false);
}
@Test
public void testIsAWithNull() throws Exception {
runTest(true, true);
}
@Test
public void testAnyObjectWithString() throws Exception {
runTest(false, true);
}
@Test
public void testAnyObjectWithNull() throws Exception {
runTest(false, false);
}
interface IInterface {
void testMethod(String parameter);
}
}
В примере testIsAWithNull должен завершиться ошибкой.