Easymock isA vs anyObject

emi*_*lyk 16 easymock

有什么区别

EasyMock.isA(String.class) 
Run Code Online (Sandbox Code Playgroud)

EasyMock.anyObject(String.class)
Run Code Online (Sandbox Code Playgroud)

(或任何其他类提供)

在什么情况下你会使用一个而不是另一个?

ter*_*kid 22

区别在于检查Null.isA在null时返回false,但anyObject也返回true.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在示例中,testIsAWithNull应该失败.