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.

Comment From: sdeleuze

FYI there is ongoing work on GraalVM side to make it work, I will try to share up to date news about current status.

Comment From: mhalbritter

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.

Comment From: mhalbritter

We have a smoke test which uses @MockBean here: https://github.com/spring-projects/spring-aot-smoke-tests/tree/main/boot/boot-tcf

Comment From: mhalbritter

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.

Comment From: eiswind

@mhalbritter are there plans to support @MockBean? Having this with the proxymockmaker would be already a big gain.

Comment From: mhalbritter

Indeed, that would be really great. Hopefully we get this working with Boot 3.1.

Comment From: sdeleuze

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 @MockBean and @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.

Comment From: wilkinsona

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.

Comment From: eiswind

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?

Comment From: wilkinsona

@eiswind That's a known issue in Spring Framework: https://github.com/spring-projects/spring-framework/issues/31705

Comment From: eiswind

@wilkinsona As always: Thanks for pointing me there.

Comment From: wilkinsona

@bclozel another one for Framework please to consider as an enhancement (if it's not already supported) to @MockitoBean and @MockitoSpyBean.

Comment From: sbrannen

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

Comment From: wilkinsona

Thanks, @sbrannen.

Comment From: sbrannen

For anyone interested in this issue, please note that Spring Framework 6.2 will support the use of @TestBean and @MockitoBean in both AOT mode and within a native image. @MockitoSpyBean is also supported in AOT mode but not currently within a native image.

  • See https://github.com/spring-projects/spring-framework/issues/32933

However, as stated in the commit, @⁠MockitoBean has currently only been tested in a GraalVM native image when mocking interfaces and using Mockito's ProxyMockMaker, along with a custom runtime hints.