Skip to content

Commit 56774ba

Browse files
committed
Use Gradle 4.6's built-in support for the JUnit Platform
Gradle 4.6 provides built-in support for the JUnit Platform within the standard `test` task. This commit configures a custom `testJUnitJupiter` test task for executing JUnit Jupiter tests directly on the JUnit Platform instead of indirectly on JUnit 4 via @RunWith(JUnitPlatform.class). This switch provides for better integration with Gradle's test reporting and paves the way for a possible transition to the JUnit Platform in the future. Issue: SPR-16672
1 parent aba882a commit 56774ba

24 files changed

+152
-107
lines changed

spring-test/spring-test.gradle

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,34 @@ task testNG(type: Test) {
103103
reports.junitXml.destination = file("$buildDir/test-results")
104104
}
105105

106+
task testJUnitJupiter(type: Test) {
107+
description = 'Runs JUnit Jupiter tests.'
108+
useJUnitPlatform {
109+
includeEngines 'junit-jupiter'
110+
excludeTags 'failing-test-case'
111+
}
112+
filter {
113+
includeTestsMatching "org.springframework.test.context.junit.jupiter.*"
114+
}
115+
reports.junitXml.destination = file("$buildDir/test-results")
116+
// Java Util Logging for the JUnit Platform.
117+
// systemProperty('java.util.logging.manager', 'org.apache.logging.log4j.jul.LogManager')
118+
}
119+
106120
test {
107121
description = 'Runs JUnit tests.'
108-
dependsOn testNG
122+
dependsOn testJUnitJupiter, testNG
109123
useJUnit()
110124
scanForTestClasses = false
111-
include(['**/*Tests.class', '**/*Test.class', '**/SpringJUnitJupiterTestSuite.class'])
112-
exclude(['**/testng/**/*.*'])
113-
// Java Util Logging for JUnit 5.
114-
// systemProperty('java.util.logging.manager', 'org.apache.logging.log4j.jul.LogManager')
125+
include(['**/*Tests.class', '**/*Test.class'])
126+
exclude(['**/testng/**/*.*', '**/jupiter/**/*.*'])
115127
reports.junitXml.destination = file("$buildDir/test-results")
116128
}
117129

118130
task aggregateTestReports(type: TestReport) {
119131
description = 'Aggregates JUnit and TestNG test reports.'
120132
destinationDir = test.reports.html.destination
121-
reportOn test, testNG
133+
reportOn test, testJUnitJupiter, testNG
122134
}
123135

124136
check.dependsOn aggregateTestReports

spring-test/src/test/java/org/springframework/test/context/junit/SpringJUnitJupiterTestSuite.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717
package org.springframework.test.context.junit;
1818

