From 02b9634b74074dd584386c087d138ec1f5065e91 Mon Sep 17 00:00:00 2001 From: jfrantzius Date: Mon, 17 Apr 2023 12:52:07 +0200 Subject: [PATCH] Fix NPE for handling `Object` (#2976) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2974 Co-authored-by: Jörg von Frantzius <1823661+jfrantzius@users.noreply.github.com> --- .../filter/TypeBasedCandidateFilter.java | 12 ++++++--- .../org/mockitousage/GenericTypeMockTest.java | 27 +++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/mockito/internal/configuration/injection/filter/TypeBasedCandidateFilter.java b/src/main/java/org/mockito/internal/configuration/injection/filter/TypeBasedCandidateFilter.java index a66ab52144..a62ee4053a 100644 --- a/src/main/java/org/mockito/internal/configuration/injection/filter/TypeBasedCandidateFilter.java +++ b/src/main/java/org/mockito/internal/configuration/injection/filter/TypeBasedCandidateFilter.java @@ -77,9 +77,15 @@ protected boolean isCompatibleTypes(Type typeToMock, Type mockType, Field inject private Stream getSuperTypes(Class concreteMockClass) { Stream mockInterfaces = Arrays.stream(concreteMockClass.getGenericInterfaces()); - Stream mockSuperTypes = - Stream.concat(mockInterfaces, Stream.of(concreteMockClass.getGenericSuperclass())); - return mockSuperTypes; + Type genericSuperclass = concreteMockClass.getGenericSuperclass(); + // for java.lang.Object, genericSuperclass is null + if (genericSuperclass != null) { + Stream mockSuperTypes = + Stream.concat(mockInterfaces, Stream.of(genericSuperclass)); + return mockSuperTypes; + } else { + return mockInterfaces; + } } private boolean recurseOnTypeArguments( diff --git a/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java b/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java index 4decb2e2e2..cb3b835c75 100644 --- a/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java +++ b/subprojects/junit-jupiter/src/test/java/org/mockitousage/GenericTypeMockTest.java @@ -8,6 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.mockito.MockitoAnnotations.*; import java.sql.Time; @@ -15,6 +16,7 @@ import java.util.Collection; import java.util.Date; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -345,5 +347,30 @@ public void testMockExists() { } + /** + * Verify regression https://github.com/mockito/mockito/issues/2974 is fixed. + */ + @Nested + public class RegressionNpe { + public abstract class Change {} + public class ChangeCollection implements Iterable { + private List changes = new ArrayList(); + @Override + public Iterator iterator() { + return null; + } + } + + @Mock Change change0; + + @InjectMocks ChangeCollection spiedImpl = new ChangeCollection(); + @Mock(name = "changes") List innerList; + + @Test + public void testNoNpe() { + assertSame(innerList, spiedImpl.changes); + } + + } }