Skip to content

Commit

Permalink
[SUREFIRE-2036] Fix JUnit 5 with configured provider
Browse files Browse the repository at this point in the history
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
  • Loading branch information
roxspring authored and slawekjaranowski committed Mar 11, 2022
1 parent f495d62 commit 68bca29
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ protected List<ProviderInfo> createProviders( TestClassPath testClasspath )
{
Artifact junitDepArtifact = getJunitDepArtifact();
return providerDetector.resolve( new DynamicProviderInfo( null ),
new JUnitPlatformProviderInfo( getJUnit5Artifact(), testClasspath ),
new JUnitPlatformProviderInfo( getJUnitPlatformRunnerArtifact(), getJUnit5Artifact(), testClasspath ),
new TestNgProviderInfo( getTestNgArtifact() ),
new JUnitCoreProviderInfo( getJunitArtifact(), junitDepArtifact ),
new JUnit4ProviderInfo( getJunitArtifact(), junitDepArtifact ),
Expand Down Expand Up @@ -2413,13 +2413,13 @@ private Artifact getJunitDepArtifact()
return getProjectArtifactMap().get( "junit:junit-dep" );
}

private Artifact getJUnit5Artifact()
private Artifact getJUnitPlatformRunnerArtifact()
{
if ( getProjectArtifactMap().get( "org.junit.platform:junit-platform-runner" ) != null )
{
return null;
}
return getProjectArtifactMap().get( "org.junit.platform:junit-platform-runner" );
}

private Artifact getJUnit5Artifact()
{
Artifact artifact = getPluginArtifactMap().get( "org.junit.platform:junit-platform-engine" );
if ( artifact == null )
{
Expand Down Expand Up @@ -3252,11 +3252,14 @@ final class JUnitPlatformProviderInfo
private static final String PROVIDER_DEP_GID = "org.junit.platform";
private static final String PROVIDER_DEP_AID = "junit-platform-launcher";

private final Artifact junitPlatformRunnerArtifact;
private final Artifact junitPlatformArtifact;
private final TestClassPath testClasspath;

JUnitPlatformProviderInfo( Artifact junitPlatformArtifact, @Nonnull TestClassPath testClasspath )
JUnitPlatformProviderInfo( Artifact junitPlatformRunnerArtifact, Artifact junitPlatformArtifact,
@Nonnull TestClassPath testClasspath )
{
this.junitPlatformRunnerArtifact = junitPlatformRunnerArtifact;
this.junitPlatformArtifact = junitPlatformArtifact;
this.testClasspath = testClasspath;
}
Expand All @@ -3271,7 +3274,7 @@ public String getProviderName()
@Override
public boolean isApplicable()
{
return junitPlatformArtifact != null;
return junitPlatformRunnerArtifact == null && junitPlatformArtifact != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ ProviderInfo newTestNgProviderInfo()

ProviderInfo newJUnitPlatformProviderInfo()
{
return new JUnitPlatformProviderInfo( null, null );
return new JUnitPlatformProviderInfo( null, null, null );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2004,7 +2004,7 @@ public static class Mojo
private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact junitPlatformArtifact,
TestClassPath testClasspathWrapper )
{
return new JUnitPlatformProviderInfo( junitPlatformArtifact, testClasspathWrapper );
return new JUnitPlatformProviderInfo( null, junitPlatformArtifact, testClasspathWrapper );
}

void setProjectTestArtifacts( List<Artifact> projectTestArtifacts )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.apache.maven.surefire.its.jiras;

/*
* 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.
*/

import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
import org.junit.Test;

/**
* Integration Tests for SUREFIRE-2036
*/
@SuppressWarnings( "checkstyle:magicnumber" )
public class Surefire2036IT extends SurefireJUnit4IntegrationTestCase
{
@Test
public void selectJUnit5UsingConfiguredProviderWithPlatformRunner()
{
unpack( "surefire-2036" )
.executeTest()
.verifyErrorFree( 1 )
.verifyTextInLog( "Running pkg.JUnit5Test" )
.verifyTextInLog(
"Using configured provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider" );
}
}
68 changes: 68 additions & 0 deletions surefire-its/src/test/resources/surefire-2036/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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.example</groupId>
<artifactId>surefire-2036</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>${java.specification.version}</maven.compiler.source>
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.6.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pkg;

import org.junit.Test;

public class JUnit4Test {
@Test
public void test() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pkg;

import org.junit.jupiter.api.Test;

class JUnit5Test {
@Test
public void test() {

}
}

0 comments on commit 68bca29

Please sign in to comment.