Refine search
/** * Creates mock object of given class or interface. * <p> * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @return mock object */ public static <T> T mock(Class<T> classToMock) { return mock(classToMock, withSettings().defaultAnswer(RETURNS_DEFAULTS)); }
/** * Creates mock with a specified strategy for its answers to interactions. * It's quite advanced feature and typically you don't need it to write decent tests. * However it can be helpful when working with legacy systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> stub the method call. * * <pre class="code"><code class="java"> * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); * Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); * </code></pre> * * <p>See examples in javadoc for {@link Mockito} class</p> * * @param classToMock class or interface to mock * @param defaultAnswer default answer for unstubbed methods * * @return mock object */ public static <T> T mock(Class<T> classToMock, Answer defaultAnswer) { return mock(classToMock, withSettings().defaultAnswer(defaultAnswer)); }
private static Object spyInstance(Field field, Object instance) { return Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance) .defaultAnswer(CALLS_REAL_METHODS) .name(field.getName())); }
/** * Creates mock with a specified strategy for its answers to interactions. * It's quite an advanced feature and typically you don't need it to write decent tests. * However it can be helpful when working with legacy systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> stub the method call. * * <pre class="code"><code class="java"> * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); * Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); * </code></pre> * * <p>See examples in javadoc for {@link Mockito} class</p> * * @param classToMock class or interface to mock * @param defaultAnswer default answer for unstubbed methods * * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock, Answer defaultAnswer) { return mock(classToMock, withSettings().defaultAnswer(defaultAnswer)); }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
return mock(classToMock, withSettings().defaultAnswer(new AnswerReturnValuesAdapter(returnValues)));
/** * Creates class mock with a specified strategy for its answers to * interactions. It's quite advanced feature and typically you don't need it * to write decent tests. However it can be helpful when working with legacy * systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> * stub the method call. * * <pre> * mockStatic(Foo.class, RETURNS_SMART_NULLS); * mockStatic(Foo.class, new YourOwnAnswer()); * </pre> * * @param classMock * class to mock * @param defaultAnswer * default answer for unstubbed methods */ public static void mockStatic(Class<?> classMock, @SuppressWarnings("rawtypes") Answer defaultAnswer) { mockStatic(classMock, withSettings().defaultAnswer(defaultAnswer)); }
/** * Creates mock with a specified strategy for its answers to interactions. * It's quite advanced feature and typically you don't need it to write * decent tests. However it can be helpful when working with legacy systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> * stub the method call. * * <pre> * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); * Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); * </pre> * * <p> * See examples in javadoc for {@link Mockito} class * </p> * * @param classToMock * class or interface to mock * @param defaultAnswer * default answer for unstubbed methods * * @return mock object */ public static <T> T mock(Class<T> classToMock, @SuppressWarnings("rawtypes") Answer defaultAnswer) { return mock(classToMock, withSettings().defaultAnswer(defaultAnswer)); }
private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata, MockCreationSettings parentMockSettings) { MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ? withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces()) : withSettings(); return propagateSerializationSettings(mockSettings, parentMockSettings) .defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata)); }
private static Object spyNewInstance(Object testInstance, Field field) throws InstantiationException, IllegalAccessException, InvocationTargetException { MockSettings settings = withSettings().defaultAnswer(CALLS_REAL_METHODS) .name(field.getName()); Class<?> type = field.getType();
@Override protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { FieldReader fieldReader = new FieldReader(fieldOwner, field); // TODO refoctor : code duplicated in SpyAnnotationEngine if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) { try { Object instance = fieldReader.read(); if (MockUtil.isMock(instance)) { // A. instance has been spied earlier // B. protect against multiple use of MockitoAnnotations.initMocks() Mockito.reset(instance); } else { Object mock = Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())); setField(fieldOwner, field, mock); } } catch (Exception e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } return false; } }
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
public Object process(Mock annotation, Field field) { MockSettings mockSettings = Mockito.withSettings(); if (annotation.extraInterfaces().length > 0) { // never null mockSettings.extraInterfaces(annotation.extraInterfaces()); } if ("".equals(annotation.name())) { mockSettings.name(field.getName()); } else { mockSettings.name(annotation.name()); } // see @Mock answer default value mockSettings.defaultAnswer(annotation.answer().get()); return Mockito.mock(field.getType(), mockSettings); } }
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
} else { field.setAccessible(true); field.set(testInstance, Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())));
@CheckReturnValue public static <T> T spy(Class<T> classToSpy) { return MOCKITO_CORE.mock(classToSpy, withSettings() .useConstructor() .defaultAnswer(CALLS_REAL_METHODS));
@SuppressWarnings( "unchecked" ) @Override public void dispatch( PendingReferenceCheck<T> reporter ) { reference.dispatch( mock( (Class<PendingReferenceCheck<T>>) reporter.getClass(), withSettings().spiedInstance( reporter ) .defaultAnswer( new ReporterSpy<>( reference, reporter, log ) ) ) ); } }
@Override protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { FieldReader fieldReader = new FieldReader(fieldOwner, field); // TODO refoctor : code duplicated in SpyAnnotationEngine if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) { try { Object instance = fieldReader.read(); if (new MockUtil().isMock(instance)) { // A. instance has been spied earlier // B. protect against multiple use of MockitoAnnotations.initMocks() Mockito.reset(instance); } else { new FieldSetter(fieldOwner, field).set( Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())) ); } } catch (Exception e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } return false; } }
MockSettings mockSettings = withSettings(); Answers answers = mockAnnotation.answer(); if (answers != null) { mockSettings.defaultAnswer(answers.get());