1919
import org.junit.platform.runner.JUnitPlatform;
20-
import org.junit.platform.suite.api.IncludeClassNamePatterns;
20+
import org.junit.platform.suite.api.ExcludeTags;
2121
import org.junit.platform.suite.api.IncludeEngines;
2222
import org.junit.platform.suite.api.SelectPackages;
2323
import org.junit.platform.suite.api.UseTechnicalNames;
@@ -48,7 +48,7 @@
4848
@RunWith(JUnitPlatform.class)
4949
@IncludeEngines("junit-jupiter")
5050
@SelectPackages("org.springframework.test.context.junit.jupiter")
51-
@IncludeClassNamePatterns("^.*TestCase$")
51+
@ExcludeTags("failing-test-case")
5252
@UseTechnicalNames
5353
public class SpringJUnitJupiterTestSuite {
5454
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,11 +45,11 @@
4545
* @since 5.0
4646
* @see SpringExtension
4747
* @see SpringJUnitConfig
48-
* @see SpringExtensionTestCase
48+
* @see SpringExtensionTests
4949
*/
5050
@SpringJUnitConfig(TestConfig.class)
5151
@DisplayName("@SpringJUnitConfig Tests")
52-
class ComposedSpringExtensionTestCase {
52+
class ComposedSpringExtensionTests {
5353

5454
@Autowired
5555
Person dilbert;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,17 +40,17 @@
4040

4141
/**
4242
* Tests for {@link DisabledIfCondition} that verify actual condition evaluation
43-
* results and exception handling; whereas, {@link DisabledIfTestCase} only tests
43+
* results and exception handling; whereas, {@link DisabledIfTests} only tests
4444
* the <em>happy paths</em>.
4545
*
4646
* <p>To run these tests in an IDE that does not have built-in support for the JUnit
4747
* Platform, simply run {@link SpringJUnitJupiterTestSuite} as a JUnit 4 test.
4848
*
4949
* @author Sam Brannen
5050
* @since 5.0
51-
* @see DisabledIfTestCase
51+
* @see DisabledIfTests
5252
*/
53-
class DisabledIfConditionTestCase {
53+
class DisabledIfConditionTests {
5454

5555
private final DisabledIfCondition condition = new DisabledIfCondition();
5656

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTestCase.java renamed to spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,16 +37,16 @@
3737
* @author Tadaya Tsuyukubo
3838
* @author Sam Brannen
3939
* @since 5.0
40-
* @see DisabledIfConditionTestCase
40+
* @see DisabledIfConditionTests
4141
* @see DisabledIf
4242
* @see SpringExtension
4343
*/
44-
class DisabledIfTestCase {
44+
class DisabledIfTests {
4545

4646
@SpringJUnitConfig(Config.class)
4747
@TestPropertySource(properties = "foo = true")
4848
@Nested
49-
class DisabledIfOnMethodTestCase {
49+
class DisabledIfOnMethodTests {
5050

5151
@Test
5252
@DisabledIf("true")
@@ -131,7 +131,7 @@ void disabledIfWithSpelStringTrueBean() {
131131
@SpringJUnitConfig(Config.class)
132132
@Nested
133133
@DisabledIf("true")
134-
class DisabledIfOnClassTestCase {
134+
class DisabledIfOnClassTests {
135135

136136
@Test
137137
void foo() {

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTestCase.java renamed to spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,16 +38,16 @@
3838
* @author Tadaya Tsuyukubo
3939
* @author Sam Brannen
4040
* @since 5.0
41-
* @see EnabledIfConditionTestCase
41+
* @see EnabledIfConditionTests
4242
* @see EnabledIf
4343
* @see SpringExtension
4444
*/
45-
class EnabledIfTestCase {
45+
class EnabledIfTests {
4646

4747
@SpringJUnitConfig(Config.class)
4848
@TestPropertySource(properties = "foo = false")
4949
@Nested
50-
class EnabledIfOnMethodTestCase {
50+
class EnabledIfOnMethodTests {
5151

5252
@Test
5353
@EnabledIf("false")
@@ -133,7 +133,7 @@ void enabledIfWithSpelStringFalseBean() {
133133
@SpringJUnitConfig(Config.class)
134134
@Nested
135135
@EnabledIf("false")
136-
class EnabledIfOnClassTestCase {
136+
class EnabledIfOnClassTests {
137137

138138
@Test
139139
void foo() {
Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@
7171
* @author Sam Brannen
7272
* @since 5.0
7373
*/
74-
class FailingBeforeAndAfterMethodsSpringExtensionTestCase {
74+
class FailingBeforeAndAfterMethodsSpringExtensionTests {
7575

7676
private static Stream<Class<?>> testClasses() {
7777
// @formatter:off
7878
return Stream.of(
79-
AlwaysFailingBeforeTestClassTestKase.class,
80-
AlwaysFailingAfterTestClassTestKase.class,
81-
AlwaysFailingPrepareTestInstanceTestKase.class,
82-
AlwaysFailingBeforeTestMethodTestKase.class,
83-
AlwaysFailingBeforeTestExecutionTestKase.class,
84-
AlwaysFailingAfterTestExecutionTestKase.class,
85-
AlwaysFailingAfterTestMethodTestKase.class,
86-
FailingBeforeTransactionTestKase.class,
87-
FailingAfterTransactionTestKase.class);
79+
AlwaysFailingBeforeTestClassTestCase.class,
80+
AlwaysFailingAfterTestClassTestCase.class,
81+
AlwaysFailingPrepareTestInstanceTestCase.class,
82+
AlwaysFailingBeforeTestMethodTestCase.class,
83+
AlwaysFailingBeforeTestExecutionTestCase.class,
84+
AlwaysFailingAfterTestExecutionTestCase.class,
85+
AlwaysFailingAfterTestMethodTestCase.class,
86+
FailingBeforeTransactionTestCase.class,
87+
FailingAfterTransactionTestCase.class);
8888
// @formatter:on
8989
}
9090

@@ -131,16 +131,16 @@ private void runTestAndAssertCounters(Class<?> testClass) {
131131
}
132132

133133
private int getExpectedStartedCount(Class<?> testClass) {
134-
return (testClass == AlwaysFailingBeforeTestClassTestKase.class ? 0 : 1);
134+
return (testClass == AlwaysFailingBeforeTestClassTestCase.class ? 0 : 1);
135135
}
136136

137137
private int getExpectedSucceededCount(Class<?> testClass) {
138-
return (testClass == AlwaysFailingAfterTestClassTestKase.class ? 1 : 0);
138+
return (testClass == AlwaysFailingAfterTestClassTestCase.class ? 1 : 0);
139139
}
140140

141141
private int getExpectedFailedCount(Class<?> testClass) {
142-
if (testClass == AlwaysFailingBeforeTestClassTestKase.class
143-
|| testClass == AlwaysFailingAfterTestClassTestKase.class) {
142+
if (testClass == AlwaysFailingBeforeTestClassTestCase.class
143+
|| testClass == AlwaysFailingAfterTestClassTestCase.class) {
144144
return 0;
145145
}
146146
return 1;
@@ -205,6 +205,7 @@ public void afterTestExecution(TestContext testContext) {
205205
}
206206
}
207207

208+
@FailingTestCase
208209
@ExtendWith(SpringExtension.class)
209210
private static abstract class BaseTestCase {
210211

@@ -214,44 +215,37 @@ void testNothing() {
214215
}
215216

216217
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
217-
// TestKase with a "K" so that it's not picked up by the suite or build
218-
static class AlwaysFailingBeforeTestClassTestKase extends BaseTestCase {
218+
static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
219219
}
220220

221221
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
222-
// TestKase with a "K" so that it's not picked up by the suite or build
223-
static class AlwaysFailingAfterTestClassTestKase extends BaseTestCase {
222+
static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
224223
}
225224

226225
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
227-
// TestKase with a "K" so that it's not picked up by the suite or build
228-
static class AlwaysFailingPrepareTestInstanceTestKase extends BaseTestCase {
226+
static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
229227
}
230228

231229
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
232-
// TestKase with a "K" so that it's not picked up by the suite or build
233-
static class AlwaysFailingBeforeTestMethodTestKase extends BaseTestCase {
230+
static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
234231
}
235232

236233
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
237-
// TestKase with a "K" so that it's not picked up by the suite or build
238-
static class AlwaysFailingBeforeTestExecutionTestKase extends BaseTestCase {
234+
static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
239235
}
240236

241237
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
242-
// TestKase with a "K" so that it's not picked up by the suite or build
243-
static class AlwaysFailingAfterTestExecutionTestKase extends BaseTestCase {
238+
static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
244239
}
245240

246241
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
247-
// TestKase with a "K" so that it's not picked up by the suite or build
248-
static class AlwaysFailingAfterTestMethodTestKase extends BaseTestCase {
242+
static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
249243
}
250244

245+
@FailingTestCase
251246
@SpringJUnitConfig(DatabaseConfig.class)
252247
@Transactional
253-
// TestKase with a "K" so that it's not picked up by the suite or build
254-
static class FailingBeforeTransactionTestKase {
248+
static class FailingBeforeTransactionTestCase {
255249

256250
@Test
257251
void testNothing() {
@@ -263,10 +257,10 @@ void beforeTransaction() {
263257
}
264258
}
265259

260+
@FailingTestCase
266261
@SpringJUnitConfig(DatabaseConfig.class)
267262
@Transactional
268-
// TestKase with a "K" so that it's not picked up by the suite or build
269-
static class FailingAfterTransactionTestKase {
263+
static class FailingAfterTransactionTestCase {
270264

271265
@Test
272266
void testNothing() {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit.jupiter;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.junit.jupiter.api.Tag;
26+
27+
/**
28+
* Custom annotation for tagging "fake" test cases which are supposed to fail
29+
* but are only intended to be used internally by "real" passing tests.
30+
*
31+
* @author Sam Brannen
32+
* @since 5.1
33+
*/
34+
@Target({ ElementType.TYPE, ElementType.METHOD })
35+
@Retention(RetentionPolicy.RUNTIME)
36+
@Documented
37+
@Tag("failing-test-case")
38+
public @interface FailingTestCase {
39+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@
4949
*
5050
* @author Sam Brannen
5151
* @since 5.1
52-
* @see SpringExtensionTestCase
52+
* @see SpringExtensionTests
5353
* @see SpringExtension
5454
* @see RegisterExtension
5555
*/
5656
@ContextConfiguration(classes = TestConfig.class)
5757
@TestPropertySource(properties = "enigma = 42")
58-
class RegisterExtensionSpringExtensionTestCase {
58+
class RegisterExtensionSpringExtensionTests {
5959

6060
@RegisterExtension
6161
static final SpringExtension springExtension = new SpringExtension();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@
4545
* @see ParameterizedTest
4646
*/
4747
@SpringJUnitConfig(TestConfig.class)
48-
class SpringExtensionParameterizedTestCase {
48+
class SpringExtensionParameterizedTests {
4949

5050
@ParameterizedTest
5151
@ValueSource(strings = { "Dilbert", "Wally" })

0 commit comments

Comments
 (0)