use of org.mockito.internal.util.reflection.BeanPropertySetter 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;
}
Aggregations