Search in sources :

Example 1 with MockUtil

use of org.mockito.internal.util.MockUtil in project gwt-test-utils by gwt-test-utils.

the class GwtSpyAnnotationEngine method process.

@Override
@SuppressWarnings("deprecation")
public // for MockitoAnnotations.Mock
void process(Class<?> context, Object testInstance) {
    Field[] fields = context.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
            assertNoIncompatibleAnnotations(Spy.class, field, com.googlecode.gwt.test.Mock.class, Mock.class, org.mockito.MockitoAnnotations.Mock.class, Captor.class);
            Object instance = null;
            try {
                FieldInitializationReport report = new FieldInitializer(testInstance, field).initialize();
                instance = report.fieldInstance();
            } catch (MockitoException e) {
                new Reporter().cannotInitializeForSpyAnnotation(field.getName(), e);
            }
            try {
                if (new MockUtil().isMock(instance)) {
                    // instance has been spied earlier
                    // for example happens when MockitoAnnotations.initMocks is called two times.
                    Mockito.reset(instance);
                } else {
                    field.setAccessible(true);
                    field.set(testInstance, Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance).defaultAnswer(Mockito.CALLS_REAL_METHODS).name(field.getName())));
                }
            } catch (IllegalAccessException e) {
                throw new MockitoException("Problems initiating spied field " + field.getName(), e);
            }
        }
    }
}
Also used : FieldInitializationReport(org.mockito.internal.util.reflection.FieldInitializationReport) MockUtil(org.mockito.internal.util.MockUtil) Reporter(org.mockito.exceptions.Reporter) FieldInitializer(org.mockito.internal.util.reflection.FieldInitializer) Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 2 with MockUtil

use of org.mockito.internal.util.MockUtil in project weex-example by KalicyZhou.

the class DefaultUriAdapterTest method setup.

@Before
public void setup() {
    WXEnvironment.sApplication = RuntimeEnvironment.application;
    WXSDKManager wxsdkManager = WXSDKManager.getInstance();
    if (!new MockUtil().isSpy(wxsdkManager)) {
        WXSDKManager spy = Mockito.spy(wxsdkManager);
        WXSDKManagerTest.setInstance(spy);
        Mockito.when(spy.getIWXHttpAdapter()).thenReturn(new IWXHttpAdapter() {

            @Override
            public void sendRequest(WXRequest request, OnHttpListener listener) {
            //do nothing.
            }
        });
    }
    adapter = new DefaultUriAdapter();
    instance = WXSDKInstanceTest.createInstance();
}
Also used : MockUtil(org.mockito.internal.util.MockUtil) WXSDKManager(com.taobao.weex.WXSDKManager) WXRequest(com.taobao.weex.common.WXRequest) Before(org.junit.Before)

Example 3 with MockUtil

use of org.mockito.internal.util.MockUtil in project sakuli by ConSol.

the class BaseTest method initBaseActionLoader.

protected void initBaseActionLoader() {
    loaderMock = BeanLoader.loadBean(BaseActionLoaderImpl.class);
    if (new MockUtil().isMock(loaderMock)) {
        reset(loaderMock);
        when(loaderMock.getSahiReport()).thenReturn(mock(Report.class));
    }
}
Also used : BaseActionLoaderImpl(org.sakuli.loader.BaseActionLoaderImpl) Report(net.sf.sahi.report.Report) MockUtil(org.mockito.internal.util.MockUtil)

Aggregations

MockUtil (org.mockito.internal.util.MockUtil)3 WXSDKManager (com.taobao.weex.WXSDKManager)1 WXRequest (com.taobao.weex.common.WXRequest)1 Field (java.lang.reflect.Field)1 Report (net.sf.sahi.report.Report)1 Before (org.junit.Before)1 Reporter (org.mockito.exceptions.Reporter)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 FieldInitializationReport (org.mockito.internal.util.reflection.FieldInitializationReport)1 FieldInitializer (org.mockito.internal.util.reflection.FieldInitializer)1 BaseActionLoaderImpl (org.sakuli.loader.BaseActionLoaderImpl)1