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.

`@InjectMockKs` can't be used with constructor injection

See original GitHub issue

Hi. Not sure whether to report this as a bug or as a feature request.

I did some searching to see if this issue has already been discussed or raised somewhere, but I couldn’t find anything.

Take the following example test class:

@ExtendWith(MockKExtension::class)
internal class SubjectTest(
    @MockK private val xServiceMock: XService,
    @MockK private val yServiceMock: YService,
) {
    @InjectMockKs
    private lateinit var subject: Subject

    // Test functions here.
}

To avoid having to use the Keyword of Shame, I prefer to use constructor injection for the Class Under Test (Subject) as well, so I try to rewrite it as follows:

@ExtendWith(MockKExtension::class)
internal class SubjectTest(
    @MockK private val xServiceMock: XService,
    @MockK private val yServiceMock: YService,
    @InjectMockKs private val subject: Subject,
) {
    // Test functions here.
}

I would expect this to work the same, but unfortunately this results in the following error:

No ParameterResolver registered for parameter [subject] in constructor...

I’ve tried this with MockK versions 1.12.0 and 1.12.1, and it fails the same way in both.

It would be nice if @InjectMockKs would work through constuctor injection as well. In my opinion, lateinit var is ugly, and I prefer not to use it unless absolutely necessary.

Is there a specific reason why this doesn’t work, like with the @MockK annotations? Or is this a bug?

Thanks.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

1reaction
Raibazcommented, Nov 29, 2021

This is indeed an unsupported behavior; MockKExtension does not take into account parameters annotated with @InjectMocks.

It should be possible, though, to handle them in the resolveParameters method and inject the mocks; not sure if parameter order is going to be relevant here (it likely is), so we may need to force the @InjectMocks parameter to be the last one, but it shouldn’t be a big deal.

0reactions
Raibazcommented, Nov 2, 2022

Yes please, that would be amazing! 🙏

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mockito injection not working for constructor AND setter mocks ...
Constructor injection will NOT be used if the type of one or more of the parameters of the CHOSEN constructor is a primitive...
Read more >
MockK | mocking library for Kotlin
Provides DSL to mock behavior. Built from zero to fit Kotlin language. Supports named parameters, object mocks, coroutines and extension function mocking.
Read more >
Mockito: Why You Should Not Use InjectMocks Annotation to ...
Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in order and as described below.
Read more >
MockK: A Mocking Library for Kotlin - Baeldung
In the above example, we've used the @InjectMockKs annotation. This specifies an object where defined mocks should be injected.
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.
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