Skip to content

Commit

Permalink
mockito#2921 make sure all codepaths can be covered in test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jörg von Frantzius committed Mar 8, 2023
1 parent 3ac47be commit 40a2fe6
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public TypeBasedCandidateFilter(MockCandidateFilter next) {
}

protected boolean isCompatibleTypes(Type typeToMock, Type mockType, Field injectMocksField) {
boolean result = false;
if (typeToMock instanceof ParameterizedType && mockType instanceof ParameterizedType) {
// ParameterizedType.equals() is documented as:
// "Instances of classes that implement this interface must implement
Expand All @@ -34,29 +35,27 @@ protected boolean isCompatibleTypes(Type typeToMock, Type mockType, Field inject
// and e.g. Set doesn't equal TreeSet, so roll our own comparison if
// ParameterizedTypeImpl.equals() returns false
if (typeToMock.equals(mockType)) {
return true;
result = true;
} else {
ParameterizedType genericTypeToMock = (ParameterizedType) typeToMock;
ParameterizedType genericMockType = (ParameterizedType) mockType;
Type[] actualTypeArguments = genericTypeToMock.getActualTypeArguments();
Type[] actualTypeArguments2 = genericMockType.getActualTypeArguments();
// Recurse on type parameters, so we properly test whether e.g. Wildcard bounds
// have a match
return recurseOnTypeArguments(
result = recurseOnTypeArguments(
injectMocksField, actualTypeArguments, actualTypeArguments2);
}
} else if (typeToMock instanceof WildcardType) {
WildcardType wildcardTypeToMock = (WildcardType) typeToMock;
Type[] upperBounds = wildcardTypeToMock.getUpperBounds();
return Arrays.stream(upperBounds)
result = Arrays.stream(upperBounds)
.anyMatch(t -> isCompatibleTypes(t, mockType, injectMocksField));
} else if (typeToMock instanceof Class && mockType instanceof Class) {
return ((Class) typeToMock).isAssignableFrom((Class) mockType);
result = ((Class) typeToMock).isAssignableFrom((Class) mockType);
} // no need to check for GenericArrayType, as Mockito cannot mock this anyway

// can only happen if there is a new subclass of Type that we don't know about yet,
// must have return statement here to be able to compile
return false;
return result;
}

private boolean recurseOnTypeArguments(
Expand Down

0 comments on commit 40a2fe6

Please sign in to comment.