Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support junit-platform-runner and junit-platform-suite-engine in plugin dependencies #484

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2385,15 +2385,16 @@ private Artifact getJunitDepArtifact()

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

Artifact artifact = getPluginArtifactMap().get( "org.junit.platform:junit-platform-engine" );
if ( artifact == null )
{
return getProjectArtifactMap().get( "org.junit.platform:junit-platform-commons" );
artifact = getProjectArtifactMap().get( "org.junit.platform:junit-platform-commons" );
}

return artifact;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,54 @@ public void shouldExistTmpDirectory() throws IOException
.isDirectory();
}

@Test
public void shouldNotBeJunit5ArtifactInPluginDeps() throws Exception
{
Artifact testClasspathJupiter = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

Artifact testClasspathPlatformEng = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

setProjectDepedenciesToMojo( testClasspathJupiter, testClasspathPlatformEng, testClasspathCommons );

Artifact pluginDep1 = new DefaultArtifact( "org.junit.platform", "junit-platform-runner",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

Artifact pluginDep2 = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

addPluginDependencies( pluginDep1, pluginDep2 );

Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
assertThat( junitPlatformArtifact ).isNull();
}

@Test
public void shouldNotBeJunit5ArtifactInProjectDeps() throws Exception
{
Artifact testClasspathJupiter = new DefaultArtifact( "org.junit.jupiter", "junit-jupiter-engine",
createFromVersion( "5.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

Artifact testClasspathPlatformEng = new DefaultArtifact( "org.junit.platform", "junit-platform-engine",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

Artifact testClasspathCommons = new DefaultArtifact( "org.junit.platform", "junit-platform-commons",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

Artifact testClasspathRunner = new DefaultArtifact( "org.junit.platform", "junit-platform-runner",
createFromVersion( "1.4.0" ), null, "jar", null, mock( ArtifactHandler.class ) );

setProjectDepedenciesToMojo( testClasspathJupiter, testClasspathPlatformEng, testClasspathCommons,
testClasspathRunner );

Artifact junitPlatformArtifact = invokeMethod( mojo, "getJUnit5Artifact" );
assertThat( junitPlatformArtifact ).isNull();
}

@Test
public void shouldSmartlyResolveJUnit5ProviderWithJUnit4() throws Exception
{
Expand Down
47 changes: 47 additions & 0 deletions maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,53 @@ Using JUnit 5 Platform
</dependencies>
+---+

** JUnit5 Suite

For more information see this
{{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/junit5-suite}example}}.

+---+
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>JUnit5Tests</include>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I afraid that users can simply comply such configurations .... and will be surprised

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't be surprised! What is the point of Junit5 Suite? It's the same point that the Junit4 suite has!
The Point is to start the suite class which is a similar case is in your Surefire project. The suite children are executed by selecting JUnit5Tests and not by surefire provider and that the reason you have to select it. You must select the suite and not the children. Why we do not use the parameter test? Because there is a significant difference between includes and test. The test is a filter performing the selection inside of the JUnit which cannot be used here because this way you are not able to select the root class JUnit5Tests and you filter out all children. The includes does exactly what we want, it does not perform filtering inside of JUnit, it performs selection of classes that you send to the JUnit framework to execute it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

such explanation should be put in example,
what inclusion is for and what value should contains.

</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like next complicated ... why dependency in project and plugin
Even if we we support it in code for some reason we shouldn't give such examples ... project dependency should be enough.

Maybe we should also link to JUnit documentation.
https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine

Copy link
Contributor Author

@Tibor17 Tibor17 Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there are more alternatives. So the JUnit5 team separated API from impl and they always stated that the user does not need to access the impl of the engine.
If you like I put engines to the dependencies. Both would work. It's just matter of taste.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO the users don;t care if they have an access to the engine internals. So I wil use only project deps. thx

Copy link
Member

@olamy olamy Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with @slawekjaranowski such extra configuration complexity will be confusing for users. and if given as an example in the users documentation the users will simply copy/paste this over complicated configuration whereas they might not need it.

+---+

* Provider Selection

If nothing is configured, Surefire detects which JUnit version to use by the following algorithm:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ public static Iterable<Object[]> artifactVersions()
{
ArrayList<Object[]> args = new ArrayList<>();
args.add( new Object[] {"1.0.3", "5.0.3", "1.0.0", "1.0.0"} );
args.add( new Object[] {"1.1.1", "5.1.1", "1.0.0", "1.0.0"} );
args.add( new Object[] {"1.2.0", "5.2.0", "1.1.0", "1.0.0"} );
//args.add( new Object[] {"1.1.1", "5.1.1", "1.0.0", "1.0.0"} );
//args.add( new Object[] {"1.2.0", "5.2.0", "1.1.0", "1.0.0"} );
args.add( new Object[] {"1.3.2", "5.3.2", "1.1.1", "1.0.0"} );
args.add( new Object[] {"1.4.2", "5.4.2", "1.1.1", "1.0.0"} );
args.add( new Object[] {"1.5.2", "5.5.2", "1.2.0", "1.1.0"} );
//args.add( new Object[] {"1.4.2", "5.4.2", "1.1.1", "1.0.0"} );
//args.add( new Object[] {"1.5.2", "5.5.2", "1.2.0", "1.1.0"} );
args.add( new Object[] {"1.6.2", "5.6.2", "1.2.0", "1.1.0"} );
//args.add( new Object[] { "1.6.0-SNAPSHOT", "5.6.0-SNAPSHOT", "1.2.0", "1.1.0" } );
//args.add( new Object[] {"1.7.2", "5.7.2", "1.2.0", "1.1.0"} );
args.add( new Object[] {"1.8.2", "5.8.2", "1.2.0", "1.1.2"} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code should be removed ... or commented why and when we want to restore it.

Copy link
Contributor Author

@Tibor17 Tibor17 Mar 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My motivation was to remove some config because the build takes long with every next config, and these configs have been tested already long enough in our tests, and the new ones have not been tested yet.

return args;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,15 @@ public void junit4Runner()
.verifyTextInLog( "Running pkg.JUnit5Tests" )
.verifyTextInLog( "Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider" );
}

@Test
public void junit5Suite()
{
unpack( "junit5-suite" )
.executeTest()
.verifyErrorFree( 1 )
.verifyTextInLog( "Running pkg.JUnit5Test" )
.verifyTextInLog(
"Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider" );
}
}
4 changes: 2 additions & 2 deletions surefire-its/src/test/resources/junit5-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.6.2</version>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
80 changes: 80 additions & 0 deletions surefire-its/src/test/resources/junit5-suite/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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>junit5-suite</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-api</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<includes>
<include>JUnit5Tests</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does should be the same version as for api?
the same for suite-api and suite-engine

</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>

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

/*
* 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.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({pkg.domain.AxTest.class})
public class JUnit5Tests
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pkg.domain;

/*
* 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.junit.jupiter.api.Test;

public class AxTest
{
@Test
void test()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pkg.domain;

/*
* 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.junit.jupiter.api.Test;

public class BxTest
{
@Test
void test()
{
}
}