@Override public boolean isMock(){ return MockUtil.isMock(toInspect); }
/** * Find out whether the object is a mock. * @return true if the object is a mock or a spy. */ public boolean isMock(){ return delegate.isMock( toInspect ); }
public boolean isSpy(Object mock) { return mock instanceof MockitoSpy && isMock(mock); }
private boolean isMockOrSpy(Object instance) { return MockUtil.isMock(instance) || MockUtil.isSpy(instance); } }
public Object answer(InvocationOnMock invocation) throws Throwable { if (throwable == null) { throw new IllegalStateException("throwable is null: " + "you shall not call #answer if #validateFor fails!"); } if (MockUtil.isMock(throwable)) { throw throwable; } Throwable t = throwable.fillInStackTrace(); if (t == null) { //Custom exceptions sometimes return null, see #866 throw throwable; } filter.filter(t); throw t; }
private boolean isMockOrSpy(Object instance) { return mockUtil.isMock(instance) || mockUtil.isSpy(instance); } }
public Object answer(InvocationOnMock invocation) throws Throwable { if (new MockUtil().isMock(throwable)) { throw throwable; } Throwable t = throwable.fillInStackTrace(); filter.filter(t); throw t; }
@Override public String toString() { return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (MockUtil.isMock(mockInstance) ? MockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
public static boolean isSpy(Object mock) { return isMock(mock) && getMockSettings(mock).getDefaultAnswer() == Mockito.CALLS_REAL_METHODS; }
public static <T> MockHandler<T> getMockHandler(T mock) { if (mock == null) { throw new NotAMockException("Argument should be a mock, but is null!"); } if (isMock(mock)) { return mockMaker.getHandler(mock); } else { throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass()); } }
@Override public void process(Class<?> context, Object testInstance) { Field[] fields = context.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) { assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, Captor.class); field.setAccessible(true); Object instance; try { instance = field.get(testInstance); if (MockUtil.isMock(instance)) { // instance has been spied earlier // for example happens when MockitoAnnotations.initMocks is called two times. Mockito.reset(instance); } else if (instance != null) { field.set(testInstance, spyInstance(field, instance)); } else { field.set(testInstance, spyNewInstance(testInstance, field)); } } catch (Exception e) { throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e); } } } }
public <T> T verify(T mock, VerificationMode mode) { if (mock == null) { reporter.nullPassedToVerify(); } else if (!mockUtil.isMock(mock)) { reporter.notAMockPassedToVerify(mock.getClass()); } mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, mode)); return mock; }
@Override public String toString() { MockUtil mockUtil = new MockUtil(); return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (mockUtil.isMock(mockInstance) ? mockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
public InOrder inOrder(Object... mocks) { if (mocks == null || mocks.length == 0) { reporter.mocksHaveToBePassedWhenCreatingInOrder(); } for (Object mock : mocks) { if (mock == null) { reporter.nullPassedWhenCreatingInOrder(); } else if (!mockUtil.isMock(mock)) { reporter.notAMockPassedWhenCreatingInOrder(); } } return new InOrderImpl(Arrays.asList(mocks)); }
@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; } }
@Override public <T> T when(T mock) { if (mock == null) { throw nullPassedToWhenMethod(); } if (!isMock(mock)) { throw notAMockPassedToWhenMethod(); } MockUtil.getInvocationContainer(mock).setAnswersForStubbing(answers, strictness); return mock; }
public InOrder inOrder(Object... mocks) { if (mocks == null || mocks.length == 0) { throw mocksHaveToBePassedWhenCreatingInOrder(); } for (Object mock : mocks) { if (mock == null) { throw nullPassedWhenCreatingInOrder(); } if (!isMock(mock)) { throw notAMockPassedWhenCreatingInOrder(); } assertNotStubOnlyMock(mock); } return new InOrderImpl(Arrays.asList(mocks)); }
private Connection spy(Connection connection) { if ( MockUtil.isMock( connection ) ) { return connection; } Connection connectionSpy = Mockito.spy( connection ); try { Mockito.doAnswer( invocation -> { PreparedStatement statement = (PreparedStatement) invocation.callRealMethod(); PreparedStatement statementSpy = Mockito.spy( statement ); String sql = (String) invocation.getArguments()[0]; preparedStatementMap.put( statementSpy, sql ); return statementSpy; } ).when( connectionSpy ).prepareStatement( ArgumentMatchers.anyString() ); Mockito.doAnswer( invocation -> { Statement statement = (Statement) invocation.callRealMethod(); Statement statementSpy = Mockito.spy( statement ); return statementSpy; } ).when( connectionSpy ).createStatement(); } catch ( SQLException e ) { throw new IllegalArgumentException( e ); } return connectionSpy; }
@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; } }
public <T> T when(T mock) { MockUtil mockUtil = new MockUtil(); if (mock == null) { reporter.nullPassedToWhenMethod(); } else { if (!mockUtil.isMock(mock)) { reporter.notAMockPassedToWhenMethod(); } } mockUtil.getMockHandler(mock).setAnswersForStubbing(answers); return mock; }