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

Mockito isn't injecting field of generic type #3207

Open
tucamsam opened this issue Dec 19, 2023 · 0 comments
Open

Mockito isn't injecting field of generic type #3207

tucamsam opened this issue Dec 19, 2023 · 0 comments

Comments

@tucamsam
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant