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.

@InjectMock replaces existing mocks

See original GitHub issue

Describe the bug Basically what I have is class C1, which implements 2 interfaces I1 and I2. Class C2 then injects those 2 interfaces. The actual (non-test) code runs well, but in testing one of the mocks is not called.

It will probably be the simplest to understand with some pseudo code, as I have a hard time explaining the problem with words.

class C1 implements I1, I2 {...}

class C2 {
  @Inject I1
  @Inject I2

  method() {
    I1.m1()
    I2.m2()
  }
}

@QuarkusTest
class Test {
  @Inject C2
  @MockInject I1
  @MockInject I2

  @Test
  test() {
    when(I1)...
    when(I2)...

    C2.method()

    verify(I1) - NOT CALLED
    verify(I2) - CALLED
  }
}

From what I understand what happens is that the mocks replace the actual implementation, but that does not seem correct to me, because the implementation is not what I really want to mock or worry about.

Now… Does this qualify as a bug, or is it a “feature”? Is there some workaround except just creating interfaces that are the “same” as implementations (one interface per class)?

Expected behavior Both mock get called.

Actual behavior One mock gets called.

** To reproduce **

git clone https://github.com/blazmrak/inject-mock-issue.git mvn clean test

Environment (please complete the following information):

  • Output of uname -a or ver: 10.0.19042.804
  • Output of java -version: 11.0.10
  • GraalVM version (if different from Java):
  • Quarkus version or git rev: 1.12
  • Build tool (ie. output of mvnw --version or gradlew --version): 3.6.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
geoandcommented, Mar 2, 2021

I’ll take a look sometime this week.

Thanks for the reproducer and the details

2reactions
blazmrakcommented, Mar 2, 2021

Sorry, I didn’t bother to setup a repo, as I didn’t know if it is a problem at all. I will get on it and update the issue.

@mkouba The scope is @ApplicationScoped.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does @InjectMock replace the bean with a mock in all other ...
I guess you have a typo and there should be @InjectMocks . For your question: does it mean, that in all cases other...
Read more >
InjectMocks injects copies @Mock objects? #1156 - GitHub
I just replaced PowerMock's JUnit test runner with MockitoJUnitRunner ... When using @InjectMocks the injected mocks appear to be different o.
Read more >
Mockito: Why You Should Not Use InjectMocks Annotation to ...
InjectMocks annotation can be seen as an equivalent of Spring's own dependency ... and should be replaced with constructor injection.
Read more >
Mockito @InjectMocks - Mocks Dependency Injection
Mockito @InjectMocks annotations allow us to inject mocked dependencies in the annotated class mocked object. This is useful when we have ...
Read more >
Mockito (Mockito 4.9.0 API) - javadoc.io
This javadoc content is also available on the https://site.mockito.org/ web ... InjectMocks - injects mock or spy fields into tested object automatically.
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