当前位置: 首页>>代码示例>>Java>>正文


Java Spy类代码示例

本文整理汇总了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;
}
 
开发者ID:marcingrzejszczak,项目名称:mockito-cookbook,代码行数:19,代码来源:MockitoAfterTestNGMethod.java

示例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);
			}
		}
	}
}
 
开发者ID:Mercateo,项目名称:rest-jersey-utils,代码行数:16,代码来源:TestBinder.java

示例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);
                }
            }
    );
}
 
开发者ID:opensingular,项目名称:singular-server,代码行数:12,代码来源:SingularServerSpringMockitoTestConfig.java

示例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);
    }
}
 
开发者ID:edgehosting,项目名称:jira-dvcs-connector,代码行数:12,代码来源:MockitoTestNgListener.java

示例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();
}
 
开发者ID:marcingrzejszczak,项目名称:mockito-cookbook,代码行数:10,代码来源:MockitoAfterTestNGMethod.java

示例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");
      }
  }
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:14,代码来源:SpyAnnotationTest.java

示例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");
      }
  }
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:15,代码来源:SpyAnnotationTest.java

示例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");
      }
  }
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:15,代码来源:SpyAnnotationTest.java

示例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");
      }
  }
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:14,代码来源:SpyAnnotationTest.java

示例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");
      }
  }
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:15,代码来源:SpyAnnotationTest.java

示例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;
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:28,代码来源:SpyOnInjectedFieldsHandler.java

示例12: isMockField

import org.mockito.Spy; //导入依赖的package包/类
@Override
protected boolean isMockField(final Field field) {
    return hasAnnotation(Spy.class, Mock.class).test(field);
}
 
开发者ID:dajudge,项目名称:testee.fi,代码行数:5,代码来源:MockitoMockContributor.java

示例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);
}
 
开发者ID:ljacqu,项目名称:DependencyInjector,代码行数:4,代码来源:AnnotationResolver.java

示例14: shouldNotAllowMockAndSpy

import org.mockito.Spy; //导入依赖的package包/类
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @Mock @Spy List mock;
    });
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:7,代码来源:WrongSetOfAnnotationsTest.java

示例15: shouldNotAllowSpyAndInjectMock

import org.mockito.Spy; //导入依赖的package包/类
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
    MockitoAnnotations.initMocks(new Object() {
        @InjectMocks @Spy List mock;
    });
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:7,代码来源:WrongSetOfAnnotationsTest.java


注:本文中的org.mockito.Spy类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。