@@ -157,7 +157,7 @@ ext {
157
157
libs. log4j2Api,
158
158
libs. log4j2Core
159
159
]
160
-
160
+
161
161
}
162
162
163
163
allprojects {
@@ -484,11 +484,36 @@ subprojects {
484
484
}
485
485
}
486
486
487
+ // Workaround for Mockito Java Agent restrictions in Java 21+
488
+ // Starting with Java 21, the JDK restricts libraries from attaching a Java agent
489
+ // to their own JVM. As a result, Mockito’s inline mock maker (mockito-core)
490
+ // fails without explicit instrumentation, and the JVM consistently emits warnings.
491
+ // See also: https://javadoc.io/doc/org.mockito/mockito-core/latest/org.mockito/org/mockito/Mockito.html#mockito-instrumentation
492
+ afterEvaluate { subproject ->
493
+ def hasMockitoCore = subproject. configurations. findAll {
494
+ it. canBeResolved
495
+ }. any { config ->
496
+ config. incoming. dependencies. any { dependency ->
497
+ " $dependency " == libs. mockitoCore
498
+ }
499
+ }
500
+
501
+ if (hasMockitoCore) {
502
+ subproject. configurations {
503
+ mockitoAgent {
504
+ transitive = false
505
+ }
506
+ }
507
+ subproject. dependencies {
508
+ mockitoAgent libs. mockitoCore
509
+ }
510
+ }
511
+ }
512
+
487
513
// The suites are for running sets of tests in IDEs.
488
514
// Gradle will run each test class, so we exclude the suites to avoid redundantly running the tests twice.
489
515
def testsToExclude = [' **/*Suite.class' ]
490
516
491
-
492
517
// This task will copy JUnit XML files out of the sub-project's build directory and into
493
518
// a top-level build/junit-xml directory. This is necessary to avoid reporting on tests which
494
519
// were not run, but instead were restored via FROM-CACHE. See KAFKA-17479 for more details.
@@ -518,6 +543,14 @@ subprojects {
518
543
}
519
544
520
545
test {
546
+
547
+ doFirst {
548
+ def mockitoAgentConfig = configurations. findByName(' mockitoAgent' )
549
+ if (mockitoAgentConfig) {
550
+ jvmArgs(" -javaagent:${ mockitoAgentConfig.asPath} " )
551
+ }
552
+ }
553
+
521
554
maxParallelForks = maxTestForks
522
555
ignoreFailures = userIgnoreFailures
523
556
@@ -551,7 +584,7 @@ subprojects {
551
584
maxFailures = userMaxTestRetryFailures
552
585
}
553
586
}
554
-
587
+
555
588
finalizedBy(" copyTestXml" )
556
589
}
557
590
0 commit comments