org.mockito.mock.MockCreationSettings Java Examples

The following examples show how to use org.mockito.mock.MockCreationSettings. 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: MockHandlerFactoryTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
//see issue 331
public void valid_handle_result_is_permitted() throws Throwable {
    //given:
    MockCreationSettings settings = (MockCreationSettings) new MockSettingsImpl().defaultAnswer(new Returns(123));
    InternalMockHandler handler = new MockHandlerFactory().create(settings);

    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();

    //when:
    Object result = handler.handle(invocation);

    //then
    assertEquals(123, result);
}
 
Example #2
Source File: MockHandlerFactoryTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
//see issue 331
public void handle_result_must_not_be_null_for_primitives() throws Throwable {
    //given:
    MockCreationSettings settings = (MockCreationSettings) new MockSettingsImpl().defaultAnswer(new Returns(null));
    InternalMockHandler handler = new MockHandlerFactory().create(settings);

    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();

    //when:
    Object result = handler.handle(invocation);

    //then null value is not a valid result for a primitive
    assertNotNull(result);
    assertEquals(0, result);
}
 
Example #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
Source File: AcrossJVMSerializationFeature.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates the wrapper that be used in the serialization stream.
 *
 * <p>Immediately serializes the Mockito mock using specifically crafted {@link MockitoMockObjectOutputStream},
 * in a byte array.</p>
 *
 * @param mockitoMock The Mockito mock to serialize.
 * @throws IOException
 */
public AcrossJVMMockSerializationProxy(Object mockitoMock) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out);

    objectOutputStream.writeObject(mockitoMock);

    objectOutputStream.close();
    out.close();

    MockCreationSettings mockSettings = new MockUtil().getMockSettings(mockitoMock);
    this.serializedMock = out.toByteArray();
    this.typeToMock = mockSettings.getTypeToMock();
    this.extraInterfaces = mockSettings.getExtraInterfaces();
}
 
Example #10
Source File: MockHandlerFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public InternalMockHandler create(MockCreationSettings settings) {
    InternalMockHandler handler = new MockHandlerImpl(settings);
    InternalMockHandler nullResultGuardian = new NullResultGuardian(handler);
    InternalMockHandler notifier = new InvocationNotifierHandler(nullResultGuardian, settings);

    return notifier;
}
 
Example #11
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 #12
Source File: InvocationContainerImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private RegisteredInvocations createRegisteredInvocations(MockCreationSettings mockSettings) {
    return mockSettings.isStubOnly()
      ? new SingleRegisteredInvocation()
      : new DefaultRegisteredInvocations();
}
 
Example #13
Source File: InvocationNotifierHandler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public InvocationNotifierHandler(InternalMockHandler<T> mockHandler, MockCreationSettings settings) {
    this.mockHandler = mockHandler;
    this.invocationListeners = settings.getInvocationListeners();
}
 
Example #14
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 #15
Source File: InvocationNotifierHandler.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockCreationSettings getMockSettings() {
    return mockHandler.getMockSettings();
}
 
Example #16
Source File: NullResultGuardian.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockCreationSettings getMockSettings() {
    return delegate.getMockSettings();
}
 
Example #17
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 #18
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 #19
Source File: MockHandlerImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockHandlerImpl(MockCreationSettings mockSettings) {
    this.mockSettings = mockSettings;
    this.mockingProgress = new ThreadSafeMockingProgress();
    this.matchersBinder = new MatchersBinder();
    this.invocationContainerImpl = new InvocationContainerImpl(mockingProgress, mockSettings);
}
 
Example #20
Source File: MockUtil.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockCreationSettings getMockSettings(Object mock) {
    return getMockHandler(mock).getMockSettings();
}
 
Example #21
Source File: MockHandlerImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockCreationSettings getMockSettings() {
    return mockSettings;
}
 
Example #22
Source File: InvocationContainerImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public InvocationContainerImpl(MockingProgress mockingProgress, MockCreationSettings mockSettings) {
    this.mockingProgress = mockingProgress;
    this.registeredInvocations = createRegisteredInvocations(mockSettings);
}
 
Example #23
Source File: MockSettingsImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MockCreationSettings<T> confirm(Class<T> typeToMock) {
    return validatedSettings(typeToMock, this);
}
 
Example #24
Source File: MethodInterceptorFilter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public MethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) {
    this.handler = handler;
    this.mockSettings = mockSettings;
}
 
Example #25
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 #26
Source File: AcrossJVMSerializationFeature.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Enable serialization serialization that will work across classloaders / and JVM.
 *
 * <p>Only enable if settings says the mock should be serializable. In this case add the
 * {@link AcrossJVMMockitoMockSerializable} to the extra interface list.</p>
 *
 * @param settings Mock creation settings.
 * @param <T> Type param to not be bothered by the generics
 */
public <T> void enableSerializationAcrossJVM(MockCreationSettings<T> settings) {
    if (settings.getSerializableMode() == SerializableMode.ACROSS_CLASSLOADERS) {
        // havin faith that this set is modifiable
        // TODO use a proper way to add the interface
        settings.getExtraInterfaces().add(AcrossJVMMockitoMockSerializable.class);
    }
}
 
Example #27
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
);
 
Example #28
Source File: MockMaker.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * If you want to provide your own implementation of {@code MockMaker} this method should:
 * <ul>
 *     <li>Create a proxy object that implements {@code settings.typeToMock} and potentially also {@code settings.extraInterfaces}.</li>
 *     <li>You may use the information from {@code settings} to create/configure your proxy object.</li>
 *     <li>Your proxy object should carry the {@code handler} with it. For example, if you generate byte code
 *     to create the proxy you could generate an extra field to keep the {@code handler} with the generated object.
 *     Your implementation of {@code MockMaker} is required to provide this instance of {@code handler} when
 *     {@link #getHandler(Object)} is called.
 *     </li>
 * </ul>
 *
 * @param settings - mock creation settings like type to mock, extra interfaces and so on.
 * @param handler See {@link org.mockito.invocation.MockHandler}.
 *                <b>Do not</b> provide your own implementation at this time. Make sure your implementation of
 *                {@link #getHandler(Object)} will return this instance.
 * @param <T> Type of the mock to return, actually the <code>settings.getTypeToMock</code>.
 * @return The mock instance.
 * @since 1.9.5
 */
<T> T createMock(
        MockCreationSettings<T> settings,
        MockHandler handler
);
 
Example #29
Source File: InternalMockHandler.java    From astor with GNU General Public License v2.0 votes vote down vote up
MockCreationSettings getMockSettings();