Trouble running JUnit 4 tests in Gradle; javaagent that transforms JUnit classes gets thwarted

36 views
Skip to first unread message

Scott Babcock

unread,
Mar 9, 2024, 3:14:23 AM3/9/24
to Byte Buddy
I converted a Java project from Maven to Gradle, and now my JUnit 4 tests don't work. The tests rely on hooks that get added to core JUnit 4 classes by a Java agent. Everything worked as expected in the Maven version of the project. However, it appears that Gradle is loading the core JUnit 4 classes into the class cache before my agent runs, so my hooks never get installed in the converted project.

Does anyone know of a way to get Gradle to run my JUnit 4 tests in a separate thread or process with its own class cache?

Rafael Winterhalter

unread,
Mar 13, 2024, 2:26:07 PM3/13/24
to Scott Babcock, Byte Buddy
I am afraid that this would be an issue Gradle people know about. Normally, the JVM disables its class cash if an agent is detected. It seems like Gradle is lacking such detection.

Am Sa., 9. März 2024 um 09:14 Uhr schrieb Scott Babcock <sco...@gmail.com>:
I converted a Java project from Maven to Gradle, and now my JUnit 4 tests don't work. The tests rely on hooks that get added to core JUnit 4 classes by a Java agent. Everything worked as expected in the Maven version of the project. However, it appears that Gradle is loading the core JUnit 4 classes into the class cache before my agent runs, so my hooks never get installed in the converted project.

Does anyone know of a way to get Gradle to run my JUnit 4 tests in a separate thread or process with its own class cache?

--
You received this message because you are subscribed to the Google Groups "Byte Buddy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to byte-buddy+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/byte-buddy/ff861d36-5feb-45e1-aec8-6e6ff39e4b8cn%40googlegroups.com.

Scott Babcock

unread,
Mar 15, 2024, 3:57:29 PM3/15/24
to Byte Buddy
Apparently Gradle isn't the cause of this issue - or at least it's not the sole cause. The JUnit tests in this project run successfully when I switch to the Selenium 3 profile that runs in JDK 8. (It's the Selenium 4 profile that runs in JDK11 that fails.) What can I do it my Java agent to detect that the classes it's attempting to transform are already loaded into the class cache? Alternatively, is it possibly that the tests are running with a different class cache than the agent does? All of this is black art to me.

Rafael Winterhalter

unread,
Mar 16, 2024, 6:07:31 AM3/16/24
to Scott Babcock, Byte Buddy
The instrunentation API (Interface) offers a means to get hold of all loaded classes. Maybe that offers a way?

I'm not entirely sure how the caching is happening here if it's not Gradle keeping track of preloaded VMs.

Scott Babcock

unread,
Mar 16, 2024, 7:58:09 PM3/16/24
to Rafael Winterhalter, Byte Buddy
Doesn't Byte Buddy emit diagnostic information regarding the transformation process? I'll also check out the instrumentation API.

Rafael Winterhalter

unread,
Mar 17, 2024, 2:37:09 AM3/17/24
to Scott Babcock, Byte Buddy
Yes, but you decide how. Register an AgentBuilder.Listener
Reply all
Reply to author
Forward
0 new messages