/** * Creates a mock with some non-standard settings. * <p> * The number of configuration points for a mock grows * so we need a fluent way to introduce new configuration without adding more and more overloaded Mockito.mock() methods. * Hence {@link MockSettings}. * <pre class="code"><code class="java"> * Listener mock = mock(Listener.class, withSettings() * .name("firstListner").defaultBehavior(RETURNS_SMART_NULLS)); * ); * </code></pre> * <b>Use it carefully and occasionally</b>. What might be reason your test needs non-standard mocks? * Is the code under test so complicated that it requires non-standard mocks? * Wouldn't you prefer to refactor the code under test so it is testable in a simple way? * <p> * See also {@link Mockito#withSettings()} * <p> * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param mockSettings additional mock settings * @return mock object */ public static <T> T mock(Class<T> classToMock, MockSettings mockSettings) { return MOCKITO_CORE.mock(classToMock, mockSettings); }
/** * Creates a mock with some non-standard settings. * <p> * The number of configuration points for a mock grows * so we need a fluent way to introduce new configuration without adding more and more overloaded Mockito.mock() methods. * Hence {@link MockSettings}. * <pre class="code"><code class="java"> * Listener mock = mock(Listener.class, withSettings() * .name("firstListner").defaultBehavior(RETURNS_SMART_NULLS)); * ); * </code></pre> * <b>Use it carefully and occasionally</b>. What might be reason your test needs non-standard mocks? * Is the code under test so complicated that it requires non-standard mocks? * Wouldn't you prefer to refactor the code under test so it is testable in a simple way? * <p> * See also {@link Mockito#withSettings()} * <p> * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param mockSettings additional mock settings * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock, MockSettings mockSettings) { return MOCKITO_CORE.mock(classToMock, mockSettings); }
Object returnValueFor(Class<?> clazz) { if (!mockitoCore.isTypeMockable(clazz)) { return null; } return mockitoCore.mock(clazz, new MockSettingsImpl().defaultAnswer(this)); } }
Object returnValueFor(Class<?> clazz) { if (!ClassImposterizer.INSTANCE.canImposterise(clazz)) { return null; } return mockitoCore.mock(clazz, new MockSettingsImpl().defaultAnswer(this)); } }
/** * 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) ); }
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
@CheckReturnValue public static <T> T spy(Class<T> classToSpy) { return MOCKITO_CORE.mock(classToSpy, withSettings() .useConstructor() .defaultAnswer(CALLS_REAL_METHODS));
/** * Creates a mock with some non-standard settings. * <p> * The number of configuration points for a mock grows * so we need a fluent way to introduce new configuration without adding more and more overloaded Mockito.mock() methods. * Hence {@link MockSettings}. * <pre> * Listener mock = mock(Listener.class, withSettings() * .name("firstListner").defaultBehavior(RETURNS_SMART_NULLS)); * ); * </pre> * <b>Use it carefully and occasionally</b>. What might be reason your test needs non-standard mocks? * Is the code under test so complicated that it requires non-standard mocks? * Wouldn't you prefer to refactor the code under test so it is testable in a simple way? * <p> * See also {@link Mockito#withSettings()} * <p> * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param mockSettings additional mock settings * @return mock object */ public static <T> T mock(Class<T> classToMock, MockSettings mockSettings) { return MOCKITO_CORE.mock(classToMock, mockSettings); }
Object returnValueFor(Class<?> clazz) { if (!ClassImposterizer.INSTANCE.canImposterise(clazz)) { return null; } return mockitoCore.mock(clazz, new MockSettingsImpl().defaultAnswer(this)); } }
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));