use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class InlineDelegateByteBuddyMockMaker method newInstance.
@Override
@SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> cls) throws InstantiationException {
Constructor<?>[] constructors = cls.getDeclaredConstructors();
if (constructors.length == 0) {
throw new InstantiationException(cls.getName() + " does not define a constructor");
}
Constructor<?> selected = constructors[0];
for (Constructor<?> constructor : constructors) {
if (Modifier.isPublic(constructor.getModifiers())) {
selected = constructor;
break;
}
}
Class<?>[] types = selected.getParameterTypes();
Object[] arguments = new Object[types.length];
int index = 0;
for (Class<?> type : types) {
arguments[index++] = makeStandardArgument(type);
}
MemberAccessor accessor = Plugins.getMemberAccessor();
try {
return (T) accessor.newInstance(selected, callback -> {
mockitoConstruction.set(true);
try {
return callback.newInstance();
} finally {
mockitoConstruction.set(false);
}
}, arguments);
} catch (Exception e) {
throw new InstantiationException("Could not instantiate " + cls.getName(), e);
}
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class IndependentAnnotationEngine method process.
@Override
public AutoCloseable process(Class<?> clazz, Object testInstance) {
List<ScopedMock> scopedMocks = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
boolean alreadyAssigned = false;
for (Annotation annotation : field.getAnnotations()) {
Object mock = createMockFor(annotation, field);
if (mock instanceof ScopedMock) {
scopedMocks.add((ScopedMock) mock);
}
if (mock != null) {
throwIfAlreadyAssigned(field, alreadyAssigned);
alreadyAssigned = true;
final MemberAccessor accessor = Plugins.getMemberAccessor();
try {
accessor.set(field, testInstance, mock);
} catch (Exception e) {
for (ScopedMock scopedMock : scopedMocks) {
scopedMock.close();
}
throw new MockitoException("Problems setting field " + field.getName() + " annotated with " + annotation, e);
}
}
}
}
return () -> {
for (ScopedMock scopedMock : scopedMocks) {
scopedMock.closeOnDemand();
}
};
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class SpyAnnotationEngine method spyNewInstance.
private static Object spyNewInstance(Object testInstance, Field field) throws InstantiationException, IllegalAccessException, InvocationTargetException {
MockSettings settings = withSettings().defaultAnswer(CALLS_REAL_METHODS).name(field.getName());
Class<?> type = field.getType();
if (type.isInterface()) {
return Mockito.mock(type, settings.useConstructor());
}
int modifiers = type.getModifiers();
if (typeIsPrivateAbstractInnerClass(type, modifiers)) {
throw new MockitoException(join("@Spy annotation can't initialize private abstract inner classes.", " inner class: '" + type.getSimpleName() + "'", " outer class: '" + type.getEnclosingClass().getSimpleName() + "'", "", "You should augment the visibility of this inner class"));
}
if (typeIsNonStaticInnerClass(type, modifiers)) {
Class<?> enclosing = type.getEnclosingClass();
if (!enclosing.isInstance(testInstance)) {
throw new MockitoException(join("@Spy annotation can only initialize inner classes declared in the test.", " inner class: '" + type.getSimpleName() + "'", " outer class: '" + enclosing.getSimpleName() + "'", ""));
}
return Mockito.mock(type, settings.useConstructor().outerInstance(testInstance));
}
Constructor<?> constructor = noArgConstructorOf(type);
if (Modifier.isPrivate(constructor.getModifiers())) {
MemberAccessor accessor = Plugins.getMemberAccessor();
return Mockito.mock(type, settings.spiedInstance(accessor.newInstance(constructor)));
} else {
return Mockito.mock(type, settings.useConstructor());
}
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class TerminalMockCandidateFilter method filterCandidate.
@Override
public OngoingInjector filterCandidate(final Collection<Object> mocks, final Field candidateFieldToBeInjected, final List<Field> allRemainingCandidateFields, final Object injectee) {
if (mocks.size() == 1) {
final Object matchingMock = mocks.iterator().next();
MemberAccessor accessor = Plugins.getMemberAccessor();
return () -> {
try {
if (!new BeanPropertySetter(injectee, candidateFieldToBeInjected).set(matchingMock)) {
accessor.set(candidateFieldToBeInjected, injectee, matchingMock);
}
} catch (RuntimeException | IllegalAccessException e) {
throw cannotInjectDependency(candidateFieldToBeInjected, matchingMock, e);
}
return matchingMock;
};
}
return OngoingInjector.nop;
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class BeanPropertySetter method set.
/**
* Set the value to the property represented by this {@link BeanPropertySetter}
* @param value the new value to pass to the property setter
* @return <code>true</code> if the value has been injected, <code>false</code> otherwise
* @throws RuntimeException Can be thrown if the setter threw an exception, if the setter is not accessible
* or, if <code>reportNoSetterFound</code> and setter could not be found.
*/
public boolean set(final Object value) {
MemberAccessor accessor = Plugins.getMemberAccessor();
Method writeMethod = null;
try {
writeMethod = target.getClass().getMethod(setterName(field.getName()), field.getType());
accessor.invoke(writeMethod, target, value);
return true;
} catch (InvocationTargetException e) {
throw new RuntimeException("Setter '" + writeMethod + "' of '" + target + "' with value '" + value + "' threw exception : '" + e.getTargetException() + "'", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Access not authorized on field '" + field + "' of object '" + target + "' with value: '" + value + "'", e);
} catch (NoSuchMethodException e) {
reportNoSetterFound();
}
reportNoSetterFound();
return false;
}
Aggregations