@Test public void createMock() throws Exception { MockDefinition definition = new MockDefinition("name", ExampleService.class, new Class<?>[] { ExampleExtraInterface.class }, Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE, true); ExampleService mock = definition.createMock(); MockCreationSettings<?> settings = new MockUtil().getMockSettings(mock); assertThat(mock).isInstanceOf(ExampleService.class); assertThat(mock).isInstanceOf(ExampleExtraInterface.class); assertThat(settings.getMockName().toString()).isEqualTo("name"); assertThat(settings.getDefaultAnswer()) .isEqualTo(Answers.RETURNS_SMART_NULLS.get()); assertThat(settings.isSerializable()).isTrue(); assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE); }
@Test public void createSpy() throws Exception { SpyDefinition definition = new SpyDefinition("name", RealExampleService.class, MockReset.BEFORE, true); RealExampleService spy = definition.createSpy(new RealExampleService("hello")); MockCreationSettings<?> settings = new MockUtil().getMockSettings(spy); assertThat(spy).isInstanceOf(ExampleService.class); assertThat(settings.getMockName().toString()).isEqualTo("name"); assertThat(settings.getDefaultAnswer()) .isEqualTo(Answers.CALLS_REAL_METHODS.get()); assertThat(MockReset.get(spy)).isEqualTo(MockReset.BEFORE); }
public InternalMockHandler create(MockCreationSettings settings) { InternalMockHandler handler = new MockHandlerImpl(settings); InternalMockHandler nullResultGuardian = new NullResultGuardian(handler); InternalMockHandler notifier = new InvocationNotifierHandler(nullResultGuardian, settings); return notifier; }
/** * 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(); }
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); }
@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); }
@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); }
@Override public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); if (adapter != null) { adapter.setHandler(newHandler); } }
@SuppressWarnings("rawtypes") // The MockCreationListener interface forces this @Override public void onMockCreated(Object mock, MockCreationSettings settings) { mocks.add(mock); }
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 MockCreationSettings getMockSettings() { final MockSettingsImpl mockSettings = new MockSettingsImpl(); mockSettings.setMockName(new MockNameImpl(mock.getName())); mockSettings.setTypeToMock(mock); return mockSettings; }
public PowerMockMethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) { super(handler, mockSettings); }
public MethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) { this.handler = handler; this.mockSettings = mockSettings; }
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)); }
/** * Creates the wrapper that be used in the serialization stream. * * <p>Immediately serializes the Mockito mock using specifically crafted * {@link AcrossJVMSerializationFeature.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(); }
public MockCreationSettings<T> confirm(Class<T> typeToMock) { return validatedSettings(typeToMock, this); }
@Override @SuppressWarnings("rawtypes") public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); adapter.setHandler(newHandler); }
public InvocationContainerImpl(MockingProgress mockingProgress, MockCreationSettings mockSettings) { this.mockingProgress = mockingProgress; this.registeredInvocations = createRegisteredInvocations(mockSettings); }
private RegisteredInvocations createRegisteredInvocations(MockCreationSettings mockSettings) { return mockSettings.isStubOnly() ? new SingleRegisteredInvocation() : new DefaultRegisteredInvocations(); }
public InvocationNotifierHandler(InternalMockHandler<T> mockHandler, MockCreationSettings settings) { this.mockHandler = mockHandler; this.invocationListeners = settings.getInvocationListeners(); }
public MockCreationSettings getMockSettings() { return mockHandler.getMockSettings(); }
public MockCreationSettings getMockSettings() { return delegate.getMockSettings(); }
public MockHandlerImpl(MockCreationSettings mockSettings) { this.mockSettings = mockSettings; this.mockingProgress = new ThreadSafeMockingProgress(); this.matchersBinder = new MatchersBinder(); this.invocationContainerImpl = new InvocationContainerImpl(mockingProgress, mockSettings); }
public MockCreationSettings getMockSettings() { return mockSettings; }
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()); }
public MockCreationSettings getMockSettings(Object mock) { return getMockHandler(mock).getMockSettings(); }
@Override public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock); adapter.setHandler(newHandler); }
/** * Enable serialization serialization that will work across classloaders / and JVM. * <p/> * <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); } }
/** * 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); } }
/** * 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 );
/** * 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 );
MockCreationSettings getMockSettings();