Support Mockito in native tests
Currently Mockito doesn't work with native tests. See https://github.com/spring-projects-experimental/spring-native/issues/1561 and https://github.com/spring-projects-experimental/spring-native/issues/1063 for more details.
For now, we can disable the ResetMocksTestExecutionListener, but it would be nice if we could make Mockito work.
FYI there is ongoing work on GraalVM side to make it work, I will try to share up to date news about current status.
There's more than just GraalVM supporting this. Currently, using @MockBean wreaks havoc even when not using native-image, just enabling AOT mode is enough.
We have a smoke test which uses @MockBean here: https://github.com/spring-projects/spring-aot-smoke-tests/tree/main/boot/boot-tcf
I've added metadata for Mockito 4.8.1 to the reachability-metadata repository. However I found no way to get the class mocks from Mockito working, as they all rely on creating classes at runtime. But it works when using the ProxyMockMaker, which is able to mock interfaces.
@mhalbritter are there plans to support @MockBean? Having this with the proxymockmaker would be already a big gain.
Indeed, that would be really great. Hopefully we get this working with Boot 3.1.
Various users (example here) seems confused about seeing their unit tests broken when using just the AOT plugin, without using GraalVM native images support.
My proposal would either to:
- Update the title of this issue in order to focus on the fact that Mockito support is broken at AOT level as mentioned by @mhalbritter here and @wilkinsona here.
- Create a different issue focused on the broken AOT support for
@MockBeanand@SpyBean.
Since I am not sure we should do something specific for Mockito on native side (should be handled on GraalVM side), I would maybe favor turning this issue into fixing the AOT support, and see later if a dedicated native issue is needed, but of course up to the Boot team to decide.
Thanks, @sdeleuze. I've updated the issue's title to focus on the AOT side of things. Once that has been addressed we can consider what's left for general native support of Mockito.
If just found with updating to 3.2 that @DisabledInAotMode did break my native tests. I had to use @DisabledInNativeTest and ignore the aot errors to make the native tests work again. There was an issue with some AotDetector beeing initialized at compile time when trying to run the native tests if I remember correctly. Should that be a seperate issue?
@eiswind That's a known issue in Spring Framework: https://github.com/spring-projects/spring-framework/issues/31705
@wilkinsona As always: Thanks for pointing me there.
@bclozel another one for Framework please to consider as an enhancement (if it's not already supported) to @MockitoBean and @MockitoSpyBean.
I think this issue can be closed as superseded by:
- https://github.com/spring-projects/spring-framework/issues/32925
- https://github.com/spring-projects/spring-framework/issues/32932
- https://github.com/spring-projects/spring-framework/issues/32933
Thanks, @sbrannen.