1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.plugins; 6 7 import org.mockito.Incubating; 8 import org.mockito.invocation.MockHandler; 9 import org.mockito.mock.MockCreationSettings; 10 11 /** 12 * The facility to create mocks. 13 * 14 * <p>By default, an internal cglib/asm/objenesis based implementation is used.</p> 15 * 16 * <p>{@code MockMaker} is an extension point that makes it possible to use custom dynamic proxies 17 * and avoid using the default cglib/asm/objenesis implementation. 18 * For example, the android users can use a MockMaker that can work with Dalvik virtual machine 19 * and hence bring Mockito to android apps developers.</p> 20 * 21 * <h3>Using the extension point</h3> 22 * 23 * <p>Suppose you wrote an extension to create mocks with some <em>Awesome</em> library, in order to tell 24 * Mockito to use it you need to put in your <strong>classpath</strong>: 25 * <ol style="list-style-type: lower-alpha"> 26 * <li>The implementation itself, for example <code>org.awesome.mockito.AwesomeMockMaker</code> that extends the <code>MockMaker</code>.</li> 27 * <li>A file "<code>mockito-extensions/org.mockito.plugins.MockMaker</code>". The content of this file is 28 * exactly a <strong>one</strong> line with the qualified name: <code>org.awesome.mockito.AwesomeMockMaker</code>.</li> 29 * </ol></p> 30 * 31 * <p>Note that if several <code>mockito-extensions/org.mockito.plugins.MockMaker</code> files exists in the classpath 32 * Mockito will only use the first returned by the standard {@link ClassLoader#getResource} mechanism. 33 * 34 * @see org.mockito.mock.MockCreationSettings 35 * @see org.mockito.invocation.MockHandler 36 * @since 1.9.5 37 */ 38 @Incubating 39 public interface MockMaker { 40 41 /** 42 * If you want to provide your own implementation of {@code MockMaker} this method should: 43 * <ul> 44 * <li>Create a proxy object that implements {@code settings.typeToMock} and potentially also {@code settings.extraInterfaces}.</li> 45 * <li>You may use the information from {@code settings} to create/configure your proxy object.</li> 46 * <li>Your proxy object should carry the {@code handler} with it. For example, if you generate byte code 47 * to create the proxy you could generate an extra field to keep the {@code handler} with the generated object. 48 * Your implementation of {@code MockMaker} is required to provide this instance of {@code handler} when 49 * {@link #getHandler(Object)} is called. 50 * </li> 51 * </ul> 52 * 53 * @param settings - mock creation settings like type to mock, extra interfaces and so on. 54 * @param handler See {@link org.mockito.invocation.MockHandler}. 55 * <b>Do not</b> provide your own implementation at this time. Make sure your implementation of 56 * {@link #getHandler(Object)} will return this instance. 57 * @param <T> Type of the mock to return, actually the <code>settings.getTypeToMock</code>. 58 * @return The mock instance. 59 * @since 1.9.5 60 */ createMock( MockCreationSettings<T> settings, MockHandler handler )61 <T> T createMock( 62 MockCreationSettings<T> settings, 63 MockHandler handler 64 ); 65 66 /** 67 * Returns the handler for the {@code mock}. <b>Do not</b> provide your own implementations at this time 68 * because the work on the {@link MockHandler} api is not completed. 69 * Use the instance provided to you by Mockito at {@link #createMock} or {@link #resetMock}. 70 * 71 * @param mock The mock instance. 72 * @return may return null - it means that there is no handler attached to provided object. 73 * This means the passed object is not really a Mockito mock. 74 * @since 1.9.5 75 */ getHandler(Object mock)76 MockHandler getHandler(Object mock); 77 78 /** 79 * Replaces the existing handler on {@code mock} with {@code newHandler}. 80 * 81 * <p>The invocation handler actually store invocations to achieve 82 * stubbing and verification. In order to reset the mock, we pass 83 * a new instance of the invocation handler.</p> 84 * 85 * <p>Your implementation should make sure the {@code newHandler} is correctly associated to passed {@code mock}</p> 86 * 87 * @param mock The mock instance whose invocation handler is to be replaced. 88 * @param newHandler The new invocation handler instance. 89 * @param settings The mock settings - should you need to access some of the mock creation details. 90 * @since 1.9.5 91 */ resetMock( Object mock, MockHandler newHandler, MockCreationSettings settings )92 void resetMock( 93 Object mock, 94 MockHandler newHandler, 95 MockCreationSettings settings 96 ); 97 }