Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 979508a

Browse files
committed
Remove JUnit 4 dependency from all modules except spring-test
This commit removes the JUnit 4 dependency from all modules except spring-test which provides explicit JUnit 4 support. This commit also includes the following. - migration from JUnit 4 assertions to JUnit Jupiter assertions in all Kotlin tests - migration from JUnit 4 assumptions in Spring's TestGroup support to JUnit Jupiter assumptions, based on org.opentest4j.TestAbortedException - introduction of a new TestGroups utility class than can be used from existing JUnit 4 tests in the spring-test module in order to perform assumptions using JUnit 4's Assume class See spring-projectsgh-23451
1 parent 3f3e419 commit 979508a

File tree

43 files changed

+159
-111
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+159
-111
lines changed

build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ configure(allprojects) { project ->
138138
dependencies {
139139
testCompile("org.junit.jupiter:junit-jupiter-api")
140140
testCompile("org.junit.jupiter:junit-jupiter-params")
141-
testCompile("junit:junit:4.13-beta-3") {
142-
exclude group: "org.hamcrest", module: "hamcrest-core"
143-
}
144141
testCompile("org.mockito:mockito-core:3.0.0") {
145142
exclude group: "org.hamcrest", module: "hamcrest-core"
146143
}
@@ -151,7 +148,6 @@ configure(allprojects) { project ->
151148
// Pull in the latest JUnit 5 Launcher API to ensure proper support in IDEs.
152149
testRuntime("org.junit.platform:junit-platform-launcher")
153150
testRuntime("org.junit.jupiter:junit-jupiter-engine")
154-
testRuntime("org.junit.vintage:junit-vintage-engine")
155151
testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
156152
testRuntime("org.apache.logging.log4j:log4j-slf4j-impl:${log4jVersion}")
157153
testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")

spring-beans/src/test/java/org/springframework/beans/factory/serviceloader/ServiceLoaderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.springframework.beans.factory.support.RootBeanDefinition;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29-
import static org.junit.Assume.assumeTrue;
29+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3030

3131
/**
3232
* @author Juergen Hoeller

spring-beans/src/test/kotlin/org/springframework/beans/KotlinBeanUtilsTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.beans
1818

19-
import org.junit.Assert.*
19+
import org.junit.jupiter.api.Assertions.*
2020
import org.junit.jupiter.api.Test
2121

2222
/**

spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory
2222
import org.springframework.beans.factory.support.RootBeanDefinition
2323
import org.springframework.tests.sample.beans.TestBean
2424

25-
import org.junit.Assert.*
25+
import org.junit.jupiter.api.Assertions.*
26+
import org.junit.jupiter.api.fail
2627
import org.springframework.beans.factory.BeanCreationException
2728
import org.springframework.tests.sample.beans.Colour
2829

spring-context/src/test/kotlin/org/springframework/context/annotation/AnnotationConfigApplicationContextExtensionsTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.context.annotation
1818

19-
import org.junit.Assert.assertNotNull
19+
import org.junit.jupiter.api.Assertions.assertNotNull
2020
import org.junit.jupiter.api.Test
2121
import org.springframework.beans.factory.getBean
2222
import org.springframework.context.support.registerBean

spring-context/src/test/kotlin/org/springframework/context/annotation/KotlinConfigurationClassTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.context.annotation
1818

19-
import org.junit.Assert.*
19+
import org.junit.jupiter.api.Assertions.*
20+
import org.junit.jupiter.api.fail
2021
import org.junit.jupiter.api.Test
2122
import org.springframework.beans.factory.getBean
2223
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException

spring-context/src/test/kotlin/org/springframework/context/annotation/Spr16022Tests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.context.annotation
1818

19-
import org.junit.Assert.assertEquals
19+
import org.junit.jupiter.api.Assertions.assertEquals
2020
import org.junit.jupiter.api.Test
2121
import org.springframework.beans.factory.BeanFactory
2222
import org.springframework.beans.factory.getBean

spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.context.support
1818

19-
import org.junit.Assert.*
19+
import org.junit.jupiter.api.Assertions.assertEquals
20+
import org.junit.jupiter.api.Assertions.assertNotNull
21+
import org.junit.jupiter.api.Assertions.assertTrue
22+
import org.junit.jupiter.api.fail
2023
import org.junit.jupiter.api.Test
2124
import org.springframework.beans.factory.NoSuchBeanDefinitionException
2225
import org.springframework.beans.factory.getBean
@@ -28,7 +31,7 @@ import java.util.stream.Collectors
2831

2932
@Suppress("UNUSED_EXPRESSION")
3033
class BeanDefinitionDslTests {
31-
34+
3235
@Test
3336
fun `Declare beans with the functional Kotlin DSL`() {
3437
val beans = beans {
@@ -193,12 +196,12 @@ class BeanDefinitionDslTests {
193196

194197
try {
195198
context.getBean<Foo>()
196-
fail()
199+
fail("should have thrown an Exception")
197200
} catch (ignored: Exception) {
198201
}
199202
try {
200203
context.getBean<FooFoo>()
201-
fail()
204+
fail("should have thrown an Exception")
202205
} catch (ignored: Exception) {
203206
}
204207
}

spring-context/src/test/kotlin/org/springframework/context/support/GenericApplicationContextExtensionsTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.context.support
1818

19-
import org.junit.Assert.assertNotNull
19+
import org.junit.jupiter.api.Assertions.assertNotNull
2020
import org.junit.jupiter.api.Test
2121
import org.springframework.beans.factory.getBean
2222

spring-context/src/test/kotlin/org/springframework/ui/ModelExtensionsTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.ui
1818

19-
import org.junit.Assert.assertEquals
20-
import org.junit.Assert.assertTrue
19+
import org.junit.jupiter.api.Assertions.assertEquals
20+
import org.junit.jupiter.api.Assertions.assertTrue
2121
import org.junit.jupiter.api.Test
2222

2323
/**

spring-context/src/test/kotlin/org/springframework/ui/ModelMapExtensionsTests.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.ui
1818

19-
import org.junit.Assert.assertEquals
20-
import org.junit.Assert.assertTrue
19+
import org.junit.jupiter.api.Assertions.assertEquals
20+
import org.junit.jupiter.api.Assertions.assertTrue
2121
import org.junit.jupiter.api.Test
2222

2323
/**

spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import java.nio.file.Paths;
2828
import java.util.HashSet;
2929

30-
import org.junit.Ignore;
30+
import org.junit.jupiter.api.Disabled;
3131
import org.junit.jupiter.api.Test;
3232

3333
import org.springframework.util.FileCopyUtils;
@@ -218,7 +218,8 @@ public void testUrlResourceWithRelativePath() throws IOException {
218218
assertThat(relative).isEqualTo(new UrlResource("file:dir/subdir"));
219219
}
220220

221-
@Ignore @Test // this test is quite slow. TODO: re-enable with JUnit categories
221+
@Disabled
222+
@Test // this test is quite slow. TODO: re-enable with JUnit categories
222223
public void testNonFileResourceExists() throws Exception {
223224
Resource resource = new UrlResource("https://www.springframework.org");
224225
assertThat(resource.exists()).isTrue();

spring-core/src/test/java/org/springframework/tests/Assume.java

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import java.util.Set;
2020

2121
import org.apache.commons.logging.Log;
22-
import org.junit.AssumptionViolatedException;
2322

24-
import static org.junit.Assume.assumeFalse;
23+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
24+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2525

2626
/**
27-
* Provides utility methods that allow JUnit tests to {@link org.junit.Assume} certain
28-
* conditions hold {@code true}. If the assumption fails, it means the test should be
29-
* skipped.
27+
* Provides utility methods that allow JUnit tests to assume certain conditions
28+
* hold {@code true}. If the assumption fails, it means the test should be
29+
* aborted.
3030
*
3131
* <p>Tests can be categorized into {@link TestGroup}s. Active groups are enabled using
3232
* the 'testGroups' system property, usually activated from the gradle command line:
@@ -35,74 +35,59 @@
3535
* gradle test -PtestGroups="performance"
3636
* </pre>
3737
*
38-
* <p>Groups can be specified as a comma separated list of values, or using the pseudo group
39-
* 'all'. See {@link TestGroup} for a list of valid groups.
38+
* <p>Groups can be activated as a comma separated list of values, or using the
39+
* pseudo group 'all'. See {@link TestGroup} for a list of valid groups.
4040
*
4141
* @author Rob Winch
4242
* @author Phillip Webb
4343
* @author Sam Brannen
4444
* @since 3.2
4545
* @see #group(TestGroup)
4646
* @see #group(TestGroup, Executable)
47+
* @see TestGroup
48+
* @see TestGroups
4749
*/
4850
public abstract class Assume {
4951

5052
static final String TEST_GROUPS_SYSTEM_PROPERTY = "testGroups";
5153

5254

5355
/**
54-
* Assume that a particular {@link TestGroup} has been specified.
55-
* @param group the group that must be specified
56-
* @throws AssumptionViolatedException if the assumption fails
56+
* Assume that a particular {@link TestGroup} is active.
57+
* @param group the group that must be active
58+
* @throws org.opentest4j.TestAbortedException if the assumption fails
5759
*/
5860
public static void group(TestGroup group) {
59-
Set<TestGroup> testGroups = loadTestGroups();
60-
if (!testGroups.contains(group)) {
61-
throw new AssumptionViolatedException("Requires unspecified group " + group + " from " + testGroups);
62-
}
61+
Set<TestGroup> testGroups = TestGroups.loadTestGroups();
62+
assumeTrue(testGroups.contains(group),
63+
() -> "Requires inactive test group " + group + "; active test groups: " + testGroups);
6364
}
6465

6566
/**
66-
* Assume that a particular {@link TestGroup} has been specified before
67-
* executing the supplied {@link Executable}.
67+
* Assume that a particular {@link TestGroup} is active before executing the
68+
* supplied {@link Executable}.
6869
* <p>If the assumption fails, the executable will not be executed, but
69-
* no {@link AssumptionViolatedException} will be thrown.
70-
* @param group the group that must be specified
70+
* no {@link org.opentest4j.TestAbortedException} will be thrown.
71+
* @param group the group that must be active
7172
* @param executable the executable to execute if the test group is active
7273
* @since 4.2
7374
*/
7475
public static void group(TestGroup group, Executable executable) throws Exception {
75-
Set<TestGroup> testGroups = loadTestGroups();
76-
if (testGroups.contains(group)) {
76+
if (TestGroups.loadTestGroups().contains(group)) {
7777
executable.execute();
7878
}
7979
}
8080

8181
/**
8282
* Assume that the specified log is not set to Trace or Debug.
8383
* @param log the log to test
84-
* @throws AssumptionViolatedException if the assumption fails
84+
* @throws org.opentest4j.TestAbortedException if the assumption fails
8585
*/
8686
public static void notLogging(Log log) {
8787
assumeFalse(log.isTraceEnabled());
8888
assumeFalse(log.isDebugEnabled());
8989
}
9090

91-
/**
92-
* Load test groups dynamically instead of during static
93-
* initialization in order to avoid a {@link NoClassDefFoundError}
94-
* being thrown while attempting to load the {@code Assume} class.
95-
*/
96-
private static Set<TestGroup> loadTestGroups() {
97-
try {
98-
return TestGroup.parse(System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY));
99-
}
100-
catch (Exception ex) {
101-
throw new IllegalStateException("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY
102-
+ "' system property: " + ex.getMessage(), ex);
103-
}
104-
}
105-
10691

10792
/**
10893
* @since 4.2

spring-core/src/test/java/org/springframework/tests/AssumeTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import java.util.Arrays;
2020

21-
import org.junit.AssumptionViolatedException;
2221
import org.junit.jupiter.api.AfterEach;
2322
import org.junit.jupiter.api.BeforeEach;
2423
import org.junit.jupiter.api.Test;
24+
import org.opentest4j.TestAbortedException;
2525

2626
import static java.util.stream.Collectors.joining;
2727
import static org.assertj.core.api.Assertions.assertThat;
@@ -77,7 +77,7 @@ public void assumeGroupWithMatchingActiveTestGroup() {
7777
try {
7878
Assume.group(LONG_RUNNING);
7979
}
80-
catch (AssumptionViolatedException ex) {
80+
catch (TestAbortedException ex) {
8181
fail("assumption should NOT have failed");
8282
}
8383
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2002-2019 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+
* https://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.tests;
18+
19+
import java.util.Set;
20+
21+
/**
22+
* Utility methods for working with {@link TestGroup}s.
23+
*
24+
* @author Sam Brannen
25+
* @author Rob Winch
26+
* @author Phillip Webb
27+
* @since 5.2
28+
*/
29+
public abstract class TestGroups {
30+
31+
static final String TEST_GROUPS_SYSTEM_PROPERTY = "testGroups";
32+
33+
34+
/**
35+
* Determine if the provided {@link TestGroup} is active.
36+
* @param group the group to check
37+
* @since 5.2
38+
*/
39+
public static boolean isGroupActive(TestGroup group) {
40+
return loadTestGroups().contains(group);
41+
}
42+
43+
/**
44+
* Load test groups dynamically instead of during static initialization in
45+
* order to avoid a {@link NoClassDefFoundError} being thrown while attempting
46+
* to load the {@link Assume} class.
47+
*/
48+
static Set<TestGroup> loadTestGroups() {
49+
try {
50+
return TestGroup.parse(System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY));
51+
}
52+
catch (Exception ex) {
53+
throw new IllegalStateException("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY
54+
+ "' system property: " + ex.getMessage(), ex);
55+
}
56+
}
57+
58+
}

spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.core
1818

19-
import org.junit.Assert.assertEquals
19+
import org.junit.jupiter.api.Assertions.assertEquals
2020
import org.junit.jupiter.api.Test
2121

2222
class KotlinDefaultParameterNameDiscovererTests {

spring-core/src/test/kotlin/org/springframework/core/KotlinGenericTypeResolverTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.core
1818

19-
import org.junit.Assert.assertEquals
19+
import org.junit.jupiter.api.Assertions.assertEquals
2020
import org.junit.jupiter.api.Test
2121
import org.springframework.core.GenericTypeResolver.resolveReturnTypeArgument
2222
import java.lang.reflect.Method

spring-core/src/test/kotlin/org/springframework/core/KotlinMethodParameterTests.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.core
1818

19-
import org.junit.Assert.assertEquals
20-
import org.junit.Assert.assertFalse
21-
import org.junit.Assert.assertTrue
19+
import org.junit.jupiter.api.Assertions.assertEquals
20+
import org.junit.jupiter.api.Assertions.assertFalse
21+
import org.junit.jupiter.api.Assertions.assertTrue
2222
import org.junit.jupiter.api.Test
2323
import java.lang.reflect.Method
2424
import java.lang.reflect.TypeVariable

0 commit comments

Comments
 (0)