Skip to content

Commit

Permalink
Fix NPE for handling Object (#2976)
Browse files Browse the repository at this point in the history
Fixes #2974

Co-authored-by: Jörg von Frantzius <1823661+jfrantzius@users.noreply.github.com>
  • Loading branch information
jfrantzius and jfrantzius committed Apr 17, 2023
1 parent 6ffd23e commit 02b9634
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -77,9 +77,15 @@ protected boolean isCompatibleTypes(Type typeToMock, Type mockType, Field inject

private Stream<Type> getSuperTypes(Class<?> concreteMockClass) {
Stream<Type> mockInterfaces = Arrays.stream(concreteMockClass.getGenericInterfaces());
Stream<Type> 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<Type> mockSuperTypes =
Stream.concat(mockInterfaces, Stream.of(genericSuperclass));
return mockSuperTypes;
} else {
return mockInterfaces;
}
}

private boolean recurseOnTypeArguments(
Expand Down
Expand Up @@ -8,13 +8,15 @@
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;
import java.util.ArrayList;
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;
Expand Down Expand Up @@ -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<TChange extends Change> implements Iterable<TChange> {
private List<TChange> changes = new ArrayList<TChange>();
@Override
public Iterator<TChange> iterator() {
return null;
}
}

@Mock Change change0;

@InjectMocks ChangeCollection spiedImpl = new ChangeCollection();
@Mock(name = "changes") List<Change> innerList;

@Test
public void testNoNpe() {
assertSame(innerList, spiedImpl.changes);
}

}
}

0 comments on commit 02b9634

Please sign in to comment.