spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Support Mockito in native tests

Open philwebb opened this issue 3 years ago • 3 comments

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.

philwebb avatar Aug 30 '22 20:08 philwebb

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

sdeleuze avatar Aug 31 '22 12:08 sdeleuze

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.

mhalbritter avatar Nov 25 '22 12:11 mhalbritter

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

mhalbritter avatar Nov 25 '22 12:11 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.

mhalbritter avatar Dec 05 '22 15:12 mhalbritter

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

eiswind avatar Dec 07 '22 08:12 eiswind

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

mhalbritter avatar Dec 08 '22 07:12 mhalbritter

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.

sdeleuze avatar Dec 04 '23 10:12 sdeleuze

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.

wilkinsona avatar Dec 04 '23 11:12 wilkinsona

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 avatar Dec 04 '23 16:12 eiswind

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

wilkinsona avatar Dec 04 '23 16:12 wilkinsona

@wilkinsona As always: Thanks for pointing me there.

eiswind avatar Dec 04 '23 16:12 eiswind

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

wilkinsona avatar May 31 '24 16:05 wilkinsona

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

sbrannen avatar Jun 01 '24 10:06 sbrannen

Thanks, @sbrannen.

wilkinsona avatar Jun 01 '24 11:06 wilkinsona