@MockBean doesn't work with @Context beans
Steps to Reproduce
- Annotate a bean as
@Context - Use it in a test
- Try to mock the bean with
@MockBean
Expected Behaviour
The @MockBean method would be invoked and the mocked bean injected in the test and its other beans.
Actual Behaviour
The actual bean is instantiated and the test fails when trying to mock its behaviour with org.mockito.exceptions.misusing.MissingMethodInvocationException.
Environment Information
- Operating System: macOS Catalina 10.15.7
- Micronaut Version: 2.1.2
- JDK Version: 11.0.2
Example Application
https://github.com/serandel/micronauttest-bug
I've hit this issue as well with @Context and @Parallel beans. My workaround was to create profile specific beans with the test profile beans as my mocks.
Interested in a better way to workaround this.
it works if you make the @MockBean also @Context, eg.
@MockBean(ContextService.class)
@Context
ContextService contextService() {
def mock = Mock(ContextService.class)
mock.greet() >> "bar"
return mock
}
I didn't think of that! Could we please add the tip to the docs? (I could provide a PR if you wish.) Thanks a lot!
PRs welcome!
@graemerocher, is it possible to solve this by adding an alias for DefaultScope or is that a too naive approach?
Ideally it would inherit the scope from the original bean definition right?
something like this:
@AliasFor(annotation = DefaultScope.class, member = "value")
Class<? extends Annotation> scope() default Singleton.class;