Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix 2974 fix npe #2976

Merged
merged 3 commits into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
}

}
}