Skip to content

Commit abc49a8

Browse files
authored
Merge pull request #1426 from hcoles/feature/auto_add_engines
auto add junit engines when missing from classpath
2 parents 2ebe677 + 315e422 commit abc49a8

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed

pitest-maven-verification/src/test/java/org/pitest/PitMojoIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public void shouldCorrectlyHandleOverrides() throws Exception {
350350

351351
@Test
352352
public void shouldReadExclusionsFromSurefireConfig() throws Exception {
353-
// Note this test also tests the argline parsing concern
353+
// Note this test also tests the argline parsing concern and junit5 classpath resolution
354354

355355
File testDir = prepare("/pit-surefire-excludes");
356356
verifier.executeGoal("test");

pitest-maven-verification/src/test/resources/pit-surefire-excludes/pom.xml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
<version>1.0-SNAPSHOT</version>
88
<name>surefire-excludes</name>
99
<dependencies>
10-
<dependency>
11-
<groupId>junit</groupId>
12-
<artifactId>junit</artifactId>
13-
<version>4.13.1</version>
14-
<scope>test</scope>
15-
</dependency>
10+
<!-- include api only, other dependencies should auto add -->
11+
<dependency>
12+
<groupId>org.junit.jupiter</groupId>
13+
<artifactId>junit-jupiter-api</artifactId>
14+
<version>5.13.4</version>
15+
<scope>test</scope>
16+
</dependency>
1617
</dependencies>
1718
<properties>
1819
<a.value>isSet</a.value>
@@ -24,16 +25,16 @@
2425
<plugin>
2526
<groupId>org.apache.maven.plugins</groupId>
2627
<artifactId>maven-compiler-plugin</artifactId>
27-
<version>2.4</version>
28+
<version>3.10.1</version>
2829
<configuration>
29-
<source>1.8</source>
30-
<target>1.8</target>
30+
<source>11</source>
31+
<target>11</target>
3132
</configuration>
3233
</plugin>
3334
<plugin>
3435
<groupId>org.apache.maven.plugins</groupId>
3536
<artifactId>maven-surefire-plugin</artifactId>
36-
<version>2.18.1</version>
37+
<version>3.1.2</version>
3738
<configuration>
3839
<excludes>
3940
<exclude>**/FailingTest.java</exclude>
@@ -58,6 +59,14 @@
5859
</mutators>
5960
<verbose>true</verbose>
6061
</configuration>
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.pitest</groupId>
65+
<artifactId>pitest-junit5-plugin</artifactId>
66+
<version>1.2.3</version>
67+
</dependency>
68+
</dependencies>
69+
6170
</plugin>
6271
</plugins>
6372
</build>

pitest-maven-verification/src/test/resources/pit-surefire-excludes/src/test/java/com/example/AnotherTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.util.Arrays;
44
import java.util.Collection;
5-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class AnotherTest {
1010

pitest-maven-verification/src/test/resources/pit-surefire-excludes/src/test/java/com/example/BadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.util.Arrays;
44
import java.util.Collection;
5-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class BadTest {
1010

pitest-maven/src/main/java/org/pitest/maven/MojoToReportOptionsConverter.java

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
import org.apache.maven.project.MavenProject;
2323
import org.codehaus.plexus.util.xml.Xpp3Dom;
2424
import org.eclipse.aether.artifact.DefaultArtifact;
25+
import org.eclipse.aether.graph.DefaultDependencyNode;
2526
import org.eclipse.aether.resolution.ArtifactRequest;
2627
import org.eclipse.aether.resolution.ArtifactResolutionException;
2728
import org.eclipse.aether.resolution.ArtifactResult;
29+
import org.eclipse.aether.resolution.DependencyRequest;
30+
import org.eclipse.aether.resolution.DependencyResolutionException;
31+
import org.eclipse.aether.resolution.DependencyResult;
2832
import org.pitest.classinfo.ClassName;
2933
import org.pitest.classpath.DirectoryClassPathRoot;
3034
import org.pitest.functional.FCollection;
@@ -86,7 +90,9 @@ public ReportOptions convert() {
8690

8791
classPath.addAll(this.mojo.getAdditionalClasspathElements());
8892

89-
autoAddJUnitPlatform(classPath);
93+
autoAddJUnitPlatformLauncher(classPath);
94+
autoAddJUnitPlatformEngine(classPath);
95+
autoAddJupiterEngine(classPath);
9096
removeExcludedDependencies(classPath);
9197

9298
addCrossModuleDirsToClasspath(classPath);
@@ -125,13 +131,22 @@ private void addCrossModuleDirsToClasspath(List<String> classPath) {
125131
*
126132
* @param classPath classpath to modify
127133
*/
128-
private void autoAddJUnitPlatform(List<String> classPath) {
134+
private void autoAddJUnitPlatformLauncher(List<String> classPath) {
135+
autoAddJUnitPlatformArtifact(classPath, "junit-platform-launcher");
136+
}
137+
138+
private void autoAddJUnitPlatformEngine(List<String> classPath) {
139+
autoAddJUnitPlatformArtifact(classPath, "junit-platform-engine");
140+
}
141+
142+
143+
private void autoAddJUnitPlatformArtifact(List<String> classPath, String artifactId) {
129144
List<Artifact> junitDependencies = this.mojo.getProject().getArtifacts().stream()
130145
.filter(a -> a.getGroupId().equals("org.junit.platform"))
131146
.collect(Collectors.toList());
132147

133-
// If the launcher has been manually added to the dependencies, there is nothing to do
134-
if (junitDependencies.stream().anyMatch(a -> a.getArtifactId().equals("junit-platform-launcher"))) {
148+
// If the artifact has been manually added to the dependencies, there is nothing to do
149+
if (junitDependencies.stream().anyMatch(a -> a.getArtifactId().equals(artifactId))) {
135150
return;
136151
}
137152

@@ -144,24 +159,67 @@ private void autoAddJUnitPlatform(List<String> classPath) {
144159
// Look for platform engine or platform commons on classpath
145160
Artifact toMatch = maybeJUnitPlatform.get();
146161

147-
// Assume that platform launcher has been released with same version number as engine and commons
148-
DefaultArtifact platformLauncher = new DefaultArtifact(toMatch.getGroupId(), "junit-platform-launcher", "jar",
162+
// Assume that artifact has been released with same version number as engine and commons
163+
DefaultArtifact platformLauncher = new DefaultArtifact(toMatch.getGroupId(), artifactId, "jar",
149164
toMatch.getVersion());
150165

166+
addArtifact(classPath, platformLauncher);
167+
}
168+
169+
private void autoAddJupiterEngine(List<String> classPath) {
170+
List<Artifact> junitDependencies = this.mojo.getProject().getArtifacts().stream()
171+
.filter(a -> a.getGroupId().equals("org.junit.jupiter"))
172+
.collect(Collectors.toList());
173+
174+
// If the engine has been manually added to the dependencies, there is nothing to do
175+
if (junitDependencies.stream().anyMatch(a -> a.getArtifactId().equals("junit-jupiter-engine"))) {
176+
return;
177+
}
178+
179+
Optional<Artifact> maybeApi = this.mojo.getProject().getArtifacts().stream()
180+
.filter(a -> a.getArtifactId().equals("junit-jupiter-api"))
181+
.findFirst();
182+
183+
if (maybeApi.isEmpty()) {
184+
return;
185+
}
186+
187+
// Look for platform engine or platform commons on classpath
188+
Artifact toMatch = maybeApi.get();
189+
190+
// Assume that engine has been released with same version number as api
191+
DefaultArtifact platformLauncher = new DefaultArtifact(toMatch.getGroupId(), "junit-jupiter-engine", "jar",
192+
toMatch.getVersion());
193+
194+
addArtifact(classPath, platformLauncher);
195+
196+
}
197+
198+
private void addArtifact(List<String> classPath, DefaultArtifact platformLauncher) {
151199
try {
200+
152201
ArtifactRequest r = new ArtifactRequest();
153202
r.setArtifact(platformLauncher);
154-
203+
155204
r.setRepositories(this.mojo.getProject().getRemotePluginRepositories());
156205
ArtifactResult resolved = this.mojo.repositorySystem().resolveArtifact(mojo.session().getRepositorySession(), r);
157206

158207
this.log.info("Auto adding " + resolved + " to classpath.");
159208
classPath.add(resolved.getArtifact().getFile().getAbsolutePath());
160-
} catch (ArtifactResolutionException e) {
209+
210+
// get any transitive dependencies,
211+
// although this doesn't seem to be neccesary for current releases of junit
212+
DependencyRequest dependencyRequest = new DependencyRequest(new DefaultDependencyNode(platformLauncher), null);
213+
DependencyResult transitive = this.mojo.repositorySystem().resolveDependencies(mojo.session().getRepositorySession(), dependencyRequest);
214+
for (ArtifactResult result : transitive.getArtifactResults()) {
215+
this.log.info("Auto adding " + result + " to classpath.");
216+
classPath.add(result.getArtifact().getFile().getAbsolutePath());
217+
}
218+
219+
} catch (DependencyResolutionException | ArtifactResolutionException e) {
161220
this.log.error("Could not resolve " + platformLauncher);
162221
throw new RuntimeException(e);
163222
}
164-
165223
}
166224

167225
private static Optional<Artifact> findJUnitArtifact(List<Artifact> junitDependencies) {
@@ -460,8 +518,9 @@ private Collection<String> findOccupiedTestPackages() {
460518
}
461519

462520
private Collection<Artifact> filteredDependencies() {
463-
return FCollection.filter(this.mojo.getPluginArtifactMap().values(),
464-
this.dependencyFilter);
521+
return this.mojo.getPluginArtifactMap().values().stream()
522+
.filter(this.dependencyFilter)
523+
.collect(Collectors.toList());
465524
}
466525

467526
private Collection<String> determineMutators() {

0 commit comments

Comments
 (0)