All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jasmine.framework.test.mockito.CustomAnswer Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
package jasmine.framework.test.mockito;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.lang.reflect.Method;

/**
 * @author mh.z
 * @param 
 */
public class CustomAnswer implements Answer {
    private boolean mockFlag;

    public CustomAnswer() {
        this.mockFlag = false;
    }

    /**
     * 模拟对象
     *
     * @param target
     * @param mock
     */
    public void mock(T target, CustomMock mock) {
        mockFlag = true;

        try {
            mock.mock(target);
        } finally {
            mockFlag = false;
        }
    }

    @Override
    public T answer(InvocationOnMock invocation) throws Throwable {
        if (mockFlag) {
            return null;
        }

        Object object = invocation.getMock();
        String className = object.getClass().getName();
        Method method = invocation.getMethod();
        String methodName = method.getName();

        throw new UnsupportedOperationException(className + "." + methodName + " should be mocked");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy