org.mockito.invocation.MockHandler Java Examples

The following examples show how to use org.mockito.invocation.MockHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CglibMockMaker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private InternalMockHandler cast(MockHandler handler) {
    if (!(handler instanceof InternalMockHandler)) {
        throw new MockitoException("At the moment you cannot provide own implementations of MockHandler." +
                "\nPlease see the javadocs for the MockMaker interface.");
    }
    return (InternalMockHandler) handler;
}
 
Example #2
Source File: InlineStaticMockMaker.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    if (adapter != null) {
        if (mockingInProgressClass.get() == mock.getClass()) {
            markerToHandler.remove(mock);
            classToMarker.remove(mock.getClass());
        } else {
            adapter.setHandler(newHandler);
        }
    }
}
 
Example #3
Source File: CglibMockMaker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public MockHandler getHandler(Object mock) {
    if (!(mock instanceof Factory)) {
        return null;
    }
    Factory factory = (Factory) mock;
    Callback callback = factory.getCallback(0);
    if (!(callback instanceof MethodInterceptorFilter)) {
        return null;
    }
    return ((MethodInterceptorFilter) callback).getHandler();
}
 
Example #4
Source File: MockUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> T createMock(MockCreationSettings<T> settings) {
    MockHandler mockHandler = new MockHandlerFactory().create(settings);

    T mock = mockMaker.createMock(settings, mockHandler);

    Object spiedInstance = settings.getSpiedInstance();
    if (spiedInstance != null) {
        new LenientCopyTool().copyToMock(spiedInstance, mock);
    }

    return mock;
}
 
Example #5
Source File: MockUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> void resetMock(T mock) {
    InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock);
    MockCreationSettings settings = oldHandler.getMockSettings();
    MockHandler newHandler = new MockHandlerFactory().create(settings);

    mockMaker.resetMock(mock, newHandler, settings);
}
 
Example #6
Source File: MockUtil.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public <T> InternalMockHandler<T> getMockHandler(T mock) {
    if (mock == null) {
        throw new NotAMockException("Argument should be a mock, but is null!");
    }

    if (isMockitoMock(mock)) {
        MockHandler handler = mockMaker.getHandler(mock);
        return (InternalMockHandler) handler;
    } else {
        throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
    }
}
 
Example #7
Source File: InlineStaticMockMaker.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    Class<T> typeToMock = settings.getTypeToMock();
    if (!typeToMock.equals(mockingInProgressClass.get()) || Modifier.isAbstract(typeToMock
            .getModifiers())) {
        return null;
    }

    Set<Class<?>> interfacesSet = settings.getExtraInterfaces();
    InvocationHandlerAdapter handlerAdapter = new InvocationHandlerAdapter(handler);

    classTransformer.mockClass(MockFeatures.withMockFeatures(typeToMock, interfacesSet));

    Instantiator instantiator = Mockito.framework().getPlugins().getDefaultPlugin
            (InstantiatorProvider2.class).getInstantiator(settings);

    T mock;
    try {
        mock = instantiator.newInstance(typeToMock);
    } catch (org.mockito.creation.instance.InstantiationException e) {
        throw new MockitoException("Unable to create mock instance of type '" + typeToMock
                .getSimpleName() + "'", e);
    }

    if (classToMarker.containsKey(typeToMock)) {
        throw new MockitoException(typeToMock + " is already mocked");
    }
    classToMarker.put(typeToMock, mock);

    markerToHandler.put(mock, handlerAdapter);
    return mock;
}
 
Example #8
Source File: MockMakerMultiplexer.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
@Override
public MockHandler getHandler(Object mock) {
    for (MockMaker mockMaker : MOCK_MAKERS) {
        MockHandler handler = mockMaker.getHandler(mock);

        if (handler != null) {
            return handler;
        }
    }

    return null;
}
 
Example #9
Source File: MockMakerMultiplexer.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    for (MockMaker mockMaker : MOCK_MAKERS) {
        T mock = mockMaker.createMock(settings, handler);

        if (mock != null) {
            return mock;
        }
    }

    return null;
}
 
