Skip to content

Commit

Permalink
fixed files form Mockito #19
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Mar 7, 2017
1 parent 67f580c commit 32e3595
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private boolean injectMockCandidates(Class<?> awaitingInjectionClazz, Set<Object
private boolean injectMockCandidatesOnFields(Set<Object> mocks, Object instance, boolean injectionOccurred, List<Field> orderedInstanceFields) {
for (Iterator<Field> it = orderedInstanceFields.iterator(); it.hasNext(); ) {
Field field = it.next();
Object injected = mockCandidateFilter.filterCandidate(mocks, field, instance).thenInject();
Object injected = mockCandidateFilter.filterCandidate(mocks, field, orderedInstanceFields, instance).thenInject();
if (injected != null) {
injectionOccurred |= true;
mocks.remove(injected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;

/**
* This node returns an actual injecter which will be either :
Expand All @@ -20,7 +21,7 @@
* </ul>
*/
public class FinalMockCandidateFilter implements MockCandidateFilter {
public OngoingInjecter filterCandidate(final Collection<Object> mocks, final Field field, final Object fieldInstance) {
public OngoingInjecter filterCandidate(final Collection<Object> mocks, final Field field, List<Field> fields, final Object fieldInstance) {
if(mocks.size() == 1) {
final Object matchingMock = mocks.iterator().next();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;

public interface MockCandidateFilter {

OngoingInjecter filterCandidate(
Collection<Object> mocks,
Field fieldToBeInjected,
Object fieldInstance
List<Field> fields, Object instance
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public NameBasedCandidateFilter(MockCandidateFilter next) {
}

public OngoingInjecter filterCandidate(Collection<Object> mocks,
Field field, Object fieldInstance) {
Field field, List<Field> fields, Object fieldInstance) {
List<Object> mockNameMatches = new ArrayList<Object>();
if (mocks.size() > 1) {
for (Object mock : mocks) {
if (field.getName().equals(mockUtil.getMockName(mock).toString())) {
mockNameMatches.add(mock);
}
}
return next.filterCandidate(mockNameMatches, field,
return next.filterCandidate(mockNameMatches, field, fields,
fieldInstance);
/*
* In this case we have to check whether we have conflicting naming
Expand All @@ -39,7 +39,23 @@ public OngoingInjecter filterCandidate(Collection<Object> mocks,
* whenever we find a field that does match its name with the mock
* name, we should take that field instead.
*/
} else if (mocks.size() == 1) {
String mockName = mockUtil.getMockName(mocks.iterator().next())
.toString();

for (Field otherField : fields) {
if (!otherField.equals(field)
&& otherField.getType().equals(field.getType())
&& otherField.getName().equals(mockName)) {

return new OngoingInjecter() {
public Object thenInject() {
return null;
}
};
}
}
}
return next.filterCandidate(mocks, field, fieldInstance);
return next.filterCandidate(mocks, field, fields, fieldInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public TypeBasedCandidateFilter(MockCandidateFilter next) {
this.next = next;
}

public OngoingInjecter filterCandidate(Collection<Object> mocks, Field field, Object fieldInstance) {
public OngoingInjecter filterCandidate(Collection<Object> mocks, Field field, List<Field> fields, Object fieldInstance) {
List<Object> mockTypeMatches = new ArrayList<Object>();
for (Object mock : mocks) {
if (field.getType().isAssignableFrom(mock.getClass())) {
mockTypeMatches.add(mock);
}
}

return next.filterCandidate(mockTypeMatches, field, fieldInstance);
return next.filterCandidate(mockTypeMatches, field, fields, fieldInstance);
}
}

0 comments on commit 32e3595

Please sign in to comment.