/** * 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 java.io.IOException */ public CrossClassLoaderSerializationProxy(Object mockitoMock) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out); objectOutputStream.writeObject(mockitoMock); objectOutputStream.close(); out.close(); MockCreationSettings<?> mockSettings = MockUtil.getMockSettings(mockitoMock); this.serializedMock = out.toByteArray(); this.typeToMock = mockSettings.getTypeToMock(); this.extraInterfaces = mockSettings.getExtraInterfaces(); }
public static boolean isSpy(Object mock) { return isMock(mock) && getMockSettings(mock).getDefaultAnswer() == Mockito.CALLS_REAL_METHODS; }
/** * 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(); }
/** * Creates a mock using the Generics Metadata. * * <li>Finally as we want to mock the actual type, but we want to pass along the contextual generics meta-data * that was resolved for the current return type, for this to happen we associate to the mock an new instance of * {@link ReturnsDeepStubs} answer in which we will store the returned type generic metadata. * * @param returnTypeGenericMetadata The metadata to use to create the new mock. * @param parentMock The parent of the current deep stub mock. * @return The mock */ private Object newDeepStubMock(GenericMetadataSupport returnTypeGenericMetadata, Object parentMock) { MockCreationSettings parentMockSettings = MockUtil.getMockSettings(parentMock); return mockitoCore().mock( returnTypeGenericMetadata.rawType(), withSettingsUsing(returnTypeGenericMetadata, parentMockSettings) ); }
} catch (IOException ioe) { MockName mockName = MockUtil.getMockName(mockitoMock); String mockedType = MockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName(); throw new MockitoSerializationIssue(join( "The mock '" + mockName + "' of type '" + mockedType + "'",
public Object answer(InvocationOnMock invocation) { if (isToStringMethod(invocation.getMethod())) { Object mock = invocation.getMock(); MockName name = MockUtil.getMockName(mock); if (name.isDefault()) { return "Mock for " + MockUtil.getMockSettings(mock).getTypeToMock().getSimpleName() + ", hashCode: " + mock.hashCode(); } else { return name.toString(); } } else if (isCompareToMethod(invocation.getMethod())) { //see issue 184. //mocks by default should return 0 if references are the same, otherwise some other value because they are not the same. Hence we return 1 (anything but 0 is good). //Only for compareTo() method by the Comparable interface return invocation.getMock() == invocation.getArgument(0) ? 0 : 1; } Class<?> returnType = invocation.getMethod().getReturnType(); return returnValueFor(returnType); }
MockUtil mockUtil = new MockUtil(); MockName mockName = mockUtil.getMockName(mockitoMock); String mockedType = mockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName(); throw new MockitoSerializationIssue(join( "The mock '" + mockName + "' of type '" + mockedType + "'",