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 gh-23451
This commit is contained in:
Sam Brannen
2019-08-15 11:52:19 +02:00
parent 3f3e41923f
commit 979508a7f3
43 changed files with 159 additions and 111 deletions

View File

@@ -27,7 +27,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import org.junit.Ignore;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.util.FileCopyUtils;
@@ -218,7 +218,8 @@ public class ResourceTests {
assertThat(relative).isEqualTo(new UrlResource("file:dir/subdir"));
}
@Ignore @Test // this test is quite slow. TODO: re-enable with JUnit categories
@Disabled
@Test // this test is quite slow. TODO: re-enable with JUnit categories
public void testNonFileResourceExists() throws Exception {
Resource resource = new UrlResource("https://www.springframework.org");
assertThat(resource.exists()).isTrue();

View File

@@ -19,14 +19,14 @@ package org.springframework.tests;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.junit.AssumptionViolatedException;
import static org.junit.Assume.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
/**
* Provides utility methods that allow JUnit tests to {@link org.junit.Assume} certain
* conditions hold {@code true}. If the assumption fails, it means the test should be
* skipped.
* Provides utility methods that allow JUnit tests to assume certain conditions
* hold {@code true}. If the assumption fails, it means the test should be
* aborted.
*
* <p>Tests can be categorized into {@link TestGroup}s. Active groups are enabled using
* the 'testGroups' system property, usually activated from the gradle command line:
@@ -35,8 +35,8 @@ import static org.junit.Assume.assumeFalse;
* gradle test -PtestGroups="performance"
* </pre>
*
* <p>Groups can be specified as a comma separated list of values, or using the pseudo group
* 'all'. See {@link TestGroup} for a list of valid groups.
* <p>Groups can be activated as a comma separated list of values, or using the
* pseudo group 'all'. See {@link TestGroup} for a list of valid groups.
*
* @author Rob Winch
* @author Phillip Webb
@@ -44,6 +44,8 @@ import static org.junit.Assume.assumeFalse;
* @since 3.2
* @see #group(TestGroup)
* @see #group(TestGroup, Executable)
* @see TestGroup
* @see TestGroups
*/
public abstract class Assume {
@@ -51,29 +53,27 @@ public abstract class Assume {
/**
* Assume that a particular {@link TestGroup} has been specified.
* @param group the group that must be specified
* @throws AssumptionViolatedException if the assumption fails
* Assume that a particular {@link TestGroup} is active.
* @param group the group that must be active
* @throws org.opentest4j.TestAbortedException if the assumption fails
*/
public static void group(TestGroup group) {
Set<TestGroup> testGroups = loadTestGroups();
if (!testGroups.contains(group)) {
throw new AssumptionViolatedException("Requires unspecified group " + group + " from " + testGroups);
}
Set<TestGroup> testGroups = TestGroups.loadTestGroups();
assumeTrue(testGroups.contains(group),
() -> "Requires inactive test group " + group + "; active test groups: " + testGroups);
}
/**
* Assume that a particular {@link TestGroup} has been specified before
* executing the supplied {@link Executable}.
* Assume that a particular {@link TestGroup} is active before executing the
* supplied {@link Executable}.
* <p>If the assumption fails, the executable will not be executed, but
* no {@link AssumptionViolatedException} will be thrown.
* @param group the group that must be specified
* no {@link org.opentest4j.TestAbortedException} will be thrown.
* @param group the group that must be active
* @param executable the executable to execute if the test group is active
* @since 4.2
*/
public static void group(TestGroup group, Executable executable) throws Exception {
Set<TestGroup> testGroups = loadTestGroups();
if (testGroups.contains(group)) {
if (TestGroups.loadTestGroups().contains(group)) {
executable.execute();
}
}
@@ -81,28 +81,13 @@ public abstract class Assume {
/**
* Assume that the specified log is not set to Trace or Debug.
* @param log the log to test
* @throws AssumptionViolatedException if the assumption fails
* @throws org.opentest4j.TestAbortedException if the assumption fails
*/
public static void notLogging(Log log) {
assumeFalse(log.isTraceEnabled());
assumeFalse(log.isDebugEnabled());
}
/**
* Load test groups dynamically instead of during static
* initialization in order to avoid a {@link NoClassDefFoundError}
* being thrown while attempting to load the {@code Assume} class.
*/
private static Set<TestGroup> loadTestGroups() {
try {
return TestGroup.parse(System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY));
}
catch (Exception ex) {
throw new IllegalStateException("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY
+ "' system property: " + ex.getMessage(), ex);
}
}
/**
* @since 4.2

View File

@@ -18,10 +18,10 @@ package org.springframework.tests;
import java.util.Arrays;
import org.junit.AssumptionViolatedException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;
import static java.util.stream.Collectors.joining;
import static org.assertj.core.api.Assertions.assertThat;
@@ -77,7 +77,7 @@ public class AssumeTests {
try {
Assume.group(LONG_RUNNING);
}
catch (AssumptionViolatedException ex) {
catch (TestAbortedException ex) {
fail("assumption should NOT have failed");
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed 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
*
* https://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.
*/
package org.springframework.tests;
import java.util.Set;
/**
* Utility methods for working with {@link TestGroup}s.
*
* @author Sam Brannen
* @author Rob Winch
* @author Phillip Webb
* @since 5.2
*/
public abstract class TestGroups {
static final String TEST_GROUPS_SYSTEM_PROPERTY = "testGroups";
/**
* Determine if the provided {@link TestGroup} is active.
* @param group the group to check
* @since 5.2
*/
public static boolean isGroupActive(TestGroup group) {
return loadTestGroups().contains(group);
}
/**
* Load test groups dynamically instead of during static initialization in
* order to avoid a {@link NoClassDefFoundError} being thrown while attempting
* to load the {@link Assume} class.
*/
static Set<TestGroup> loadTestGroups() {
try {
return TestGroup.parse(System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY));
}
catch (Exception ex) {
throw new IllegalStateException("Failed to parse '" + TEST_GROUPS_SYSTEM_PROPERTY
+ "' system property: " + ex.getMessage(), ex);
}
}
}