本文整理汇总了Java中org.mockito.Spy类的典型用法代码示例。如果您正苦于以下问题:Java Spy类的具体用法?Java Spy怎么用?Java Spy使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Spy类属于org.mockito包,在下文中一共展示了Spy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instanceMocksIn
import org.mockito.Spy; //导入依赖的package包/类
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;
}
示例2: configure
import org.mockito.Spy; //导入依赖的package包/类
@Override
protected void configure() {
Set<Field> f = ReflectionUtils.getAllFields(instanceToReadMocksFrom.getClass());
for (Field field : f) {
if (field.getAnnotation(Mock.class) != null || field.getAnnotation(Spy.class) != null) {
try {
field.setAccessible(true);
bindReflectedInstance(field.get(instanceToReadMocksFrom), field.getType());
} catch (Exception e) {
throw new IllegalArgumentException("Unable to bind mock field " + field.getName() + " from "
+ instanceToReadMocksFrom.getClass().getName(), e);
}
}
}
}
示例3: registerMockitoTestClassMocksAndSpies
import org.mockito.Spy; //导入依赖的package包/类
private void registerMockitoTestClassMocksAndSpies(ApplicationContextMock applicationContext) {
new Mirror().on(myTestClass.getClass()).reflectAll().fields().matching(f -> f.isAnnotationPresent(Mock.class) || f.isAnnotationPresent(Spy.class)).forEach(
f -> {
try {
applicationContext.putOrReplaceBean(f.get(myTestClass));
} catch (IllegalAccessException e) {
getLogger().trace(e.getMessage(), e);
}
}
);
}
示例4: resetMocks
import org.mockito.Spy; //导入依赖的package包/类
private void resetMocks(Object instance)
{
Iterable<InstanceField> toReset = Fields.allDeclaredFieldsOf(instance)
.filter(annotatedBy(Mock.class, InjectMocks.class, Spy.class))
.notNull()
.instanceFields();
for (InstanceField field : toReset)
{
field.set(null);
}
}
示例5: instanceMocksOf
import org.mockito.Spy; //导入依赖的package包/类
@SuppressWarnings({"deprecation", "unchecked"})
private Collection<Object> instanceMocksOf(Object instance) {
return Fields.allDeclaredFieldsOf(instance)
.filter(annotatedBy(Mock.class,
Spy.class,
MockitoAnnotations.Mock.class))
.notNull()
.assignedValues();
}
示例6: shouldFailIfTypeIsAnInterface
import org.mockito.Spy; //导入依赖的package包/类
@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");
}
}
示例7: shouldReportWhenNoArgConstructor
import org.mockito.Spy; //导入依赖的package包/类
@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");
}
}
示例8: shouldReportWhenConstructorThrows
import org.mockito.Spy; //导入依赖的package包/类
@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");
}
}
示例9: shouldFailIfTypeIsAbstract
import org.mockito.Spy; //导入依赖的package包/类
@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");
}
}
示例10: shouldFailIfTypeIsInnerClass
import org.mockito.Spy; //导入依赖的package包/类
@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");
}
}
示例11: processInjection
import org.mockito.Spy; //导入依赖的package包/类
@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;
}
示例12: isMockField
import org.mockito.Spy; //导入依赖的package包/类
@Override
protected boolean isMockField(final Field field) {
return hasAnnotation(Spy.class, Mock.class).test(field);
}
示例13: AnnotationResolver
import org.mockito.Spy; //导入依赖的package包/类
public AnnotationResolver(TestClass testClass, Object target) {
this(testClass, target, Inject.class, InjectMocks.class, Mock.class, Spy.class, InjectDelayed.class);
}
示例14: shouldNotAllowMockAndSpy
import org.mockito.Spy; //导入依赖的package包/类
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
MockitoAnnotations.initMocks(new Object() {
@Mock @Spy List mock;
});
}
示例15: shouldNotAllowSpyAndInjectMock
import org.mockito.Spy; //导入依赖的package包/类
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
MockitoAnnotations.initMocks(new Object() {
@InjectMocks @Spy List mock;
});
}