Skip to content

Commit 1f99455

Browse files
committed
[SUREFIRE-2040] No tests executed with junit-platform-suite and -Dtest=TestSuite
1 parent db99f16 commit 1f99455

File tree

8 files changed

+306
-6
lines changed

8 files changed

+306
-6
lines changed

maven-surefire-plugin/src/site/apt/examples/junit-platform.apt.vm

+37
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ Using JUnit 5 Platform
294294
<plugin>
295295
<groupId>org.apache.maven.plugins</groupId>
296296
<artifactId>maven-surefire-plugin</artifactId>
297+
<version>${project.version}</version>
297298
<dependencies>
298299
<dependency>
299300
<groupId>org.junit.jupiter</groupId>
@@ -334,6 +335,7 @@ Using JUnit 5 Platform
334335
<plugin>
335336
<groupId>org.apache.maven.plugins</groupId>
336337
<artifactId>maven-surefire-plugin</artifactId>
338+
<version>${project.version}</version>
337339
<dependencies>
338340
<dependency>
339341
<groupId>org.junit.vintage</groupId>
@@ -411,6 +413,41 @@ Using JUnit 5 Platform
411413
</dependencies>
412414
+---+
413415

416+
** JUnit5 Suite
417+
418+
For more information see this
419+
{{{https://github.com/apache/maven-surefire/tree/master/surefire-its/src/test/resources/junit5-suite}example with surefire integration test}}
420+
and the {{{https://junit.org/junit5/docs/current/user-guide/#junit-platform-suite-engine}tutorial}}.
421+
422+
+---+
423+
<dependencies>
424+
<dependency>
425+
<groupId>org.junit.jupiter</groupId>
426+
<artifactId>junit-jupiter-engine</artifactId>
427+
<version>5.8.2</version>
428+
<scope>test</scope>
429+
</dependency>
430+
<dependency>
431+
<groupId>org.junit.platform</groupId>
432+
<artifactId>junit-platform-suite-engine</artifactId>
433+
<version>1.8.2</version>
434+
<scope>test</scope>
435+
</dependency>
436+
</dependencies>
437+
<build>
438+
<plugins>
439+
<plugin>
440+
<groupId>org.apache.maven.plugins</groupId>
441+
<artifactId>maven-surefire-plugin</artifactId>
442+
<version>${project.version}</version>
443+
<configuration>
444+
<test><!-- your test suite class name should be here --></test>
445+
</configuration>
446+
</plugin>
447+
</plugins>
448+
</build>
449+
+---+
450+
414451
* Provider Selection
415452

416453
If nothing is configured, Surefire detects which JUnit version to use by the following algorithm:

surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1787JUnit5IT.java

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
2323
import org.junit.Test;
2424

25+
import static org.hamcrest.Matchers.containsString;
26+
import static org.hamcrest.Matchers.equalTo;
27+
2528
/**
2629
*
2730
*/
@@ -129,4 +132,17 @@ public void junit4Runner()
129132
.verifyTextInLog( "Running pkg.JUnit5Tests" )
130133
.verifyTextInLog( "Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider" );
131134
}
135+
136+
@Test
137+
public void junit5Suite() throws Exception
138+
{
139+
unpack( "junit5-suite" )
140+
.executeTest()
141+
.verifyErrorFree( 1 )
142+
.verifyTextInLog(
143+
"Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider" )
144+
.verifyTextInLog( "Running pkg.JUnit5Test" )
145+
.verifyTextInLog( "Running pkg.domain.AxTest" )
146+
.assertThatLogLine( containsString( "Running pkg.domain.BxTest" ), equalTo( 0 ) );
147+
}
132148
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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>junit5-suite</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-api</artifactId>
40+
<!--
41+
The engine uses version 5.8.2. Due to the JUnit5 modularity model splits api and implementation,
42+
the implementations should be backwards compatible with older versions of api, we are testing this corner case
43+
for internal Surefire requirements. The user may or may not split the api and impl and versions.
44+
-->
45+
<version>5.8.0</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.junit.platform</groupId>
50+
<artifactId>junit-platform-suite-api</artifactId>
51+
<!--
52+
The engine uses version 5.8.2. Due to the JUnit5 modularity model splits api and implementation,
53+
the implementations should be backwards compatible with old versions of api, we are testing this corner case
54+
for internal Surefire requirements. The user may or may not split the api and impl and versions.
55+
-->
56+
<version>1.8.0</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<pluginManagement>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-surefire-plugin</artifactId>
67+
<version>${surefire.version}</version>
68+
<configuration>
69+
<test>JUnit5Tests</test>
70+
</configuration>
71+
<dependencies>
72+
<!--
73+
You may split the impl from api as follows. It avoids a situation where your tests have
74+
direct access to the engine internals.
75+
As an example, the impl:5.9.0 should be backwards with api:1.8.0 and one limitation where you
76+
would not observe features introduced in 1.9.0 and 5.9.0.
77+
-->
78+
<dependency>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter-engine</artifactId>
81+
<version>5.8.2</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.junit.platform</groupId>
85+
<artifactId>junit-platform-suite</artifactId>
86+
<version>1.8.2</version>
87+
</dependency>
88+
</dependencies>
89+
</plugin>
90+
</plugins>
91+
</pluginManagement>
92+
</build>
93+
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package pkg;
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.junit.platform.suite.api.SelectClasses;
23+
import org.junit.platform.suite.api.Suite;
24+
25+
@Suite
26+
@SelectClasses({pkg.domain.AxTest.class})
27+
public class JUnit5Tests
28+
{
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pkg.domain;
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.junit.jupiter.api.Test;
23+
24+
public class AxTest
25+
{
26+
@Test
27+
void test()
28+
{
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package pkg.domain;
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.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.fail;
25+
26+
public class BxTest
27+
{
28+
@Test
29+
void test()
30+
{
31+
fail();
32+
}
33+
}

surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import static org.apache.maven.surefire.api.report.ConsoleOutputCapture.startCapture;
3333
import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
3434
import static org.apache.maven.surefire.api.report.RunMode.RERUN_TEST_AFTER_FAILURE;
35+
import static org.apache.maven.surefire.api.testset.TestListResolver.optionallyWildcardFilter;
3536
import static org.apache.maven.surefire.api.util.TestsToRun.fromClass;
3637
import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank;
3738
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
@@ -55,7 +56,6 @@
5556
import org.apache.maven.surefire.api.report.ReporterException;
5657
import org.apache.maven.surefire.api.report.ReporterFactory;
5758
import org.apache.maven.surefire.api.suite.RunResult;
58-
import org.apache.maven.surefire.api.testset.TestListResolver;
5959
import org.apache.maven.surefire.api.testset.TestSetFailedException;
6060
import org.apache.maven.surefire.api.util.ScanResult;
6161
import org.apache.maven.surefire.api.util.SurefireReflectionException;
@@ -275,11 +275,11 @@ private Filter<?>[] newFilters()
275275
.map( TagFilter::excludeTags )
276276
.ifPresent( filters::add );
277277

278-
TestListResolver testListResolver = parameters.getTestRequest().getTestListResolver();
279-
if ( !testListResolver.isEmpty() )
280-
{
281-
filters.add( new TestMethodFilter( testListResolver ) );
282-
}
278+
of( optionallyWildcardFilter( parameters.getTestRequest().getTestListResolver() ) )
279+
.filter( f -> !f.isEmpty() )
280+
.filter( f -> !f.isWildcard() )
281+
.map( TestMethodFilter::new )
282+
.ifPresent( filters::add );
283283

284284
getPropertiesList( INCLUDE_JUNIT5_ENGINES_PROP )
285285
.map( EngineFilter::includeEngines )

surefire-providers/surefire-junit-platform/src/test/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProviderTest.java

+61
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.apache.maven.surefire.api.report.RunMode.NORMAL_RUN;
3030
import static org.assertj.core.api.Assertions.assertThat;
3131
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertFalse;
3233
import static org.junit.jupiter.api.Assertions.assertNull;
3334
import static org.junit.jupiter.api.Assertions.assertThrows;
3435
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -43,6 +44,7 @@
4344
import static org.mockito.Mockito.verify;
4445
import static org.mockito.Mockito.when;
4546
import static org.mockito.Mockito.withSettings;
47+
import static org.powermock.reflect.Whitebox.getInternalState;
4648

4749
import java.io.PrintStream;
4850
import java.util.ArrayList;
@@ -891,6 +893,65 @@ public void parsesConfigurationParameters()
891893
assertEquals( "EOF", provider.getConfigurationParameters().get( "qux" ) );
892894
}
893895

896+
@Test
897+
public void shouldFilterTestMethod()
898+
{
899+
ProviderParameters providerParameters = providerParametersMock();
900+
TestListResolver testListResolver = new TestListResolver( "**/*Test#test*" );
901+
assertFalse( testListResolver.isEmpty() );
902+
assertFalse( testListResolver.isWildcard() );
903+
TestRequest request = new TestRequest( null, null, testListResolver, 0 );
904+
when( providerParameters.getTestRequest() ).thenReturn( request );
905+
906+
JUnitPlatformProvider provider = new JUnitPlatformProvider( providerParameters );
907+
908+
assertThat( provider.getFilters() )
909+
.hasSize( 1 );
910+
911+
assertThat( provider.getFilters()[0] )
912+
.isInstanceOf( TestMethodFilter.class );
913+
914+
Object expectedTestListResolver = getInternalState( provider.getFilters()[0], "testListResolver" );
915+
916+
assertThat( expectedTestListResolver )
917+
.isInstanceOf( TestListResolver.class );
918+
919+
assertThat( expectedTestListResolver )
920+
.isSameAs( testListResolver );
921+
}
922+
923+
@Test
924+
public void shouldNotFilterEmpty()
925+
{
926+
ProviderParameters providerParameters = providerParametersMock();
927+
TestListResolver testListResolver = new TestListResolver( "" );
928+
assertTrue( testListResolver.isEmpty() );
929+
assertFalse( testListResolver.isWildcard() );
930+
TestRequest request = new TestRequest( null, null, testListResolver, 0 );
931+
when( providerParameters.getTestRequest() ).thenReturn( request );
932+
933+
JUnitPlatformProvider provider = new JUnitPlatformProvider( providerParameters );
934+
935+
assertThat( provider.getFilters() )
936+
.isEmpty();
937+
}
938+
939+
@Test
940+
public void shouldNotFilterWildcard()
941+
{
942+
ProviderParameters providerParameters = providerParametersMock();
943+
TestListResolver testListResolver = new TestListResolver( "*.java" );
944+
assertTrue( testListResolver.isWildcard() );
945+
assertFalse( testListResolver.isEmpty() );
946+
TestRequest request = new TestRequest( null, null, testListResolver, 0 );
947+
when( providerParameters.getTestRequest() ).thenReturn( request );
948+
949+
JUnitPlatformProvider provider = new JUnitPlatformProvider( providerParameters );
950+
951+
assertThat( provider.getFilters() )
952+
.isEmpty();
953+
}
954+
894955
@Test
895956
public void executesSingleTestIncludedByName()
896957
throws Exception

0 commit comments

Comments
 (0)