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 73460f7c7 Don't require a test framework when there are no tests to
run (#3570)
73460f7c7 is described below
commit 73460f7c7756c5fea680f90321d1b6a9ce53efef
Author: Olivier Lamy <[email protected]>
AuthorDate: Sat Nov 7 18:46:58 2026 +1000
Don't require a test framework when there are no tests to run (#3570)
A module that inherits groups/excludedGroups from a parent failed the build
when its target/test-classes existed but held nothing runnable - only test
resources, or only shared helper classes. The check lived in
verifyParameters(),
which just looks at whether the test classes directory exists, and ran
before
the real test scan.
Move it to after the scan so it only fires when there are actually tests to
run. The error is also split in two: one message for nothing on the test
classpath, one for a JUnit that really is too old, which now names the
version
it found.
Fixes #2795
Signed-off-by: Olivier Lamy <[email protected]>
---
.../plugin/surefire/AbstractSurefireMojo.java | 31 +++++++-----
.../plugin/surefire/AbstractSurefireMojoTest.java | 52 ++++++++++++++++++++
.../jiras/Surefire2795GroupsWithoutTestsIT.java | 45 ++++++++++++++++++
.../surefire-2795-groups-no-tests/pom.xml | 55 ++++++++++++++++++++++
.../src/test/java/issue2795/ApiMatcher.java | 28 +++++++++++
.../src/test/resources/placeholder.properties | 1 +
.../pom.xml | 55 ++++++++++++++++++++++
.../src/test/java/issue2795/PlainTest.java | 26 ++++++++++
8 files changed, 281 insertions(+), 12 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 2c13eff1f..a25488ecb 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
@@ -1087,6 +1087,9 @@ 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.
+ warnIfDefunctGroupsCombinations();
}
logReportsDirectory();
executeAfterPreconditionsChecked(scan, platform);
@@ -1223,7 +1226,6 @@ boolean verifyParameters() throws MojoFailureException,
MojoExecutionException {
ensureWorkingDirectoryExists();
ensureParallelRunningCompatibility();
warnIfUselessUseSystemClassLoaderParameter();
- warnIfDefunctGroupsCombinations();
warnIfRerunClashes();
warnIfWrongShutdownValue();
warnIfIllegalTempDir();
@@ -3006,19 +3008,24 @@ private List<CommandLineOption> commandLineOptions() {
}
private void warnIfDefunctGroupsCombinations() throws
MojoFailureException, MojoExecutionException {
+ if (!isAnyGroupsSelected() || getTestNgArtifact() != null) {
+ return;
+ }
+
Artifact junitArtifact = getJunitArtifact();
- if (isAnyGroupsSelected()) {
- if (getTestNgArtifact() == null) {
- boolean junit47Compatible = isJunit47Compatible(junitArtifact);
- boolean junit5PlatformCompatible = getJUnit5Artifact() != null;
- if (!junit47Compatible && !junit5PlatformCompatible) {
- throw new MojoFailureException("groups/excludedGroups are
specified but JUnit version on "
- + "classpath is too old to support groups. "
- + "Check your dependency:tree to see if your
project "
- + "is picking up an old junit version");
- }
- }
+ if (isJunit47Compatible(junitArtifact) || getJUnit5Artifact() != null)
{
+ return;
+ }
+
+ if (junitArtifact == null) {
+ throw new MojoFailureException("groups/excludedGroups are
specified but no test framework "
+ + "supporting groups was found on the test classpath. Add
TestNG, JUnit 4.7+ or a "
+ + "JUnit Platform engine to the test dependencies of this
module.");
}
+
+ throw new MojoFailureException("groups/excludedGroups are specified
but the JUnit version on the "
+ + "test classpath (" + junitArtifact.getVersion() + ") is too
old to support groups. "
+ + "Check your dependency:tree to see if your project is
picking up an old junit version.");
}
private void warnIfRerunClashes() throws MojoFailureException {
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 a5d0c3cf9..4fafa2cf8 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
@@ -1950,6 +1950,58 @@ protected File[] getSuiteXmlFiles() {
mojo.ensureNoSuiteXmlFiles();
}
+ @Test
+ public void shouldNotRequireTestFrameworkWhenNoGroupsSelected() throws
Exception {
+ givenProjectArtifact();
+
+ invokeMethod(mojo, "warnIfDefunctGroupsCombinations");
+ }
+
+ @Test
+ public void shouldFailWhenGroupsSelectedAndNoTestFrameworkOnClasspath() {
+ givenProjectArtifact();
+ setInternalState(mojo, "excludedGroups", "SlowTest");
+
+ Exception e = assertThrows(Exception.class, () -> invokeMethod(mojo,
"warnIfDefunctGroupsCombinations"));
+ assertThat(e.getCause())
+ .isInstanceOf(MojoFailureException.class)
+ .hasMessageStartingWith("groups/excludedGroups are specified
but no test framework");
+ }
+
+ @Test
+ public void shouldFailWhenGroupsSelectedAndJunitIsTooOld() {
+ givenProjectArtifact();
+ givenJunitOnTestClasspath("4.5");
+ setInternalState(mojo, "excludedGroups", "SlowTest");
+
+ Exception e = assertThrows(Exception.class, () -> invokeMethod(mojo,
"warnIfDefunctGroupsCombinations"));
+ assertThat(e.getCause())
+ .isInstanceOf(MojoFailureException.class)
+ .hasMessageContaining("(4.5) is too old to support groups");
+ }
+
+ @Test
+ public void shouldAcceptGroupsWithJunit47OrNewer() throws Exception {
+ givenProjectArtifact();
+ givenJunitOnTestClasspath("4.13.2");
+ setInternalState(mojo, "excludedGroups", "SlowTest");
+
+ invokeMethod(mojo, "warnIfDefunctGroupsCombinations");
+ }
+
+ private void givenProjectArtifact() {
+ Artifact projectArtifact = mojo.getMojoArtifact();
+ MavenProject project = mock(MavenProject.class);
+ when(project.getArtifact()).thenReturn(projectArtifact);
+ mojo.setProject(project);
+ }
+
+ private void givenJunitOnTestClasspath(String version) {
+ mojo.setJunitArtifactName("junit:junit");
+ setProjectDepedenciesToMojo(new DefaultArtifact(
+ "junit", "junit", createFromVersion(version), "test", "jar",
null, mock(ArtifactHandler.class)));
+ }
+
@Test
public void shouldAcceptTestNgOnlyParallelModesWithTestNg() throws
Exception {
Artifact projectArtifact = mojo.getMojoArtifact();
diff --git
a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2795GroupsWithoutTestsIT.java
b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2795GroupsWithoutTestsIT.java
new file mode 100644
index 000000000..323a16f3b
--- /dev/null
+++
b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire2795GroupsWithoutTestsIT.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.surefire.its.jiras;
+
+import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
+import org.junit.jupiter.api.Test;
+
+/**
+ * A module with no tests to run should not need a test framework just because
it
+ * inherits groups from a parent. Modules that do have tests are still checked.
+ * See #2795.
+ */
+public class Surefire2795GroupsWithoutTestsIT extends
SurefireJUnit4IntegrationTestCase {
+
+ @Test
+ void shouldSkipGroupsCheckWhenThereAreNoTestsToRun() {
+
unpack("surefire-2795-groups-no-tests").executeTest().verifyErrorFreeLog();
+ }
+
+ @Test
+ void shouldStillFailWhenTestsExistWithoutATestFramework() {
+ unpack("surefire-2795-groups-test-without-framework")
+ .maven()
+ .withFailure()
+ .executeTest()
+ .verifyTextInLog("groups/excludedGroups are specified but no
test framework "
+ + "supporting groups was found on the test classpath");
+ }
+}
diff --git
a/surefire-its/src/test/resources/surefire-2795-groups-no-tests/pom.xml
b/surefire-its/src/test/resources/surefire-2795-groups-no-tests/pom.xml
new file mode 100644
index 000000000..96a0b1bff
--- /dev/null
+++ b/surefire-its/src/test/resources/surefire-2795-groups-no-tests/pom.xml
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.plugins.surefire</groupId>
+ <artifactId>surefire-2795-groups-no-tests</artifactId>
+ <version>1.0</version>
+ <name>Inherited excludedGroups in a module with no tests to run</name>
+
+ <properties>
+ <maven.compiler.source>1.8</maven.compiler.source>
+ <maven.compiler.target>1.8</maven.compiler.target>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <!-- No test framework: just a test resource and a helper class, nothing to
run. -->
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.8.0</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${surefire.version}</version>
+ <configuration>
+ <excludedGroups>slow</excludedGroups>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/surefire-its/src/test/resources/surefire-2795-groups-no-tests/src/test/java/issue2795/ApiMatcher.java
b/surefire-its/src/test/resources/surefire-2795-groups-no-tests/src/test/java/issue2795/ApiMatcher.java
new file mode 100644
index 000000000..34adc9f61
--- /dev/null
+++
b/surefire-its/src/test/resources/surefire-2795-groups-no-tests/src/test/java/issue2795/ApiMatcher.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package issue2795;
+
+/**
+ * A shared test helper. No tests in here, and no test framework needed.
+ */
+public final class ApiMatcher {
+ public static boolean matches(String actual, String expected) {
+ return actual != null && actual.equals(expected);
+ }
+}
diff --git
a/surefire-its/src/test/resources/surefire-2795-groups-no-tests/src/test/resources/placeholder.properties
b/surefire-its/src/test/resources/surefire-2795-groups-no-tests/src/test/resources/placeholder.properties
new file mode 100644
index 000000000..8f2b20a8e
--- /dev/null
+++
b/surefire-its/src/test/resources/surefire-2795-groups-no-tests/src/test/resources/placeholder.properties
@@ -0,0 +1 @@
+placeholder=only a test resource, no test classes
diff --git
a/surefire-its/src/test/resources/surefire-2795-groups-test-without-framework/pom.xml
b/surefire-its/src/test/resources/surefire-2795-groups-test-without-framework/pom.xml
new file mode 100644
index 000000000..2c4df0932
--- /dev/null
+++
b/surefire-its/src/test/resources/surefire-2795-groups-test-without-framework/pom.xml
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements. See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership. The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied. See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.plugins.surefire</groupId>
+ <artifactId>surefire-2795-groups-test-without-framework</artifactId>
+ <version>1.0</version>
+ <name>Inherited excludedGroups with a test class but no test framework</name>
+
+ <properties>
+ <maven.compiler.source>1.8</maven.compiler.source>
+ <maven.compiler.target>1.8</maven.compiler.target>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <!-- No test framework, but a class surefire will pick up as a test. -->
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>3.8.0</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>${surefire.version}</version>
+ <configuration>
+ <excludedGroups>slow</excludedGroups>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git
a/surefire-its/src/test/resources/surefire-2795-groups-test-without-framework/src/test/java/issue2795/PlainTest.java
b/surefire-its/src/test/resources/surefire-2795-groups-test-without-framework/src/test/java/issue2795/PlainTest.java
new file mode 100644
index 000000000..d5b0f207c
--- /dev/null
+++
b/surefire-its/src/test/resources/surefire-2795-groups-test-without-framework/src/test/java/issue2795/PlainTest.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package issue2795;
+
+/**
+ * Looks like a test to surefire, but there is no test framework to run it
with.
+ */
+public class PlainTest {
+ public void checkSomething() {}
+}