JUnit5 Mockito Extension: @InjectMocks seems to discard generic type
See original GitHub issueHello everybody, it seems that the Mockito JUnit 5 extension (@InjectMocks
) does not properly work for generics. Declaring something like the following in a JUnit5 test:
@Mock(name = "mockA")
private GenericObjectContainer<String> mockA;
@Mock(name = "mockB")
private GenericObjectContainer<Integer> mockB;
@InjectMocks
private TestClass myTestObject; // has a constructor setting the distinct fields
leads to only mockA
being injected into myTestObject
which is expected to receive mockA
and mockB
, which makes it impossible to apply distinct when
-mock-behaviour for a whitebox test. A workaround is to define the mocks the old-fashioned way using Mockito.mock(...)
and creating the test object myself via new TestClass(mockA, mockB)
.
I’ve created a project that reproduces the problem: Mockito Junit5 Generics Bug.
Can anyone confirm that this really is a bug?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Different mocks are used for @Mock and @InjectMock when ...
Hi, I have a problem which seems to be very similar to this one #1346 but it is happening when I run test...
Read more >Mockito - Mock generic objects of the same type - Stack Overflow
I am trying to mock my GenericDao objects, using Mockito Framework 1.9. 5 and JUnit 4.11, but Mockito does always mock the first...
Read more >Mockito (Mockito 4.9.0 API) - javadoc.io
@ Captor simplifies creation of ArgumentCaptor - useful when the argument to capture is a nasty generic class and you want to avoid...
Read more >Using Mockito With JUnit 5 | Code With Arho
Mockito will first try to inject mocks by constructor injection, followed by setter injection, or field injection. Pros: Easy to inject mocks.
Read more >Annotated Mockito objects should be initialized
public class FooTest { // Noncompliant: Mockito initialization missing @Mock private Bar bar; @Spy private Baz baz; @InjectMocks private Foo fooUnderTest; ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The link to your project is broken, and I wasn’t able to reproduce it (gist)…
Thanks for the detailed comment @LeMikaelF ! I personally fully understand the MockitoExtension and its related annotations do not need to work as a fully-fledge CDI framework like Spring provides. As the documenation is clear on this and it is still easily possible to create a test object with mocked dependencies, I’m closing this issue. All the best to you - Mockito is really nice! 😃