下面列出了怎么用org.mockito.mock.MockCreationSettings的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
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);
}
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
if (adapter != null) {
adapter.setHandler(newHandler);
}
}
@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;
}
@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;
}
@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);
}
}
}
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 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 ClassImposterizer.INSTANCE.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 MockCreationSettings<T> confirm(Class<T> typeToMock) {
return validatedSettings(typeToMock, this);
}
public MockCreationSettings getMockSettings(Object mock) {
return getMockHandler(mock).getMockSettings();
}
@Override
@SuppressWarnings("rawtypes")
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
adapter.setHandler(newHandler);
}
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
adapter.setHandler(newHandler);
}
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
for (MockMaker mockMaker : MOCK_MAKERS) {
mockMaker.resetMock(mock, newHandler, settings);
}
}
/**
* 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();