项目:mockito-cookbook
文件:MockitoAfterTestNGMethod.java
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;
}
项目:astor
文件:MockitoAfterTestNGMethod.java
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;
}
项目:rest-jersey-utils
文件:TestBinder.java
@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);
}
}
}
}
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);
}
}
);
}
项目:jira-dvcs-connector
文件:MockitoTestNgListener.java
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);
}
}
项目:mockito-cookbook
文件:MockitoAfterTestNGMethod.java
@SuppressWarnings({"deprecation","unchecked"})
private Collection<Object> instanceMocksOf(Object instance) {
return Fields.allDeclaredFieldsOf(instance)
.filter(annotatedBy(Mock.class,Spy.class,MockitoAnnotations.Mock.class))
.notNull()
.assignedValues();
}
项目:astor
文件:SpyAnnotationTest.java
@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");
}
}
项目:astor
文件:SpyAnnotationTest.java
@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");
}
}
项目:astor
文件:SpyAnnotationTest.java
@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");
}
}
项目:astor
文件:SpyAnnotationTest.java
@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");
}
}
项目:astor
文件:SpyAnnotationTest.java
@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");
}
}
项目:astor
文件:MockitoAfterTestNGMethod.java
@SuppressWarnings({"deprecation",MockitoAnnotations.Mock.class))
.notNull()
.assignedValues();
}
项目:astor
文件:SpyOnInjectedFieldsHandler.java
@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;
}
项目:testee.fi
文件:MockitoMockContributor.java
@Override
protected boolean isMockField(final Field field) {
return hasAnnotation(Spy.class,Mock.class).test(field);
}
项目:DependencyInjector
文件:AnnotationResolver.java
public AnnotationResolver(TestClass testClass,Object target) {
this(testClass,target,Inject.class,Mock.class,InjectDelayed.class);
}
@Test(expected=MockitoException.class)
public void shouldNotAllowMockAndSpy() throws Exception {
MockitoAnnotations.initMocks(new Object() {
@Mock @Spy List mock;
});
}
@Test(expected=MockitoException.class)
public void shouldNotAllowSpyAndInjectMock() throws Exception {
MockitoAnnotations.initMocks(new Object() {
@InjectMocks @Spy List mock;
});
}
@Test(expected=MockitoException.class)
public void shouldNotAllowCaptorAndSpy() throws Exception {
MockitoAnnotations.initMocks(new Object() {
@Spy @Captor ArgumentCaptor captor;
});
}
项目:astor
文件:MockScanner.java
private boolean isAnnotatedByMockOrSpy(Field field) {
return null != field.getAnnotation(Spy.class)
|| null != field.getAnnotation(Mock.class)
|| null != field.getAnnotation(MockitoAnnotations.Mock.class);
}
项目:test-injector
文件:Preconditions.java
public static void checkPreconditions(Object test) {
checkThatFieldsAnnotatedWithAreNotNull(test,Bind.class);
checkThatFieldsAnnotatedWithAreNotNull(test,Spy.class);
checkThatFieldsAnnotatedWithMockAreNull(test);
}
项目:test-injector
文件:FieldBindingsCollector.java
public void collectBindings(Object test) {
collectBindings(test,Mock.class);
collectBindings(test,Spy.class);
collectBindings(test,Bind.class);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。