Java Code Examples for org.mockito.Spy

The following examples show how to use org.mockito.Spy. These examples are extracted from open source projects. 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 Project: COLA   Source File: SpyHelper.java    License: 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 2
Source Project: mockito-cookbook   Source File: MockitoAfterTestNGMethod.java    License: 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 3
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 4
Source Project: mockito-cookbook   Source File: MockitoAfterTestNGMethod.java    License: 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 5
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 6
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 7
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 Project: astor   Source File: SpyAnnotationTest.java    License: 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 9
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 10
@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 11
@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 12
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 13
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 14
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 15
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 16
Source Project: astor   Source File: SpyAnnotationTest.java    License: 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 17
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Mock @Spy List mock;
    });
}
 
Example 18
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
 
Example 19
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Spy @Captor ArgumentCaptor captor;
    });
}
 
Example 20
Source Project: astor   Source File: MockScanner.java    License: 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 21
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Mock @Spy List mock;
    });
}
 
Example 22
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
 
Example 23
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Spy @Captor ArgumentCaptor captor;
    });
}