micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.08k stars 1.07k forks source link

Mocking of non-interface beans not working #3972

Open ericathedev opened 4 years ago

ericathedev commented 4 years ago

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!

simonsilvalauinger commented 4 years ago

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

Regards Simon

stale[bot] commented 4 years ago

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.

balazsjuhasz-tsc commented 4 years ago

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

alvarosanchez commented 4 years ago

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

graemerocher commented 4 years ago

Test seems to have passed on the CI?

alvarosanchez commented 4 years ago

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

vijamesDD commented 3 years ago

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

jaecktec commented 3 years ago

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?

ljoia commented 3 years ago

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

sandroboehme commented 3 years ago

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.

jacekgajek commented 2 years ago

@sandroboehme

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

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

This definitely should be documented!

jaecktec commented 2 years ago

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

ericsnx commented 1 year ago

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.

eddgrant commented 6 months ago

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.