mockito icon indicating copy to clipboard operation
mockito copied to clipboard

Help wanted: compiling mockito with native-image

Open eiswind opened this issue 4 years ago • 11 comments

I am trying to compile mockito with mock-maker-proxy with graalvm native image, and I got stuck with the plugin mechanism (At least the stacktrace point to that). (In preparation for having some mockito suppport in spring-native https://github.com/spring-projects-experimental/spring-native/issues/1063)

I know this maybe a little of topic, but having at least "some" mockito when running native tests made me get started with this.

What I've got so far is

  • some proxy config
  • some reflection config
  • basic compiler args

When I compile this example https://github.com/eiswind/native-mock-example

I get a NPE at:

     Caused by: java.lang.NullPointerException
       org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:26)
       org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:20)
       com.oracle.svm.core.classinitialization.ClassInitializationInfo.invokeClassInitializer(ClassInitializationInfo.java:375)
       com.oracle.svm.core.classinitialization.ClassInitializationInfo.initialize(ClassInitializationInfo.java:295)

from which I would guess that registry itself is null, but that makes only little sense. I added some --initialize-at-runtime for the plugins package but that didn't do it.

So for know I can only hope that this is something of interest and I really would appreciate some pointers what to try.

eiswind avatar Sep 26 '21 14:09 eiswind

@raphw Does https://github.com/oracle/graal/issues/3969 resolve this issue as well?

TimvdLippe avatar Nov 01 '21 19:11 TimvdLippe

Possibly, but this would require a detection run prior. There's not much of a point, in my opinion, to run one-off tests with Graal native image, though. The efficiency comes from repeated startup in of an unchanged code. That's not really our niche.

raphw avatar Nov 02 '21 00:11 raphw

I really think there is a point in running the tests with native image, as we can detect many configuration errors that happen only at runtime.

eiswind avatar Nov 02 '21 07:11 eiswind

You are right, that's a use case. We will only be able to support Graal native image with the subclass mock maker for now, unfortunately, which requires a bunch of add-opens on the command line to work with Java 17. But in theory, it is definitely possible that this would work then.

raphw avatar Nov 02 '21 09:11 raphw

I've added support for Mockito to the GraalVM reachability metadata and got the ProxyMockMaker to work, see here for more details.

The reachability metadata repo is a way to get non-compliant libraries to work nonetheless on GraalVM. Feel free to copy the GraalVM metadata files directly in the Mockito codebase (actually I would be very happy about that :)).

What is the status of the other mock makers (particularly one which allows mocking of classes) on GraalVM?

mhalbritter avatar Dec 05 '22 15:12 mhalbritter

It works already: https://gist.github.com/raphw/e0f6864ddcad69630d839c2eebaad818

But it requires some prework. As far as I know, the Graal team is working on something to ease this.

raphw avatar Dec 07 '22 10:12 raphw