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.

Different mocks are used for @Mock and @InjectMock in the same test class with JUnit 5 extension

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
szpakcommented, Mar 27, 2018

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?

0reactions
karthikairamcommented, Apr 18, 2020

I’m seeing this same bug when using SpringExtension.class along with MockitoExtension.class:

@ExtendWith({MockitoExtension.class, SpringExtension.class})
public class InjectMocksTest {
  static class TacticalStation {}

  static class SpaceShip {
    private final TacticalStation tacticalStation;

    public SpaceShip(final TacticalStation tacticalStation) {
      this.tacticalStation = tacticalStation;
    }

    public TacticalStation getTacticalStation() {
      return tacticalStation;
    }
  }

  @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
  }
}

This is using Mockito 3.1.0, JUnit 5.6.0, and Spring Boot 5.3.0.RELEASE.

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

Read more comments on GitHub >

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

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