Skip to content

Commit

Permalink
In TypeBasedCandidateFilter.isCompatibleTypes -> ClassCastException: …
Browse files Browse the repository at this point in the history
…class TypeVariableImpl cannot be cast to class Class mockito#3294

- just ignore Object typed fields during injection target analysis, because it will almost always lead to unwanted "multiple matches" problem
- moved knowledge about feature toggle GENERICS_SUPPORT_DISABLED_KEY back to TypeBasedCandidateFilter private scope and printing it just once
  • Loading branch information
daniel-matheis-vivavis committed Mar 14, 2024
1 parent b175229 commit 0f7e204
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Expand Up @@ -4,7 +4,6 @@
*/
package org.mockito.internal.configuration.injection;

import static org.mockito.internal.configuration.injection.filter.TypeBasedCandidateFilter.GENERICS_SUPPORT_DISABLED;
import static org.mockito.internal.exceptions.Reporter.cannotInitializeForInjectMocksAnnotation;
import static org.mockito.internal.exceptions.Reporter.fieldInitialisationThrewException;
import static org.mockito.internal.util.collections.Sets.newMockSafeHashSet;
Expand Down Expand Up @@ -72,8 +71,6 @@ public boolean processInjection(
Field injectMocksField, Object injectMocksFieldOwner, Set<Object> mockCandidates) {
FieldInitializationReport report =
initializeInjectMocksField(injectMocksField, injectMocksFieldOwner);
boolean genericsSupportEnabled = System.getProperty(GENERICS_SUPPORT_DISABLED) == null;
System.out.println("v6 genericsSupportEnabled:" + genericsSupportEnabled);
// for each field in the class hierarchy
boolean injectionOccurred = false;
Class<?> fieldClass = report.fieldClass();
Expand Down
Expand Up @@ -24,8 +24,15 @@

public class TypeBasedCandidateFilter implements MockCandidateFilter {

public static final String GENERICS_SUPPORT_DISABLED =
private static final String GENERICS_SUPPORT_DISABLED_KEY =
"mockito.typeBasedCandidateFilter.genericsSupport.disabled";
private static final boolean GENERICS_SUPPORT_ENABLED =
System.getProperty(GENERICS_SUPPORT_DISABLED_KEY) == null;

static {
System.out.println("v6 GENERICS_SUPPORT_ENABLED:" + GENERICS_SUPPORT_ENABLED);
}

private final MockCandidateFilter next;

public TypeBasedCandidateFilter(MockCandidateFilter next) {
Expand Down Expand Up @@ -232,11 +239,10 @@ public OngoingInjector filterCandidate(
final List<Field> allRemainingCandidateFields,
final Object injectee,
final Field injectMocksField) {
boolean genericsSupportEnabled = System.getProperty(GENERICS_SUPPORT_DISABLED) == null;
List<Object> mockTypeMatches = new ArrayList<>();
for (Object mock : mocks) {
if (candidateFieldToBeInjected.getType().isAssignableFrom(mock.getClass())) {
if (genericsSupportEnabled) {
if (GENERICS_SUPPORT_ENABLED) {
Type mockType = MockUtil.getMockSettings(mock).getGenericTypeToMock();
Type typeToMock = candidateFieldToBeInjected.getGenericType();
boolean bothHaveTypeInfo = typeToMock != null && mockType != null;
Expand All @@ -253,6 +259,12 @@ public OngoingInjector filterCandidate(
"v6.5 field is assignable from mock class, but no generic type information is available ");
mockTypeMatches.add(mock);
}
} else if (Object.class.equals(candidateFieldToBeInjected.getType())) {
System.out.println(
"v6.6 Skipping Object typed field during injection analysis because we don't want to have multi matches, field:"
+ candidateFieldToBeInjected
+ ", mock:"
+ mock);
} else {
mockTypeMatches.add(mock);
}
Expand Down

0 comments on commit 0f7e204

Please sign in to comment.