This is an automated email from the ASF dual-hosted git repository.
olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
The following commit(s) were added to refs/heads/master by this push:
new d7c7e359e Fail when argLine is set and forkCount is 0 (#3543)
d7c7e359e is described below
commit d7c7e359ea50bfbacf54923c27757f03eecc3ccc
Author: Sharang Gupta <[email protected]>
AuthorDate: Sat Nov 14 09:10:33 2026 +0000
Fail when argLine is set and forkCount is 0 (#3543)
* Warn that argLine has no effect when forkCount is 0
With forkCount=0 the tests run inside the Maven JVM, so the JVM options
in argLine are never applied. This is easy to miss when another plugin
sets argLine, for example the -javaagent that jacoco-maven-plugin adds:
the tests pass and the coverage is silently missing.
verifyParameters() already warns that useSystemClassLoader has no
effect when not forking. Add the same check for argLine.
Closes #2356
* Fail when argLine is set and forkCount is 0
A warning is easy to miss in the build output, and the JVM options are
dropped either way. Fail the build instead, as #2315 asks, once there
are tests to run and the argLine is not blank after late replacement.
* Move argLine interpolation to SurefireHelper
DefaultForkConfiguration keeps its public surface, and the helper
sits with the other shared argLine placeholder code. Two tests now
expect InvocationTargetException instead of any Exception.
---------
Co-authored-by: Olivier Lamy <[email protected]>
---
.../plugin/surefire/AbstractSurefireMojo.java | 22 ++++++-
.../maven/plugin/surefire/SurefireHelper.java | 36 +++++++++++
.../booterclient/DefaultForkConfiguration.java | 34 +---------
.../plugin/surefire/AbstractSurefireMojoTest.java | 74 ++++++++++++++++++++++
.../maven/plugin/surefire/SurefireHelperTest.java | 25 ++++++++
.../src/site/markdown/examples/debugging.md.vm | 10 +++
.../fork-options-and-parallel-execution.md.vm | 2 +-
.../its/CheckTestFailIfNoTestsForkCountIT.java | 9 ++-
.../maven/surefire/its/SystemPropertiesTestIT.java | 3 +-
.../Surefire2315ArgLineWithoutForkIT.java} | 39 +++++-------
.../default-configuration-classWithNoTests/pom.xml | 8 ++-
.../src/test/resources/system-properties/pom.xml | 3 +-
12 files changed, 202 insertions(+), 63 deletions(-)
diff --git
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index 8693bc2a9..c08a343e4 100644
---
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -139,6 +139,7 @@
import static
org.apache.maven.plugin.surefire.AbstractSurefireMojo.PluginFailureReason.COULD_NOT_RUN_SPECIFIED_TESTS;
import static
org.apache.maven.plugin.surefire.AbstractSurefireMojo.PluginFailureReason.NONE;
import static
org.apache.maven.plugin.surefire.SurefireDependencyResolver.isWithinVersionSpec;
+import static
org.apache.maven.plugin.surefire.SurefireHelper.interpolateArgLineWithPropertyExpressions;
import static
org.apache.maven.plugin.surefire.SurefireHelper.replaceThreadNumberPlaceholders;
import static org.apache.maven.plugin.surefire.util.DependencyScanner.filter;
import static
org.apache.maven.surefire.api.booter.ProviderParameterNames.EXCLUDE_JUNIT5_ENGINES_PROP;
@@ -535,7 +536,8 @@ public abstract class AbstractSurefireMojo extends
AbstractMojo implements Suref
private String jvm;
/**
- * Arbitrary JVM options to set on the command line. Only effective for
forked executions.
+ * Arbitrary JVM options to set on the command line. Only effective for
forked executions: the build fails if
+ * it is not blank while {@code forkCount} is 0 and there are tests to run.
* <br>
* <br>
* Using an alternate syntax for placeholders in {@code argLine}, namely
<code>@{...}</code> allows late replacement
@@ -1114,9 +1116,11 @@ public void execute() throws MojoExecutionException,
MojoFailureException {
if (getConsoleLogger().isDebugEnabled()) {
getConsoleLogger().debug("Tests to run: " +
scan.getClasses());
}
- // Only check this once we know there are tests. A module
with nothing to run
- // should not need a test framework just to satisfy
inherited groups.
+ // Only check these once we know there are tests. A module
with nothing to run
+ // should not need a test framework just to satisfy
inherited groups, nor fail
+ // because of an inherited argLine.
warnIfDefunctGroupsCombinations();
+ ensureNoArgLineWhenNotForking();
}
logReportsDirectory();
executeAfterPreconditionsChecked(scan, platform);
@@ -3143,6 +3147,18 @@ private void
warnIfUselessUseSystemClassLoaderParameter() {
}
}
+ private void ensureNoArgLineWhenNotForking() throws MojoFailureException {
+ if (isNotForking() && isNotBlank(getArgLine()) &&
isNotBlank(interpolatedArgLine())) {
+ throw new MojoFailureException("argLine cannot be applied because
forkCount is 0 and the tests run "
+ + "inside the Maven JVM. Remove argLine or set forkCount
to 1 or more.");
+ }
+ }
+
+ private String interpolatedArgLine() {
+ return interpolateArgLineWithPropertyExpressions(
+ getArgLine(), getProject().getModel().getProperties());
+ }
+
private boolean isNotForking() {
return !isForking();
}
diff --git
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
index bd3e082e7..23fe56fd6 100644
---
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
+++
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
@@ -19,6 +19,7 @@
package org.apache.maven.plugin.surefire;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import java.io.File;
import java.util.ArrayList;
@@ -26,6 +27,7 @@
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
+import java.util.Properties;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenSession;
@@ -113,6 +115,40 @@ public static String
replaceThreadNumberPlaceholders(@Nonnull String argLine, in
.replace(FORK_NUMBER_PLACEHOLDER, threadNumberAsString);
}
+ /**
+ * Replaces expressions <pre>@{property-name}</pre> with the corresponding
properties
+ * from the model. This allows late evaluation of property values when the
plugin is executed (as compared
+ * to evaluation when the pom is parsed as is done with
<pre>${property-name}</pre> expressions).
+ *
+ * This allows other plugins to modify or set properties with the changes
getting picked up by surefire.
+ *
+ * @param argLine the configured argLine, may be {@code null}
+ * @param modelProperties the properties of the project model
+ * @return the trimmed argLine with its placeholders replaced, or an empty
string if there is no argLine
+ */
+ @Nonnull
+ public static String interpolateArgLineWithPropertyExpressions(
+ @Nullable String argLine, @Nonnull Properties modelProperties) {
+ if (argLine == null) {
+ return "";
+ }
+
+ String resolvedArgLine = argLine.trim();
+
+ if (resolvedArgLine.isEmpty()) {
+ return "";
+ }
+
+ for (final String key : modelProperties.stringPropertyNames()) {
+ String field = "@{" + key + "}";
+ if (argLine.contains(field)) {
+ resolvedArgLine = resolvedArgLine.replace(field,
modelProperties.getProperty(key, ""));
+ }
+ }
+
+ return resolvedArgLine;
+ }
+
public static File replaceForkThreadsInPath(File path, int replacement) {
Deque<String> dirs = new LinkedList<>();
File root = path;
diff --git
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
index c590adde7..35055dd86 100644
---
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
+++
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
@@ -38,6 +38,7 @@
import org.apache.maven.surefire.booter.SurefireBooterForkException;
import org.apache.maven.surefire.extensions.ForkNodeFactory;
+import static
org.apache.maven.plugin.surefire.SurefireHelper.interpolateArgLineWithPropertyExpressions;
import static
org.apache.maven.plugin.surefire.SurefireHelper.replaceForkThreadsInPath;
import static
org.apache.maven.plugin.surefire.SurefireHelper.replaceThreadNumberPlaceholders;
import static org.apache.maven.plugin.surefire.util.Relocator.relocate;
@@ -220,36 +221,6 @@ private File getWorkingDirectory(int forkNumber) throws
SurefireBooterForkExcept
return cwd;
}
- /**
- * Replaces expressions <pre>@{property-name}</pre> with the corresponding
properties
- * from the model. This allows late evaluation of property values when the
plugin is executed (as compared
- * to evaluation when the pom is parsed as is done with
<pre>${property-name}</pre> expressions).
- *
- * This allows other plugins to modify or set properties with the changes
getting picked up by surefire.
- */
- @Nonnull
- private String interpolateArgLineWithPropertyExpressions() {
- if (getArgLine() == null) {
- return "";
- }
-
- String resolvedArgLine = getArgLine().trim();
-
- if (resolvedArgLine.isEmpty()) {
- return "";
- }
-
- for (final String key : getModelProperties().stringPropertyNames()) {
- String field = "@{" + key + "}";
- if (getArgLine().contains(field)) {
- resolvedArgLine =
- resolvedArgLine.replace(field,
getModelProperties().getProperty(key, ""));
- }
- }
-
- return resolvedArgLine;
- }
-
@Nonnull
private static String stripWhitespace(@Nonnull String argLine) {
return argLine.replaceAll("\\s", " ");
@@ -345,7 +316,8 @@ protected Classpath getBooterClasspath() {
@Nonnull
private String newJvmArgLine(int forks) {
- String interpolatedArgs =
stripWhitespace(interpolateArgLineWithPropertyExpressions());
+ String interpolatedArgs =
+
stripWhitespace(interpolateArgLineWithPropertyExpressions(getArgLine(),
getModelProperties()));
String argsWithReplacedForkNumbers =
replaceThreadNumberPlaceholders(interpolatedArgs, forks);
return extendJvmArgLine(argsWithReplacedForkNumbers);
}
diff --git
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
index b12553b8a..71e7e1d9b 100644
---
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
+++
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
@@ -23,6 +23,7 @@
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -48,6 +49,7 @@
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
+import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
@@ -2074,6 +2076,78 @@ public void
shouldStringifyNonStringSystemPropertyVariableValues() {
assertThat(mojo.getSystemPropertyVariables()).containsExactly(entry("isOffline",
"true"));
}
+ @Test
+ public void shouldFailWhenArgLineIsSetAndNotForking() {
+ givenProjectWithModelProperties(new Properties());
+ mojo.forkCount = "0";
+ mojo.setArgLine("-javaagent:jacocoagent.jar");
+
+ InvocationTargetException e = assertThrows(
+ InvocationTargetException.class, () -> invokeMethod(mojo,
"ensureNoArgLineWhenNotForking"));
+ assertThat(e.getCause())
+ .isInstanceOf(MojoFailureException.class)
+ .hasMessage("argLine cannot be applied because forkCount is 0
and the tests run "
+ + "inside the Maven JVM. Remove argLine or set
forkCount to 1 or more.");
+ }
+
+ @Test
+ public void
shouldFailWhenArgLinePlaceholderResolvesToJvmOptionsAndNotForking() {
+ Properties modelProperties = new Properties();
+ modelProperties.setProperty("jacocoArgLine",
"-javaagent:jacocoagent.jar");
+ givenProjectWithModelProperties(modelProperties);
+ mojo.forkCount = "0";
+ mojo.setArgLine("@{jacocoArgLine}");
+
+ InvocationTargetException e = assertThrows(
+ InvocationTargetException.class, () -> invokeMethod(mojo,
"ensureNoArgLineWhenNotForking"));
+ assertThat(e.getCause())
+ .isInstanceOf(MojoFailureException.class)
+ .hasMessageStartingWith("argLine cannot be applied because
forkCount is 0");
+ }
+
+ @Test
+ public void shouldAcceptArgLineWhenForking() throws Exception {
+ givenProjectWithModelProperties(new Properties());
+ mojo.forkCount = "1";
+ mojo.setArgLine("-javaagent:jacocoagent.jar");
+
+ invokeMethod(mojo, "ensureNoArgLineWhenNotForking");
+ }
+
+ @Test
+ public void shouldAcceptMissingArgLineWhenNotForking() throws Exception {
+ mojo.forkCount = "0";
+
+ invokeMethod(mojo, "ensureNoArgLineWhenNotForking");
+ }
+
+ @Test
+ public void shouldAcceptBlankArgLineWhenNotForking() throws Exception {
+ mojo.forkCount = "0";
+ mojo.setArgLine(" \n ");
+
+ invokeMethod(mojo, "ensureNoArgLineWhenNotForking");
+ }
+
+ @Test
+ public void shouldAcceptArgLineResolvingToNothingWhenNotForking() throws
Exception {
+ Properties modelProperties = new Properties();
+ modelProperties.setProperty("jacocoArgLine", "");
+ givenProjectWithModelProperties(modelProperties);
+ mojo.forkCount = "0";
+ mojo.setArgLine("@{jacocoArgLine}");
+
+ invokeMethod(mojo, "ensureNoArgLineWhenNotForking");
+ }
+
+ private void givenProjectWithModelProperties(Properties modelProperties) {
+ Model model = new Model();
+ model.setProperties(modelProperties);
+ MavenProject project = mock(MavenProject.class);
+ when(project.getModel()).thenReturn(model);
+ mojo.setProject(project);
+ }
+
private void setProjectDepedenciesToMojo(Artifact... deps) {
for (Artifact dep : deps) {
mojo.getProjectArtifactMap().put(dep.getGroupId() + ":" +
dep.getArtifactId(), dep);
diff --git
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
index 4ac0441ed..00fa2a0d6 100644
---
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
+++
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
@@ -21,6 +21,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
+import java.util.Properties;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -36,6 +37,7 @@
import static java.util.Collections.addAll;
import static java.util.Collections.singleton;
import static
org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPath;
+import static
org.apache.maven.plugin.surefire.SurefireHelper.interpolateArgLineWithPropertyExpressions;
import static org.apache.maven.plugin.surefire.SurefireHelper.reportExecution;
import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
import static org.assertj.core.api.Assertions.assertThat;
@@ -69,6 +71,29 @@ public void shouldReplaceLongForkNumberPath() {
assertThat(changed.getPath()).isEqualTo(new File(new File(root,
"reports-5"), "subdir").getPath());
}
+ @Test
+ public void shouldInterpolateArgLinePlaceholderFromModelProperties() {
+ Properties modelProperties = new Properties();
+ modelProperties.setProperty("jacocoArgLine",
"-javaagent:jacocoagent.jar");
+
+ assertThat(interpolateArgLineWithPropertyExpressions("
@{jacocoArgLine} -Xmx64m ", modelProperties))
+ .isEqualTo("-javaagent:jacocoagent.jar -Xmx64m");
+ }
+
+ @Test
+ public void shouldKeepArgLinePlaceholderWithoutModelProperty() {
+ assertThat(interpolateArgLineWithPropertyExpressions("@{unknown}", new
Properties()))
+ .isEqualTo("@{unknown}");
+ }
+
+ @Test
+ public void shouldInterpolateMissingOrBlankArgLineToEmptyString() {
+ assertThat(interpolateArgLineWithPropertyExpressions(null, new
Properties()))
+ .isEmpty();
+ assertThat(interpolateArgLineWithPropertyExpressions(" \n ", new
Properties()))
+ .isEmpty();
+ }
+
@Test
public void shouldBeThreeDumpFiles() {
String[] dumps = SurefireHelper.getDumpFilesToPrint();
diff --git a/maven-surefire-plugin/src/site/markdown/examples/debugging.md.vm
b/maven-surefire-plugin/src/site/markdown/examples/debugging.md.vm
index c2686c064..b36ba522b 100644
--- a/maven-surefire-plugin/src/site/markdown/examples/debugging.md.vm
+++ b/maven-surefire-plugin/src/site/markdown/examples/debugging.md.vm
@@ -64,6 +64,16 @@ mvn -DforkCount=0 verify
#{end}
```
+The JVM options in `argLine` cannot be applied without forking, so the build
fails if `argLine` is not empty and there are tests to run. If it comes from a
property, as with the JaCoCo agent, you can clear it on the command line:
+
+```text
+#{if}(${project.artifactId}=="maven-surefire-plugin")
+mvn -DforkCount=0 -DargLine= test
+#{else}
+mvn -DforkCount=0 -DargLine= verify
+#{end}
+```
+
Then all you need to do is debug Maven itself. Maven ships with a `mvnDebug`
shell script that you can use to launch Maven with convenient debugging options:
```text
diff --git
a/maven-surefire-plugin/src/site/markdown/examples/fork-options-and-parallel-execution.md.vm
b/maven-surefire-plugin/src/site/markdown/examples/fork-options-and-parallel-execution.md.vm
index 01667b4e6..182a01893 100644
---
a/maven-surefire-plugin/src/site/markdown/examples/fork-options-and-parallel-execution.md.vm
+++
b/maven-surefire-plugin/src/site/markdown/examples/fork-options-and-parallel-execution.md.vm
@@ -186,7 +186,7 @@ The _default setting_ is `forkCount=1`/`reuseForks=true`,
which means that ${pro
`forkCount=1`/`reuseForks=false` executes each test class in its own JVM
process, one after another. It creates the highest level of separation for the
test execution, but it would probably also give you the longest execution time
of all the available options. Consider it as a last resort.
-With the `argLine` property, you can specify additional parameters to be
passed to the forked JVM process, such as memory settings. System property
variables from the main maven process are passed to the forked process as well.
Additionally, you can use the element `systemPropertyVariables` to specify
variables and values to be added to the system properties during the test
execution.
+With the `argLine` property, you can specify additional parameters to be
passed to the forked JVM process, such as memory settings. They cannot be
applied without forking, so the build fails if `argLine` is not empty while
`forkCount=0` and there are tests to run. System property variables from the
main maven process are passed to the forked process as well. Additionally, you
can use the element `systemPropertyVariables` to specify variables and values
to be added to the system propertie [...]
You can use the placeholder `${esc.d}{surefire.forkNumber}` within `argLine`,
`environmentVariables` (since ${project.artifactId}:3.2.0), or within the
system properties (both those specified via `mvn test -D...` and via
`systemPropertyVariables`). Before executing the tests, the
${thisPlugin.toLowerCase()} plugin replaces that placeholder by the number of
the actually executing process, counting from 1 to the effective value of
`forkCount` times the maximum number of parallel executions [...]
diff --git
a/surefire-its/src/test/java/org/apache/maven/surefire/its/CheckTestFailIfNoTestsForkCountIT.java
b/surefire-its/src/test/java/org/apache/maven/surefire/its/CheckTestFailIfNoTestsForkCountIT.java
index 124f38044..7df3758aa 100644
---
a/surefire-its/src/test/java/org/apache/maven/surefire/its/CheckTestFailIfNoTestsForkCountIT.java
+++
b/surefire-its/src/test/java/org/apache/maven/surefire/its/CheckTestFailIfNoTestsForkCountIT.java
@@ -40,7 +40,13 @@ public void failIfNoTestsForkAlways() {
@Test
public void failIfNoTestsForkNever() {
-
unpack().forkNever().failIfNoTests(true).maven().withFailure().executeTest();
+ unpack().forkNever()
+ .argLine("")
+ .failIfNoTests(true)
+ .maven()
+ .withFailure()
+ .executeTest()
+ .verifyTextInLog("No tests were executed!");
}
@Test
@@ -62,6 +68,7 @@ public void dontFailIfNoTestsForkAlways() {
@Test
public void dontFailIfNoTestsForkNever() {
Assertions.assertThat(unpack().forkNever()
+ .argLine("")
.failIfNoTests(false)
.executeTest()
.verifyErrorFreeLog()
diff --git
a/surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
b/surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
index c73338ba0..5737e9009 100644
---
a/surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
+++
b/surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
@@ -43,7 +43,8 @@ public void testSystemPropertiesNoFork() {
unpack().forkNever()
.addGoal("-DsetOnMavenCommandLine=baz")
.addGoal("-DsetOnArgLineWorkAround=baz")
- // DGF fake the argLine, since we're not forking
+ // not forking, so the argLine cannot apply: clear it and set
its property on the Maven JVM
+ .argLine("")
.addGoal("-DsetOnArgLine=bar")
.executeTest()
.verifyErrorFree(8);
diff --git
a/surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2315ArgLineWithoutForkIT.java
similarity index 53%
copy from
surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
copy to
surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2315ArgLineWithoutForkIT.java
index c73338ba0..40747e8ef 100644
---
a/surefire-its/src/test/java/org/apache/maven/surefire/its/SystemPropertiesTestIT.java
+++
b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2315ArgLineWithoutForkIT.java
@@ -16,40 +16,35 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.maven.surefire.its;
+package org.apache.maven.surefire.its.jiras;
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
-import org.apache.maven.surefire.its.fixture.SurefireLauncher;
import org.junit.jupiter.api.Test;
/**
- * Test system properties
- *
- * @author <a href="mailto:[email protected]">Dan Fabulich</a>
+ * The JVM options in argLine cannot be applied when the tests run inside the
Maven JVM,
+ * so the build fails instead of dropping them. A module with no tests to run
is left alone.
+ * See #2315 and #2356.
*/
-public class SystemPropertiesTestIT extends SurefireJUnit4IntegrationTestCase {
+public class Surefire2315ArgLineWithoutForkIT extends
SurefireJUnit4IntegrationTestCase {
@Test
- public void testSystemProperties() {
- unpack().addGoal("-DsetOnMavenCommandLine=baz")
- .addGoal("-DsetOnArgLineWorkAround=baz")
+ void shouldFailWhenArgLineIsSetAndTestsRunInsideTheMavenJvm() {
+ unpack("default-configuration")
+ .forkNever()
+ .argLine("-Xmx64m")
+ .maven()
+ .withFailure()
.executeTest()
- .verifyErrorFree(8);
+ .verifyTextInLog("argLine cannot be applied because forkCount
is 0");
}
@Test
- public void testSystemPropertiesNoFork() {
-
- unpack().forkNever()
- .addGoal("-DsetOnMavenCommandLine=baz")
- .addGoal("-DsetOnArgLineWorkAround=baz")
- // DGF fake the argLine, since we're not forking
- .addGoal("-DsetOnArgLine=bar")
+ void shouldAcceptArgLineWhenThereAreNoTestsToRun() {
+ unpack("surefire-2795-groups-no-tests")
+ .forkNever()
+ .argLine("-Xmx64m")
.executeTest()
- .verifyErrorFree(8);
- }
-
- public SurefireLauncher unpack() {
- return unpack("/system-properties");
+ .verifyErrorFreeLog();
}
}
diff --git
a/surefire-its/src/test/resources/default-configuration-classWithNoTests/pom.xml
b/surefire-its/src/test/resources/default-configuration-classWithNoTests/pom.xml
index d92d563c1..7903f0ad3 100644
---
a/surefire-its/src/test/resources/default-configuration-classWithNoTests/pom.xml
+++
b/surefire-its/src/test/resources/default-configuration-classWithNoTests/pom.xml
@@ -36,6 +36,11 @@
<version>1.0-SNAPSHOT</version>
<name>Test for class with no tests</name>
+ <properties>
+ <!-- a property rather than plugin configuration, so that a non-forked run
can clear it -->
+ <argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
+ </properties>
+
<dependencies>
<dependency>
<groupId>junit</groupId>
@@ -50,9 +55,6 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
- <configuration>
-
<argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
- </configuration>
</plugin>
</plugins>
</pluginManagement>
diff --git a/surefire-its/src/test/resources/system-properties/pom.xml
b/surefire-its/src/test/resources/system-properties/pom.xml
index a058f87ad..f1c91bf80 100644
--- a/surefire-its/src/test/resources/system-properties/pom.xml
+++ b/surefire-its/src/test/resources/system-properties/pom.xml
@@ -73,7 +73,6 @@
<overriddenPropertyFomFile>value2</overriddenPropertyFomFile>
</systemPropertyVariables>
<systemPropertiesFile>${project.basedir}/src/test/config/propsfile.properties</systemPropertiesFile>
- <argLine>-DsetOnArgLine=bar</argLine>
</configuration>
</plugin>
</plugins>
@@ -90,6 +89,8 @@
</dependencies>
<properties>
+ <!-- a property rather than plugin configuration, so that a non-forked run
can clear it -->
+ <argLine>-DsetOnArgLine=bar</argLine>
<setOnArgLineWorkAround>fool</setOnArgLineWorkAround>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>