Skip to content

Commit 68bca29

Browse files
roxspringslawekjaranowski
authored andcommittedMar 11, 2022
[SUREFIRE-2036] Fix JUnit 5 with configured provider
Modified getJUnit5Artifact() to care _only_ about JUnit 5 artifacts Added getJUnitPlatformRunnerArtifact() to care about the JUnit Platform Runner artifact JUnitPlatformProviderInfo is enabled by default based on a combination of above Avoids NullPointerException when explicitly configured provider needs to know the available JUnit5 artifact
1 parent f495d62 commit 68bca29

File tree

7 files changed

+142
-10
lines changed

7 files changed

+142
-10
lines changed
 

Diff for: ‎maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ protected List<ProviderInfo> createProviders( TestClassPath testClasspath )
11811181
{
11821182
Artifact junitDepArtifact = getJunitDepArtifact();
11831183
return providerDetector.resolve( new DynamicProviderInfo( null ),
1184-
new JUnitPlatformProviderInfo( getJUnit5Artifact(), testClasspath ),
1184+
new JUnitPlatformProviderInfo( getJUnitPlatformRunnerArtifact(), getJUnit5Artifact(), testClasspath ),
11851185
new TestNgProviderInfo( getTestNgArtifact() ),
11861186
new JUnitCoreProviderInfo( getJunitArtifact(), junitDepArtifact ),
11871187
new JUnit4ProviderInfo( getJunitArtifact(), junitDepArtifact ),
@@ -2413,13 +2413,13 @@ private Artifact getJunitDepArtifact()
24132413
return getProjectArtifactMap().get( "junit:junit-dep" );
24142414
}
24152415

2416-
private Artifact getJUnit5Artifact()
2416+
private Artifact getJUnitPlatformRunnerArtifact()
24172417
{
2418-
if ( getProjectArtifactMap().get( "org.junit.platform:junit-platform-runner" ) != null )
2419-
{
2420-
return null;
2421-
}
2418+
return getProjectArtifactMap().get( "org.junit.platform:junit-platform-runner" );
2419+
}
24222420

2421+
private Artifact getJUnit5Artifact()
2422+
{
24232423
Artifact artifact = getPluginArtifactMap().get( "org.junit.platform:junit-platform-engine" );
24242424
if ( artifact == null )
24252425
{
@@ -3252,11 +3252,14 @@ final class JUnitPlatformProviderInfo
32523252
private static final String PROVIDER_DEP_GID = "org.junit.platform";
32533253
private static final String PROVIDER_DEP_AID = "junit-platform-launcher";
32543254

3255+
private final Artifact junitPlatformRunnerArtifact;
32553256
private final Artifact junitPlatformArtifact;
32563257
private final TestClassPath testClasspath;
32573258

3258-
JUnitPlatformProviderInfo( Artifact junitPlatformArtifact, @Nonnull TestClassPath testClasspath )
3259+
JUnitPlatformProviderInfo( Artifact junitPlatformRunnerArtifact, Artifact junitPlatformArtifact,
3260+
@Nonnull TestClassPath testClasspath )
32593261
{
3262+
this.junitPlatformRunnerArtifact = junitPlatformRunnerArtifact;
32603263
this.junitPlatformArtifact = junitPlatformArtifact;
32613264
this.testClasspath = testClasspath;
32623265
}
@@ -3271,7 +3274,7 @@ public String getProviderName()
32713274
@Override
32723275
public boolean isApplicable()
32733276
{
3274-
return junitPlatformArtifact != null;
3277+
return junitPlatformRunnerArtifact == null && junitPlatformArtifact != null;
32753278
}
32763279

32773280
@Override

Diff for: ‎maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoJava7PlusTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ ProviderInfo newTestNgProviderInfo()
531531

532532
ProviderInfo newJUnitPlatformProviderInfo()
533533
{
534-
return new JUnitPlatformProviderInfo( null, null );
534+
return new JUnitPlatformProviderInfo( null, null, null );
535535
}
536536

537537
@Override

Diff for: ‎maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,7 @@ public static class Mojo
20042004
private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact junitPlatformArtifact,
20052005
TestClassPath testClasspathWrapper )
20062006
{
2007-
return new JUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
2007+
return new JUnitPlatformProviderInfo( null, junitPlatformArtifact, testClasspathWrapper );
20082008
}
20092009

20102010
void setProjectTestArtifacts( List<Artifact> projectTestArtifacts )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.apache.maven.surefire.its.jiras;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
23+
import org.junit.Test;
24+
25+
/**
26+
* Integration Tests for SUREFIRE-2036
27+
*/
28+
@SuppressWarnings( "checkstyle:magicnumber" )
29+
public class Surefire2036IT extends SurefireJUnit4IntegrationTestCase
30+
{
31+
@Test
32+
public void selectJUnit5UsingConfiguredProviderWithPlatformRunner()
33+
{
34+
unpack( "surefire-2036" )
35+
.executeTest()
36+
.verifyErrorFree( 1 )
37+
.verifyTextInLog( "Running pkg.JUnit5Test" )
38+
.verifyTextInLog(
39+
"Using configured provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider" );
40+
}
41+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one
4+
~ or more contributor license agreements. See the NOTICE file
5+
~ distributed with this work for additional information
6+
~ regarding copyright ownership. The ASF licenses this file
7+
~ to you under the Apache License, Version 2.0 (the
8+
~ "License"); you may not use this file except in compliance
9+
~ with the License. You may obtain a copy of the License at
10+
~
11+
~ http://www.apache.org/licenses/LICENSE-2.0
12+
~
13+
~ Unless required by applicable law or agreed to in writing,
14+
~ software distributed under the License is distributed on an
15+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
~ KIND, either express or implied. See the License for the
17+
~ specific language governing permissions and limitations
18+
~ under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
26+
<groupId>org.example</groupId>
27+
<artifactId>surefire-2036</artifactId>
28+
<version>1.0-SNAPSHOT</version>
29+
30+
<properties>
31+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
32+
<maven.compiler.source>${java.specification.version}</maven.compiler.source>
33+
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter-engine</artifactId>
40+
<version>5.6.2</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.junit.platform</groupId>
45+
<artifactId>junit-platform-runner</artifactId>
46+
<version>1.6.2</version>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-surefire-plugin</artifactId>
56+
<version>${surefire.version}</version>
57+
<dependencies>
58+
<dependency>
59+
<groupId>org.apache.maven.surefire</groupId>
60+
<artifactId>surefire-junit-platform</artifactId>
61+
<version>${surefire.version}</version>
62+
</dependency>
63+
</dependencies>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package pkg;
2+
3+
import org.junit.Test;
4+
5+
public class JUnit4Test {
6+
@Test
7+
public void test() {
8+
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package pkg;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class JUnit5Test {
6+
@Test
7+
public void test() {
8+
9+
}
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.