As few people have already suggested, this happens due to JEP 451. Allowing the dynamic loading of agents using the -XX:+EnableDynamicAgentLoading
option is not a good solution. It just masks the problem. And even if you allow it for some reason, you should really avoid loading agents dynamically anyway. Upcoming versions of Java will get you in trouble this way.
Instead, following the Mockito's guide (in case you are using Mockito, of course), you can load the agent properly (and yes, you can directly load mockito-core
as an agent, no need to load byte-buddy-agent
in this case; the error message might be a little misleading). If you are not using Mockito, the process is the same, just point at the agent that needs to be loaded according to the error message (e.g. net.bytebuddy:byte-buddy-agent:jar
).
From your question I see your problem is with IntelliJ. The above solution fixes the problem only when you use a build tool to run your tests (e.g. when you run mvn test
on the command line), but if you want to just click on the green arrow on the left side of your test and run it from there, the message will still be present. The reason is that this way you execute directly the java
command and the build tool is not involved at all. A way to mitigate this is to update your run configuration by appending to the VM options filed the -javaagent
property (usually, you will already have -ea
set there).
The catch here is that since you are not using a build tool, you can't get automatically the path to the agent's JAR, like you would have it otherwise (e.g. ${org.mockito:mockito-core:jar}
in case of Maven + Mockito). Instead, you can do this:
-javaagent:$MAVEN_REPOSITORY$/org/mockito/mockito-core/5.14.2/mockito-core-5.14.2.jar
Unfortunately, as you can see, you need to point to a specific Mockito version since you can't know at this point the version used by your build tool.
Another catch is that you need to use this specific run configuration to run your test. If you click instead on the green arrow on the left side of the test class/method, IntelliJ will create a new run configuration that doesn't have your VM option set. I am not sure off the top of my head if the option could be set globally in the IntelliJ's settings, so that it is present every time a new run configuration is created. If someone knows a way, please share.
I know this is not the best approach, but on the good side, this is just a problem in the IDE. I really hope the folks from JetBrains come up with a solution for this.