Introduce @EnabledForTestGroups in Spring's test suite

Closes gh-23476
This commit is contained in:
Sam Brannen
2019-08-17 14:27:35 +02:00
parent bbe33832cf
commit 288461a541
33 changed files with 244 additions and 181 deletions

View File

@@ -51,13 +51,13 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.tests.EnabledForTestGroups;
import org.springframework.util.ClassUtils;
import org.springframework.util.StopWatch;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.tests.TestGroup.PERFORMANCE;
/**
* Unit tests for {@link DefaultConversionService}.
@@ -966,8 +966,8 @@ public class DefaultConversionServiceTests {
}
@Test
@EnabledForTestGroups(PERFORMANCE)
public void testPerformance1() {
Assume.group(TestGroup.PERFORMANCE);
StopWatch watch = new StopWatch("integer->string conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
for (int i = 0; i < 4000000; i++) {

View File

@@ -44,8 +44,7 @@ import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.tests.EnabledForTestGroups;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
@@ -55,6 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.springframework.tests.TestGroup.PERFORMANCE;
/**
* Unit tests for {@link GenericConversionService}.
@@ -343,8 +343,8 @@ public class GenericConversionServiceTests {
}
@Test
@EnabledForTestGroups(PERFORMANCE)
public void testPerformance2() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
StopWatch watch = new StopWatch("list<string> -> list<integer> conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
List<String> source = new LinkedList<>();
@@ -368,8 +368,8 @@ public class GenericConversionServiceTests {
}
@Test
@EnabledForTestGroups(PERFORMANCE)
public void testPerformance3() throws Exception {
Assume.group(TestGroup.PERFORMANCE);
StopWatch watch = new StopWatch("map<string, string> -> map<string, integer> conversionPerformance");
watch.start("convert 4,000,000 with conversion service");
Map<String, String> source = new HashMap<>();
@@ -884,6 +884,7 @@ public class GenericConversionServiceTests {
private static class StringToMyEnumInterfaceConverterFactory implements ConverterFactory<String, MyEnumInterface> {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public <T extends MyEnumInterface> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToMyEnumInterfaceConverter(targetType);
@@ -897,6 +898,7 @@ public class GenericConversionServiceTests {
this.enumType = enumType;
}
@Override
public T convert(String source) {
for (T value : enumType.getEnumConstants()) {
if (value.getCode().equals(source)) {
@@ -911,6 +913,7 @@ public class GenericConversionServiceTests {
private static class StringToMyEnumBaseInterfaceConverterFactory implements ConverterFactory<String, MyEnumBaseInterface> {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public <T extends MyEnumBaseInterface> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToMyEnumBaseInterfaceConverter(targetType);
@@ -924,6 +927,7 @@ public class GenericConversionServiceTests {
this.enumType = enumType;
}
@Override
public T convert(String source) {
for (T value : enumType.getEnumConstants()) {
if (value.getBaseCode().equals(source)) {

View File

@@ -25,11 +25,10 @@ import org.springframework.core.io.UrlResource;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.tests.EnabledForTestGroups;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.tests.TestGroup.LONG_RUNNING;
/**
* Unit tests for checking the behaviour of {@link CachingMetadataReaderFactory} under
@@ -39,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Costin Leau
* @author Sam Brannen
*/
@EnabledForTestGroups(LONG_RUNNING)
public class CachingMetadataReaderLeakTests {
private static final int ITEMS_TO_LOAD = 9999;
@@ -47,8 +47,6 @@ public class CachingMetadataReaderLeakTests {
@Test
public void testSignificantLoad() throws Exception {
Assume.group(TestGroup.LONG_RUNNING);
// the biggest public class in the JDK (>60k)
URL url = getClass().getResource("/java/awt/Component.class");
assertThat(url).isNotNull();

View File

@@ -42,6 +42,7 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue;
* @author Phillip Webb
* @author Sam Brannen
* @since 3.2
* @see EnabledForTestGroups @EnabledForTestGroups
* @see #group(TestGroup)
* @see #group(TestGroup, Executable)
* @see TestGroup
@@ -56,7 +57,9 @@ public abstract class Assume {
* Assume that a particular {@link TestGroup} is active.
* @param group the group that must be active
* @throws org.opentest4j.TestAbortedException if the assumption fails
* @deprecated as of Spring Framework 5.2 in favor of {@link EnabledForTestGroups}
*/
@Deprecated
public static void group(TestGroup group) {
Set<TestGroup> testGroups = TestGroups.loadTestGroups();
assumeTrue(testGroups.contains(group),

View File

@@ -37,18 +37,18 @@ import static org.springframework.tests.TestGroup.PERFORMANCE;
* @author Sam Brannen
* @since 5.0
*/
public class AssumeTests {
class AssumeTests {
private String originalTestGroups;
@BeforeEach
public void trackOriginalTestGroups() {
void trackOriginalTestGroups() {
this.originalTestGroups = System.getProperty(TEST_GROUPS_SYSTEM_PROPERTY);
}
@AfterEach
public void restoreOriginalTestGroups() {
void restoreOriginalTestGroups() {
if (this.originalTestGroups != null) {
setTestGroups(this.originalTestGroups);
}
@@ -58,21 +58,24 @@ public class AssumeTests {
}
@Test
public void assumeGroupWithNoActiveTestGroups() {
@SuppressWarnings("deprecation")
void assumeGroupWithNoActiveTestGroups() {
setTestGroups("");
Assume.group(LONG_RUNNING);
fail("assumption should have failed");
}
@Test
public void assumeGroupWithNoMatchingActiveTestGroup() {
@SuppressWarnings("deprecation")
void assumeGroupWithNoMatchingActiveTestGroup() {
setTestGroups(PERFORMANCE, CI);
Assume.group(LONG_RUNNING);
fail("assumption should have failed");
}
@Test
public void assumeGroupWithMatchingActiveTestGroup() {
@SuppressWarnings("deprecation")
void assumeGroupWithMatchingActiveTestGroup() {
setTestGroups(LONG_RUNNING);
try {
Assume.group(LONG_RUNNING);
@@ -83,15 +86,16 @@ public class AssumeTests {
}
@Test
public void assumeGroupWithBogusActiveTestGroup() {
void assumeGroupWithBogusActiveTestGroup() {
assertBogusActiveTestGroupBehavior("bogus");
}
@Test
public void assumeGroupWithAllMinusBogusActiveTestGroup() {
void assumeGroupWithAllMinusBogusActiveTestGroup() {
assertBogusActiveTestGroupBehavior("all-bogus");
}
@SuppressWarnings("deprecation")
private void assertBogusActiveTestGroupBehavior(String testGroups) {
// Should result in something similar to the following:
//

View File

@@ -0,0 +1,47 @@
/*
* 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.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* {@code @EnabledForTestGroups} is used to enable the annotated test class or
* test method for one or more {@link TestGroup} {@linkplain #value values}.
*
* @author Sam Brannen
* @since 5.2
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ExtendWith(TestGroupsCondition.class)
public @interface EnabledForTestGroups {
/**
* One or more {@link TestGroup}s that must be active.
*/
TestGroup[] value();
}

View File

@@ -28,6 +28,7 @@ import static java.lang.String.format;
/**
* A test group used to limit when certain tests are run.
*
* @see EnabledForTestGroups @EnabledForTestGroups
* @see Assume#group(TestGroup)
* @author Phillip Webb
* @author Chris Beams

View File

@@ -0,0 +1,57 @@
/*
* 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.Arrays;
import java.util.Optional;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.util.Assert;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
/**
* {@link ExecutionCondition} for Spring's {@link TestGroup} support.
*
* @author Sam Brannen
* @since 5.2
* @see EnabledForTestGroups @EnabledForTestGroups
*/
class TestGroupsCondition implements ExecutionCondition {
private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = enabled("@EnabledForTestGroups is not present");
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<EnabledForTestGroups> optional = findAnnotation(context.getElement(), EnabledForTestGroups.class);
if (!optional.isPresent()) {
return ENABLED_BY_DEFAULT;
}
TestGroup[] testGroups = optional.get().value();
Assert.state(testGroups.length > 0, "You must declare at least one TestGroup in @EnabledForTestGroups");
return (Arrays.stream(testGroups).anyMatch(TestGroups::isGroupActive)) ?
enabled("Enabled for TestGroups: " + Arrays.toString(testGroups)) :
disabled("Disabled for TestGroups: " + Arrays.toString(testGroups));
}
}

View File

@@ -27,12 +27,12 @@ import java.util.List;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.tests.EnabledForTestGroups;
import org.springframework.tests.sample.objects.TestObject;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.springframework.tests.TestGroup.PERFORMANCE;
/**
* @author Rob Harrop
@@ -326,9 +326,8 @@ public class ReflectionUtilsTests {
}
@Test
@EnabledForTestGroups(PERFORMANCE)
public void getUniqueDeclaredMethods_isFastEnough() {
Assume.group(TestGroup.PERFORMANCE);
@SuppressWarnings("unused")
class C {
void m00() { } void m01() { } void m02() { } void m03() { } void m04() { }