當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。