本文整理匯總了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;
});
}