micronaut-core icon indicating copy to clipboard operation
micronaut-core copied to clipboard

Mocking of non-interface beans not working

Open ericathedev opened this issue 5 years ago • 14 comments

The mocking of non-interface beans not working using the latest micronautVersion 1.3.2 and micronaut-test-junit5:1.1.5. Could this be a reoccurance of https://github.com/micronaut-projects/micronaut-test/issues/20 which was fixed in 1.1?

To see the problem in action please look at the example project: https://github.com/ericathedev/test-kt. It uses Micronaut, Kotlin and JUnit5.

Running ClassWithInjectedFileTest#inject mock classToInject returns

expected: <mock> but was: <exampleMethodReturn>
org.opentest4j.AssertionFailedError: expected: <mock> but was: <exampleMethodReturn>

When mocking a class with an interface the test works, see ClassWithInjectedFileTest#inject mock classToInjectWithInterface.

Thank you in advance!

ericathedev avatar Mar 02 '20 11:03 ericathedev

Hello everybody, I can confirm the bug. Using an interface instead of a non-interface bean @MockBean is working.

Regards Simon

simonsilvalauinger avatar Mar 16 '20 11:03 simonsilvalauinger

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 15 '20 12:04 stale[bot]

We have experienced the same. Is there any plan to fix? Thanks

balazsjuhasz-tsc avatar Jul 27 '20 13:07 balazsjuhasz-tsc

micronaut-projects/micronaut-test#20 was in fact incorrectly closed

alvarosanchez avatar Aug 25 '20 17:08 alvarosanchez

Test seems to have passed on the CI?

graemerocher avatar Aug 25 '20 17:08 graemerocher

It didn't run on CI as far as I can tell because I didn't create a PR.

They are running now: https://github.com/micronaut-projects/micronaut-test/pull/264

alvarosanchez avatar Aug 25 '20 18:08 alvarosanchez

Hi, we have found this issue as well, using micronaut core 2.2.3 and micronaut test 2.2.1. Is there any update on a fix please? Thanks

vijamesDD avatar Apr 28 '21 10:04 vijamesDD

what the sample project is missing is:

allOpen {
	annotations("io.micronaut.aop.Around", "javax.inject.Singleton")
}

and mocking worked like this:

@get:MockBean(ClassToInject::class)
val mockClassToInject: ClassToInject = mockk()

would this fix your issue?

jaecktec avatar May 12 '21 15:05 jaecktec

what the sample project is missing is:

allOpen {
	annotations("io.micronaut.aop.Around", "javax.inject.Singleton")
}

and mocking worked like this:

@get:MockBean(ClassToInject::class)
val mockClassToInject: ClassToInject = mockk()

would this fix your issue?

The workaround actually fixed it for me (version 2.3.4)

This should be the default behavior, or at least be mentioned in the documentation

ljoia avatar Jun 17 '21 17:06 ljoia

I can confirm the issue. E.g. the "Mocking Collaborators" example for Kotest only works with the MathService interface and the MathServiceImp implementation. Unfortunately the provided workaround didn't work for me in this case.

sandroboehme avatar Sep 02 '21 10:09 sandroboehme

@sandroboehme

allOpen {
       annotations("jakarta.inject.Singleton")
}

worked for me (jakarta.inject instead of javax.inject, Around not required)

This definitely should be documented!

jacekgajek avatar Jan 26 '22 13:01 jacekgajek

This should also be added to https://micronaut.io/launch

jaecktec avatar Jan 28 '22 07:01 jaecktec

Is there an actual official solution to that? I know I can simply create an interface, but I try to do it for scenarios that are actually needed/justifiable.

ericsnx avatar Jan 15 '23 22:01 ericsnx

Can confirm that it's still an issue in Micronaut 4.4.1 (which I'm using with with Mockk 1.13.10).

I have spent so many hours trying to get certain tests passing and have only just stumbled upon this, which immediately resolved the problem for me.

I think it would save much time and frustration for many people if this were clearly documented somewhere in the Micronaut docs.

eddgrant avatar Apr 29 '24 15:04 eddgrant