1616
1717package org .springframework .build ;
1818
19- import java .util .Map ;
20-
2119import org .gradle .api .Project ;
20+ import org .gradle .api .artifacts .Configuration ;
21+ import org .gradle .api .artifacts .Dependency ;
2222import org .gradle .api .plugins .JavaBasePlugin ;
2323import org .gradle .api .tasks .testing .Test ;
2424import org .gradle .api .tasks .testing .TestFrameworkOptions ;
2525import org .gradle .api .tasks .testing .junitplatform .JUnitPlatformOptions ;
2626import org .gradle .testretry .TestRetryPlugin ;
2727import org .gradle .testretry .TestRetryTaskExtension ;
2828
29+ import java .util .Map ;
30+
2931/**
3032 * Conventions that are applied in the presence of the {@link JavaBasePlugin}. When the
3133 * plugin is applied:
3234 * <ul>
3335 * <li>The {@link TestRetryPlugin Test Retry} plugin is applied so that flaky tests
3436 * are retried 3 times when running on the CI server.
37+ * <li>Common test properties are configured
38+ * <li>The Mockito Java agent is set on test tasks.
3539 * </ul>
3640 *
3741 * @author Brian Clozel
@@ -45,6 +49,7 @@ void apply(Project project) {
4549 }
4650
4751 private void configureTestConventions (Project project ) {
52+ configureMockitoAgent (project );
4853 project .getTasks ().withType (Test .class ,
4954 test -> {
5055 configureTests (project , test );
@@ -75,6 +80,19 @@ private void configureTests(Project project, Test test) {
7580 );
7681 }
7782
83+ private void configureMockitoAgent (Project project ) {
84+ if (project .hasProperty ("mockitoVersion" )) {
85+ String mockitoVersion = (String ) project .getProperties ().get ("mockitoVersion" );
86+ Configuration mockitoAgentConfig = project .getConfigurations ().create ("mockitoAgent" );
87+ mockitoAgentConfig .setTransitive (false );
88+ Dependency mockitoCore = project .getDependencies ().create ("org.mockito:mockito-core:" + mockitoVersion );
89+ mockitoAgentConfig .getDependencies ().add (mockitoCore );
90+ project .afterEvaluate (p -> {
91+ p .getTasks ().withType (Test .class , test -> test .jvmArgs ("-javaagent:" + mockitoAgentConfig .getAsPath ()));
92+ });
93+ }
94+ }
95+
7896 private void configureTestRetryPlugin (Project project , Test test ) {
7997 project .getPlugins ().withType (TestRetryPlugin .class , testRetryPlugin -> {
8098 TestRetryTaskExtension testRetry = test .getExtensions ().getByType (TestRetryTaskExtension .class );
0 commit comments