You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm performing an upgrade of our codebase (Java 11 to 17, Spring upgrade, Hibernate upgrade, etc), as well as some test code, including an upgrade from Mockito 1.10 to 5.8.0. Unfortunately, I'm seeing NullPointerExceptions on several of our tests.
I've managed to boil it down to the following two tests that I inserted into the GenericTypeMockTest in a Mockito repo that I pulled down.
@Nested
public class ClassWithGenericFieldsTest {
public class UnderTestWithGenericField<T> {
T toBeInjected;
}
public class ConcreteClass {}
@Mock ConcreteClass injectedMock;
@InjectMocks UnderTestWithGenericField<ConcreteClass> underTestWithGenericField = new UnderTestWithGenericField<ConcreteClass>();
@Test
void testWithGenericFields() {
assertNotNull(injectedMock);
// verify that we can match the type parameters of the class under test
assertEquals(injectedMock, underTestWithGenericField.toBeInjected);
}
}
and
@Nested
public class SuperClassWithGenericFieldsTest {
public class SuperWithGenericField<T> {
List<T> listToBeInjected;
T instanceToBeInjected;
}
public class ChildClass extends SuperWithGenericField<ConcreteClass> {}
public class ConcreteClass {}
@Mock List<ConcreteClass> injectedList;
@Mock ConcreteClass injectedInstance;
@InjectMocks ChildClass underTestWithGenericField = new ChildClass();
@Test
void testWithGenericFields() {
assertNotNull(injectedList);
assertNotNull(injectedInstance);
// verify that we can match the type parameters of the class under test
assertEquals(injectedList, underTestWithGenericField.listToBeInjected);
assertEquals(injectedInstance, underTestWithGenericField.instanceToBeInjected);
}
}
While the first test does successfully inject the mock if the type of the mock is instead something like List<T> rather than just T, that's not reflective of my codebase. Additionally, the second, as illustrated, fails in both cases.
This pattern is rather prevalent through the codebase I'm working on, so I'd prefer not to have to rewrite significant portions of it.
The text was updated successfully, but these errors were encountered:
I'm performing an upgrade of our codebase (Java 11 to 17, Spring upgrade, Hibernate upgrade, etc), as well as some test code, including an upgrade from Mockito 1.10 to 5.8.0. Unfortunately, I'm seeing NullPointerExceptions on several of our tests.
I've managed to boil it down to the following two tests that I inserted into the
GenericTypeMockTest
in a Mockito repo that I pulled down.and
While the first test does successfully inject the mock if the type of the mock is instead something like
List<T>
rather than justT
, that's not reflective of my codebase. Additionally, the second, as illustrated, fails in both cases.This pattern is rather prevalent through the codebase I'm working on, so I'd prefer not to have to rewrite significant portions of it.
The text was updated successfully, but these errors were encountered: