Different mocks are used for @Mock and @InjectMock in the same test class with JUnit 5 extension
See original GitHub issueI’ve been playing with the new JUnit 5 support in Mockito 2.17.0. I have found that with @Mock
and @InjectMocks
used in the same test class different mocks are injected. I would expect to have the same mock reused (as it works with JUnit 4 or manual @Mock
initialization).
Sample code:
@ExtendWith(MockitoExtension.class)
public class SpaceShip5BugMiniTest {
@InjectMocks
private SpaceShip spaceShip;
@Mock
private TacticalStation tacticalStation;
@Test
public void shouldInjectMocks() {
assertThat(tacticalStation).isNotNull();
assertThat(spaceShip.getTacticalStation()).isEqualTo(tacticalStation);
//Bug ↑ - different mocks are use in @Mock and @InjectMock
}
}
Error message:
org.opentest4j.AssertionFailedError:
Expecting:
<"tacticalStation (TacticalStation$MockitoMock$991606095@61d6015a)">
to be equal to:
<"tacticalStation (TacticalStation$MockitoMock$991606095@b62d79)">
but was not.
Expected :tacticalStation
Actual :tacticalStation
The minimal (non-)working example is available here.
@TimvdLippe Do you have an idea what could be wrong?
Mockito 2.17.0 (2.17.1 is not being released due to (https://github.com/mockito/shipkit/issues/679), junit-jupiter 5.1.0, OpenJDK 1.8.0_161, Linux.
Checklist:
- The mockito message in the stacktrace have useful information, but it didn’t help
- The problematic code (if that’s possible) is copied here; Note that some configuration are impossible to mock via Mockito
- Provide versions (mockito / jdk / os / any other relevant information)
- Provide a Short, Self Contained, Correct (Compilable), Example of the issue (same as any question on stackoverflow.com)
- Read the contributing guide
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (10 by maintainers)
Top Results From Across the Web
Different mocks are used for @Mock and @InjectMock in the ...
Different mocks are used for @Mock and @InjectMock in the same test class with JUnit 5 extension when using SpringExtension.class along with ...
Read more >Mockito and JUnit 5 - Using ExtendWith - Baeldung
Learn how to automatically inject mocks into Mockito tests using the JUnit 5 extension model.
Read more >Using Mockito With JUnit 5 | Code With Arho
We will learn a test framework independent way, and how to use the Mockito JUnit 5 extension. This article is part of the...
Read more >Mockito @InjectMocks - Mocks Dependency Injection
If there is more than one mocked object of the same class, then mock object name is used to inject the dependencies. Mock...
Read more >JUnit 5 tutorial, part 1: Unit testing with JUnit 5, Mockito, and ...
In addition to the groups property, JUnit 5 allows you to use an ... class ServiceTest { @Mock Repository repository; @InjectMocks Service ...
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
It works flawlessly. Thanks @SeriyBg!
@TimvdLippe As this bug can impact all those people preferring a constructor injection, what do you thing about releasing the new minor version (e.g. after #1350 is hopefully merged 😃 ) to Maven Central?
Yes I am also facing this issue. Mine is also exactly the same scenario. Below are my dependencies, org.junit.jupiter:junit-jupiter-engine:5.6.0 org.springframework.boot:spring-boot-starter:2.2.6.RELEASE org.springframework.boot:spring-boot-starter-test:2.2.6.RELEASE org.mockito:mockito-junit-jupiter:3.1.0