org.mockito.internal.util.reflection.FieldReader Java Examples
The following examples show how to use
org.mockito.internal.util.reflection.FieldReader.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpyHelper.java From COLA with GNU Lesser General Public License v2.1 | 6 votes |
private void processInjectAnnotation(Class clazz){ Set<Field> mockDependentFields = new HashSet<Field>(); new InjectAnnotationScanner(clazz, Inject.class).addTo(mockDependentFields); new InjectAnnotationScanner(clazz, InjectOnlyTest.class).addTo(mockDependentFields); for(Field f : mockDependentFields){ FieldReader fr = new FieldReader(owner, f); Object value = null; if(!fr.isNull()){ value = fr.read(); } boolean isMockOrSpy = isMockOrSpy(value); if(value != null && isMockOrSpy){ Mockito.reset(value); continue; } if(value == null){ value = Mockito.spy(f.getType()); } //spy会新生成对象,导致从容器中脱离 //else{ // value = Mockito.spy(value); //} new FieldSetter(owner, f).set(value); } }
Example #2
Source File: AnnotationFragmentManager.java From AndroidUnitTest with Apache License 2.0 | 6 votes |
public void addToActivity(@NonNull Object target, Fragment fragment) { String tag = null; for (Field fragmentField : fragmentFields) { Fragment fragmentOfField = (Fragment) new FieldReader(target, fragmentField).read(); if (fragment == fragmentOfField) { RFragment fragmentAnnotation = fragmentField.getAnnotation(RFragment.class); tag = fragmentAnnotation.tag(); } } if (tag == null || tag.isEmpty()) { tag = fragment.getClass().toString(); } ControllerActivity controllerActivity = getAndroidUnitTest().activity(); //if no activity is create, the default activity manager behaviour is to create one if (controllerActivity.get() == null) { controllerActivity.createAndInitActivity(FragmentActivity.class, null); controllerActivity.setActivityState(ActivityState.CREATED); } AnnotationFragmentManager.addToActivity(getActivity(), fragment, tag); }
Example #3
Source File: SpyOnInjectedFieldsHandler.java From astor with GNU General Public License v2.0 | 5 votes |
@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; }
Example #4
Source File: MockScanner.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Scan and prepare mocks for the given <code>testClassInstance</code> and <code>clazz</code> in the type hierarchy. * * @return A prepared set of mock */ private Set<Object> scan() { Set<Object> mocks = newMockSafeHashSet(); for (Field field : clazz.getDeclaredFields()) { // mock or spies only FieldReader fieldReader = new FieldReader(instance, field); Object mockInstance = preparedMock(fieldReader.read(), field); if (mockInstance != null) { mocks.add(mockInstance); } } return mocks; }