Search in sources :

Example 1 with BeanPropertySetter

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;
}
Also used : MemberAccessor(org.mockito.plugins.MemberAccessor) BeanPropertySetter(org.mockito.internal.util.reflection.BeanPropertySetter)

Aggregations

BeanPropertySetter (org.mockito.internal.util.reflection.BeanPropertySetter)1 MemberAccessor (org.mockito.plugins.MemberAccessor)1