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

added test case to verify first case of mockito#3294:
generics in combintation with inheritance in test and under test classes led to a ClassCastException (java.lang.ClassCastException: class sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to class java.lang.Class (sun.reflect.generics.reflectiveObjects.TypeVariableImpl and java.lang.Class are in module java.base of loader 'bootstrap')
        at org.mockito.internal.configuration.injection.filter.TypeBasedCandidateFilter.isCompatibleTypes(TypeBasedCandidateFilter.java:64))
  • Loading branch information
daniel-matheis-vivavis committed Mar 13, 2024
1 parent 5f1018f commit b175229
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.function.Supplier;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* Verify (part of) regression https://github.com/mockito/mockito/issues/3294 is fixed.
*/
@ExtendWith(MockitoExtension.class)
class InjectionWithGenericsAndInheritanceIssue3294Test extends BaseTest<String> {

@Mock Supplier<String> supplier;

@InjectMocks UnderTest underTest;

@Test
void test() {
assertNotNull(underTest.supplier);
}
}

class BaseTest<T> {

@Mock Supplier<T> supplier;
}

class BaseUnderTest<T> {

Supplier<T> supplier;
}

class UnderTest extends BaseUnderTest<String> {}

0 comments on commit b175229

Please sign in to comment.