question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JUnit5 Mockito Extension: @InjectMocks seems to discard generic type

See original GitHub issue

Hello 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LeMikaelFcommented, Aug 29, 2021

The link to your project is broken, and I wasn’t able to reproduce it (gist)…

0reactions
SaltySunlightcommented, Aug 31, 2021

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! 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found