org.mockito.Spy Java Examples

The following examples show how to use org.mockito.Spy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockitoAfterTestNGMethod.java    From mockito-cookbook with Apache License 2.0 6 votes vote down vote up
private Set<Object> instanceMocksIn(Object instance, Class<?> clazz) {
    Set<Object> instanceMocks = new HashSet<Object>();
    Field[] declaredFields = clazz.getDeclaredFields();
    for (Field declaredField : declaredFields) {
        if (declaredField.isAnnotationPresent(Mock.class) || declaredField.isAnnotationPresent(Spy.class)) {
            declaredField.setAccessible(true);
            try {
                Object fieldValue = declaredField.get(instance);
                if (fieldValue != null) {
                    instanceMocks.add(fieldValue);
                }
            } catch (IllegalAccessException e) {
                throw new MockitoException("Could not access field " + declaredField.getName());
            }
        }
    }
    return instanceMocks;
}
 
Example #2
Source File: MockitoAfterTestNGMethod.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private Set<Object> instanceMocksIn(Object instance, Class<?> clazz) {
    Set<Object> instanceMocks = new HashSet<Object>();
    Field[] declaredFields = clazz.getDeclaredFields();
    for (Field declaredField : declaredFields) {
        if (declaredField.isAnnotationPresent(Mock.class) || declaredField.isAnnotationPresent(Spy.class)) {
            declaredField.setAccessible(true);
            try {
                Object fieldValue = declaredField.get(instance);
                if (fieldValue != null) {
                    instanceMocks.add(fieldValue);
                }
            } catch (IllegalAccessException e) {
                throw new MockitoException("Could not access field " + declaredField.getName());
            }
        }
    }
    return instanceMocks;
}
 
Example #3
Source File: SpyHelper.java    From COLA with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Set<Object> getOriTargetSet(){
    Set<Object> oriTargetSet = new HashSet<>();
    Set<Field> mockFields = new HashSet<Field>();
    new InjectAnnotationScanner(ownerClazz, Spy.class).addTo(mockFields);
    new InjectAnnotationScanner(ownerClazz, Mock.class).addTo(mockFields);
    if(mockFields.size() == 0){
        return new HashSet<>();
    }
    if(!isSpringContainer()){
        return new HashSet<>();
    }
    for(Field field : mockFields){
        Object oriTarget = getBean(field);
        if(oriTarget == null){
            continue;
        }
        oriTargetSet.add(oriTarget);
    }
    return oriTargetSet;
}
 
Example #4
Source File: MockitoAfterTestNGMethod.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({"deprecation", "unchecked"})
private Collection<Object> instanceMocksOf(Object instance) {
    return Fields.allDeclaredFieldsOf(instance)
                                        .filter(annotatedBy(Mock.class,
                                                            Spy.class,
                                                            MockitoAnnotations.Mock.class))
                                        .notNull()
                                        .assignedValues();
}
 
Example #5
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldFailIfTypeIsInnerClass() throws Exception {
class FailingSpy {
	@Spy private TheInnerClass spyTypeIsInner;
          class TheInnerClass { }
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (MockitoException e) {
          Assertions.assertThat(e.getMessage()).contains("inner class");
      }
  }
 
Example #6
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldFailIfTypeIsAbstract() throws Exception {
class FailingSpy {
	@Spy private AbstractList spyTypeIsAbstract;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("abstract class");
      }
  }
 
Example #7
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldReportWhenConstructorThrows() throws Exception {
class FailingSpy {
       @Spy
          ThrowingConstructor throwingConstructor;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("raised an exception");
      }
  }
 
Example #8
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldReportWhenNoArgConstructor() throws Exception {
class FailingSpy {
       @Spy
          NoValidConstructor noValidConstructor;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("default constructor");
      }
  }
 
Example #9
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldFailIfTypeIsAnInterface() throws Exception {
class FailingSpy {
	@Spy private List spyTypeIsInterface;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("an interface");
      }
  }
 
Example #10
Source File: SpyOnInjectedFieldsHandler.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example #11
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldFailIfTypeIsInnerClass() throws Exception {
class FailingSpy {
	@Spy private TheInnerClass spyTypeIsInner;
          class TheInnerClass { }
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (MockitoException e) {
          Assertions.assertThat(e.getMessage()).contains("inner class");
      }
  }
 
Example #12
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldFailIfTypeIsAbstract() throws Exception {
class FailingSpy {
	@Spy private AbstractList spyTypeIsAbstract;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("abstract class");
      }
  }
 
Example #13
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldReportWhenConstructorThrows() throws Exception {
class FailingSpy {
       @Spy
          ThrowingConstructor throwingConstructor;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("raised an exception");
      }
  }
 
Example #14
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldReportWhenNoArgConstructor() throws Exception {
class FailingSpy {
       @Spy
          NoValidConstructor noValidConstructor;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("default constructor");
      }
  }
 
Example #15
Source File: SpyAnnotationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
  public void shouldFailIfTypeIsAnInterface() throws Exception {
class FailingSpy {
	@Spy private List spyTypeIsInterface;
}

      try {
          MockitoAnnotations.initMocks(new FailingSpy());
          fail();
      } catch (Exception e) {
          Assertions.assertThat(e.getMessage()).contains("an interface");
      }
  }
 
Example #16
Source File: MockitoAfterTestNGMethod.java    From mockito-cookbook with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"deprecation", "unchecked"})
private Collection<Object> instanceMocksOf(Object instance) {
    return Fields.allDeclaredFieldsOf(instance)
                                        .filter(annotatedBy(Mock.class,
                                                            Spy.class,
                                                            MockitoAnnotations.Mock.class))
                                        .notNull()
                                        .assignedValues();
}
 
Example #17
Source File: MockScanner.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private boolean isAnnotatedByMockOrSpy(Field field) {
    return null != field.getAnnotation(Spy.class)
            || null != field.getAnnotation(Mock.class)
            || null != field.getAnnotation(MockitoAnnotations.Mock.class);
}
 
Example #18
Source File: WrongSetOfAnnotationsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Mock @Spy List mock;
    });
}
 
Example #19
Source File: WrongSetOfAnnotationsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
 
Example #20
Source File: WrongSetOfAnnotationsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Spy @Captor ArgumentCaptor captor;
    });
}
 
Example #21
Source File: WrongSetOfAnnotationsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Spy @Captor ArgumentCaptor captor;
    });
}
 
Example #22
Source File: WrongSetOfAnnotationsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
 
Example #23
Source File: WrongSetOfAnnotationsTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Mock @Spy List mock;
    });
}