@SuppressWarnings("unchecked") private void addAnswersForStubbing(MockitoMethodInvocationControl invocationControl) { final MockHandler mockHandler = invocationControl.getInvocationHandler().getHandler(); final List list = Whitebox.getInternalState(this, List.class); try { Whitebox.invokeMethod(mockHandler, "setAnswersForStubbing", list); } catch (Exception e) { throw new RuntimeException(e); } }
public MockHandler getHandler(Object mock) { // Return a fake mock handler for static method mocks if(mock instanceof Class) { return new PowerMockInternalMockHandler((Class<?>) mock); } else { return cglibMockMaker.getHandler(mock); } }
private MockHandler replaceMatchersBinderIfNeeded(MockHandler mockHandler) { if(!Whitebox.getFieldsOfType(mockHandler, MatchersBinder.class).isEmpty()) { Whitebox.setInternalState(mockHandler, new PowerMockMatchersBinder()); } else if(!Whitebox.getFieldsOfType(mockHandler, InternalMockHandler.class).isEmpty()) { final MockHandler internalMockHandler = Whitebox.getInternalState(mockHandler, MockHandler.class); return replaceMatchersBinderIfNeeded(internalMockHandler); } return mockHandler; }
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; }
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(); }
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; }
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); }
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()); } }
@Override public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); if (adapter != null) { adapter.setHandler(newHandler); } }
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) { return cglibMockMaker.createMock(settings, handler); }
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { cglibMockMaker.resetMock(mock, newHandler, settings); }
public MockHandler getHandler() { return handler; }
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) { InternalMockHandler mockitoHandler = cast(handler); new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings); return new ClassImposterizer(new InstantiatorProvider().getInstantiator(settings)).imposterise( new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces()); }
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { ((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings)); }
public InvocationHandlerAdapter(MockHandler handler) { this.handler = handler; }
public void setHandler(MockHandler handler) { this.handler = handler; }
@Override public MockHandler getHandler(Object mock) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); return adapter != null ? adapter.getHandler() : null; }
@Override @SuppressWarnings("rawtypes") public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); adapter.setHandler(newHandler); }
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()); }
@Override public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); adapter.setHandler(newHandler); }
InvocationHandlerAdapter(MockHandler handler) { this.handler = handler; }
/** * Set a new handler for this adapter. */ void setHandler(MockHandler handler) { this.handler = handler; }
/** * 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 );
/** * Returns the handler for the {@code mock}. <b>Do not</b> provide your own implementations at this time * because the work on the {@link MockHandler} api is not completed. * Use the instance provided to you by Mockito at {@link #createMock} or {@link #resetMock}. * * @param mock The mock instance. * @return may return null - it means that there is no handler attached to provided object. * This means the passed object is not really a Mockito mock. * @since 1.9.5 */ MockHandler getHandler(Object mock);
/** * 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 );
/** * Get the handler registered with this adapter. * * @return handler */ MockHandler getHandler() { return handler; }