Example #10
Source File: InlineDexmakerMockMaker.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    if (adapter != null) {
        adapter.setHandler(newHandler);
    }
}
 
Example #11
Source File: MockMakerMultiplexer.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    for (MockMaker mockMaker : MOCK_MAKERS) {
        mockMaker.resetMock(mock, newHandler, settings);
    }
}
 
Example #12
Source File: DexmakerMockMaker.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    adapter.setHandler(newHandler);
}
 
Example #13
Source File: DexmakerMockMaker.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
@Override
public MockHandler getHandler(Object mock) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    return adapter != null ? adapter.getHandler() : null;
}
 
Example #14
Source File: InvocationHandlerAdapter.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
InvocationHandlerAdapter(MockHandler handler) {
    this.handler = handler;
}
 
Example #15
Source File: InvocationHandlerAdapter.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
/**
 * Set a new handler for this adapter.
 */
void setHandler(MockHandler handler) {
    this.handler = handler;
}
 
Example #16
Source File: InlineDexmakerMockMaker.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
@Override
public MockHandler getHandler(Object mock) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    return adapter != null ? adapter.getHandler() : null;
}
 
Example #17
Source File: InvocationHandlerAdapter.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
public InvocationHandlerAdapter(MockHandler handler) {
    this.handler = handler;
}
 
Example #18
Source File: InlineStaticMockMaker.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
@Override
public MockHandler getHandler(Object mock) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    return adapter != null ? adapter.getHandler() : null;
}
 
Example #19
Source File: InvocationHandlerAdapter.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
public void setHandler(MockHandler handler) {
    this.handler = handler;
}
 
Example #20
Source File: InvocationHandlerAdapter.java    From dexmaker with Apache License 2.0 4 votes vote down vote up
public MockHandler getHandler() {
    return handler;
}
 
Example #21
Source File: MethodInterceptorFilter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockHandler getHandler() {
    return handler;
}
 
Example #22
Source File: IosMockMaker.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
  InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
  adapter.setHandler(newHandler);
}
 
Example #23
Source File: IosMockMaker.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public MockHandler getHandler(Object mock) {
  InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
  return adapter != null ? adapter.getHandler() : null;
}
 
Example #24
Source File: InvocationHandlerAdapter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public void setHandler(MockHandler handler) {
  this.handler = handler;
}
 
Example #25
Source File: InvocationHandlerAdapter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public MockHandler getHandler() {
  return handler;
}
 
Example #26
Source File: InvocationHandlerAdapter.java    From j2objc with Apache License 2.0 4 votes vote down vote up
public InvocationHandlerAdapter(MockHandler handler) {
  this.handler = handler;
}
 
Example #27
Source File: CglibMockMaker.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    ((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings));
}
 
Example #28
Source File: CglibMockMaker.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    InternalMockHandler mockitoHandler = cast(handler);
    new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings);
    return ClassImposterizer.INSTANCE.imposterise(
            new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces());
}
 
Example #29
Source File: InvocationHandlerAdapter.java    From dexmaker with Apache License 2.0 2 votes vote down vote up
/**
 * Get the handler registered with this adapter.
 *
 * @return handler
 */
MockHandler getHandler() {
    return handler;
}
 
Example #30
Source File: MockMaker.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Replaces the existing handler on {@code mock} with {@code newHandler}.
 *
 * <p>The invocation handler actually store invocations to achieve
 * stubbing and verification. In order to reset the mock, we pass
 * a new instance of the invocation handler.</p>
 *
 * <p>Your implementation should make sure the {@code newHandler} is correctly associated to passed {@code mock}</p>
 *
 * @param mock The mock instance whose invocation handler is to be replaced.
 * @param newHandler The new invocation handler instance.
 * @param settings The mock settings - should you need to access some of the mock creation details.
 * @since 1.9.5
 */
void resetMock(
        Object mock,
        MockHandler newHandler,
        MockCreationSettings settings
);