Skip to content

Commit 0beee7d

Browse files
committed
Stop using convention-based attribute overrides in tests in spring-test and spring-mvc
See gh-28760
1 parent c54b151 commit 0beee7d

15 files changed

+69
-33
lines changed

spring-test/src/test/java/org/springframework/test/context/OverriddenMetaAnnotationAttributesTestContextAnnotationUtilsTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -117,8 +117,10 @@ static class OverriddenMetaValueConfigTestCase {
117117
@Retention(RetentionPolicy.RUNTIME)
118118
@interface MetaLocationsConfig {
119119

120+
@AliasFor(annotation = ContextConfiguration.class)
120121
String[] locations() default {};
121122

123+
@AliasFor(annotation = ContextConfiguration.class)
122124
boolean inheritLocations();
123125
}
124126

spring-test/src/test/java/org/springframework/test/context/TestContextAnnotationUtilsTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.Test;
3030

3131
import org.springframework.core.SpringProperties;
32+
import org.springframework.core.annotation.AliasFor;
3233
import org.springframework.core.annotation.AnnotationUtils;
3334
import org.springframework.core.annotation.Order;
3435
import org.springframework.stereotype.Component;
@@ -557,14 +558,15 @@ private void assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(Clas
557558
@Target(ElementType.TYPE)
558559
@interface MetaConfig {
559560

561+
@AliasFor(annotation = ContextConfiguration.class)
562+
Class<?>[] classes() default { DevConfig.class, ProductionConfig.class };
563+
560564
class DevConfig {
561565
}
562566

563567
class ProductionConfig {
564568
}
565569

566-
567-
Class<?>[] classes() default { DevConfig.class, ProductionConfig.class };
568570
}
569571

570572
// -------------------------------------------------------------------------

spring-test/src/test/java/org/springframework/test/context/TestExecutionListenersTests.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.core.Ordered;
26+
import org.springframework.core.annotation.AliasFor;
2627
import org.springframework.core.annotation.AnnotationConfigurationException;
2728
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
2829
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
@@ -209,7 +210,7 @@ private void assertRegisteredListeners(Class<?> testClass, List<Class<?>> expect
209210

210211
private void assertNumRegisteredListeners(Class<?> testClass, int expected) {
211212
TestContextManager testContextManager = new TestContextManager(testClass);
212-
assertThat(testContextManager.getTestExecutionListeners().size()).as("Num registered TELs for " + testClass).isEqualTo(expected);
213+
assertThat(testContextManager.getTestExecutionListeners()).as("Num registered TELs for " + testClass).hasSize(expected);
213214
}
214215

215216

@@ -294,6 +295,7 @@ static class DuplicateListenersConfigTestCase {
294295
@Retention(RetentionPolicy.RUNTIME)
295296
@interface MetaListenersWithOverrides {
296297

298+
@AliasFor(annotation = TestExecutionListeners.class)
297299
Class<? extends TestExecutionListener>[] listeners() default
298300
{FooTestExecutionListener.class, BarTestExecutionListener.class};
299301
}
@@ -302,17 +304,21 @@ Class<? extends TestExecutionListener>[] listeners() default
302304
@Retention(RetentionPolicy.RUNTIME)
303305
@interface MetaInheritedListenersWithOverrides {
304306

307+
@AliasFor(annotation = TestExecutionListeners.class)
305308
Class<? extends TestExecutionListener>[] listeners() default QuuxTestExecutionListener.class;
306309

310+
@AliasFor(annotation = TestExecutionListeners.class)
307311
boolean inheritListeners() default true;
308312
}
309313

310314
@TestExecutionListeners
311315
@Retention(RetentionPolicy.RUNTIME)
312316
@interface MetaNonInheritedListenersWithOverrides {
313317

318+
@AliasFor(annotation = TestExecutionListeners.class)
314319
Class<? extends TestExecutionListener>[] listeners() default QuuxTestExecutionListener.class;
315320

321+
@AliasFor(annotation = TestExecutionListeners.class)
316322
boolean inheritListeners() default false;
317323
}
318324

spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323
import org.junit.runners.model.FrameworkMethod;
2424

25+
import org.springframework.core.annotation.AliasFor;
2526
import org.springframework.test.annotation.Timed;
2627
import org.springframework.test.context.TestContextManager;
2728

@@ -94,6 +95,7 @@ void springTimeoutWithMetaAnnotationAndOverride() {
9495
@Retention(RetentionPolicy.RUNTIME)
9596
private static @interface MetaTimedWithOverride {
9697

98+
@AliasFor(annotation = Timed.class)
9799
long millis() default 1000;
98100
}
99101

spring-test/src/test/java/org/springframework/test/context/junit4/TimedSpringRunnerTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -25,6 +25,7 @@
2525
import org.junit.runner.Runner;
2626
import org.junit.runners.JUnit4;
2727

28+
import org.springframework.core.annotation.AliasFor;
2829
import org.springframework.test.annotation.Timed;
2930
import org.springframework.test.context.TestExecutionListeners;
3031

@@ -112,12 +113,13 @@ public void springAndJUnitTimeouts() {
112113

113114
@Timed(millis = 10)
114115
@Retention(RetentionPolicy.RUNTIME)
115-
private static @interface MetaTimed {
116+
private @interface MetaTimed {
116117
}
117118

118119
@Timed(millis = 1000)
119120
@Retention(RetentionPolicy.RUNTIME)
120-
private static @interface MetaTimedWithOverride {
121+
private @interface MetaTimedWithOverride {
122+
@AliasFor(annotation = Timed.class)
121123
long millis() default 1000;
122124
}
123125

spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -24,6 +24,7 @@
2424
import org.springframework.context.annotation.Bean;
2525
import org.springframework.context.annotation.Configuration;
2626
import org.springframework.context.annotation.Profile;
27+
import org.springframework.core.annotation.AliasFor;
2728
import org.springframework.test.context.ActiveProfiles;
2829
import org.springframework.test.context.ActiveProfilesResolver;
2930
import org.springframework.test.context.ContextConfiguration;
@@ -42,6 +43,13 @@
4243
@Target(ElementType.TYPE)
4344
public @interface ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig {
4445

46+
@AliasFor(annotation = ContextConfiguration.class)
47+
Class<?>[] classes() default { DevConfig.class, ProductionConfig.class, ResolverConfig.class };
48+
49+
@AliasFor(annotation = ActiveProfiles.class)
50+
Class<? extends ActiveProfilesResolver> resolver() default CustomResolver.class;
51+
52+
4553
@Configuration
4654
@Profile("dev")
4755
static class DevConfig {
@@ -81,9 +89,4 @@ public String[] resolve(Class<?> testClass) {
8189
}
8290
}
8391

84-
85-
Class<?>[] classes() default { DevConfig.class, ProductionConfig.class, ResolverConfig.class };
86-
87-
Class<? extends ActiveProfilesResolver> resolver() default CustomResolver.class;
88-
8992
}

spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesMetaConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24+
import org.springframework.core.annotation.AliasFor;
2425
import org.springframework.test.context.ActiveProfiles;
2526
import org.springframework.test.context.ContextConfiguration;
2627

@@ -38,8 +39,10 @@
3839
@Target(ElementType.TYPE)
3940
public @interface ConfigClassesAndProfilesMetaConfig {
4041

42+
@AliasFor(annotation = ContextConfiguration.class)
4143
Class<?>[] classes() default {};
4244

45+
@AliasFor(annotation = ActiveProfiles.class)
4346
String[] profiles() default {};
4447

4548
}

spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -24,6 +24,7 @@
2424
import org.springframework.context.annotation.Bean;
2525
import org.springframework.context.annotation.Configuration;
2626
import org.springframework.context.annotation.Profile;
27+
import org.springframework.core.annotation.AliasFor;
2728
import org.springframework.test.context.ActiveProfiles;
2829
import org.springframework.test.context.ContextConfiguration;
2930

@@ -41,6 +42,12 @@
4142
@Target(ElementType.TYPE)
4243
public @interface ConfigClassesAndProfilesWithCustomDefaultsMetaConfig {
4344

45+
@AliasFor(annotation = ContextConfiguration.class)
46+
Class<?>[] classes() default { DevConfig.class, ProductionConfig.class };
47+
48+
@AliasFor(annotation = ActiveProfiles.class)
49+
String[] profiles() default "dev";
50+
4451
@Configuration
4552
@Profile("dev")
4653
static class DevConfig {
@@ -61,9 +68,4 @@ public String foo() {
6168
}
6269
}
6370

64-
65-
Class<?>[] classes() default { DevConfig.class, ProductionConfig.class };
66-
67-
String[] profiles() default "dev";
68-
6971
}

spring-test/src/test/java/org/springframework/test/context/junit4/annotation/meta/ConfigClassesAndProfilesWithCustomDefaultsMetaConfigWithOverridesTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.testfixture.beans.Pet;
2525
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2626
import org.springframework.test.context.junit4.annotation.PojoAndStringConfig;
27+
import org.springframework.test.context.junit4.annotation.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
2728

2829
import static org.assertj.core.api.Assertions.assertThat;
2930

@@ -35,8 +36,8 @@
3536
* @since 4.0
3637
*/
3738
@RunWith(SpringJUnit4ClassRunner.class)
38-
@ConfigClassesAndProfilesWithCustomDefaultsMetaConfig(classes = { PojoAndStringConfig.class,
39-
ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig.class }, profiles = "prod")
39+
@ConfigClassesAndProfilesWithCustomDefaultsMetaConfig(
40+
classes = { PojoAndStringConfig.class, ProductionConfig.class }, profiles = "prod")
4041
public class ConfigClassesAndProfilesWithCustomDefaultsMetaConfigWithOverridesTests {
4142

4243
@Autowired
@@ -65,4 +66,5 @@ public void verifyPet() {
6566
public void verifyFoo() {
6667
assertThat(this.foo).isEqualTo("Production Foo");
6768
}
69+
6870
}

spring-test/src/test/java/org/springframework/test/context/support/AbstractContextConfigurationUtilsTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -27,6 +27,7 @@
2727

2828
import org.springframework.context.ApplicationContextInitializer;
2929
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.annotation.AliasFor;
3031
import org.springframework.test.context.ActiveProfiles;
3132
import org.springframework.test.context.BootstrapContext;
3233
import org.springframework.test.context.BootstrapTestUtils;
@@ -158,8 +159,10 @@ static class QuuxConfig {
158159
@Target(ElementType.TYPE)
159160
public static @interface MetaLocationsFooConfigWithOverrides {
160161

162+
@AliasFor(annotation = ContextConfiguration.class)
161163
String[] locations() default "/foo.xml";
162164

165+
@AliasFor(annotation = ActiveProfiles.class)
163166
String[] profiles() default "foo";
164167
}
165168

spring-test/src/test/java/org/springframework/test/context/support/BootstrapTestUtilsMergedConfigTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.api.Test;
2626

2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.annotation.AliasFor;
2829
import org.springframework.test.context.ActiveProfiles;
2930
import org.springframework.test.context.BootstrapTestUtils;
3031
import org.springframework.test.context.ContextConfiguration;
@@ -466,6 +467,7 @@ void buildMergedConfigWithEmptyConfigurationOnEnclosingClassAndExplicitConfigOnN
466467
@Target(ElementType.TYPE)
467468
public static @interface SpringAppConfig {
468469

470+
@AliasFor(annotation = ContextConfiguration.class)
469471
Class<?>[] classes() default {};
470472
}
471473

spring-test/src/test/java/org/springframework/test/context/support/ContextLoaderUtilsContextHierarchyTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -26,6 +26,7 @@
2626

2727
import org.springframework.context.ApplicationContextInitializer;
2828
import org.springframework.context.ConfigurableApplicationContext;
29+
import org.springframework.core.annotation.AliasFor;
2930
import org.springframework.test.context.ContextConfiguration;
3031
import org.springframework.test.context.ContextConfigurationAttributes;
3132
import org.springframework.test.context.ContextHierarchy;
@@ -579,6 +580,7 @@ private static class TestClass3WithSingleLevelContextHierarchyFromMetaAnnotation
579580
@Retention(RetentionPolicy.RUNTIME)
580581
private static @interface ContextConfigWithOverrides {
581582

583+
@AliasFor(annotation = ContextConfiguration.class)
582584
String[] locations() default "A.xml";
583585
}
584586

spring-test/src/test/java/org/springframework/test/context/support/DirtiesContextTestExecutionListenerTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Test;
2525
import org.mockito.BDDMockito;
2626

27+
import org.springframework.core.annotation.AliasFor;
2728
import org.springframework.test.annotation.DirtiesContext;
2829
import org.springframework.test.annotation.DirtiesContext.ClassMode;
2930
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
@@ -403,8 +404,10 @@ void test() {
403404
@Retention(RetentionPolicy.RUNTIME)
404405
@interface MetaDirtyWithOverrides {
405406

407+
@AliasFor(annotation = DirtiesContext.class)
406408
ClassMode classMode() default AFTER_EACH_TEST_METHOD;
407409

410+
@AliasFor(annotation = DirtiesContext.class)
408411
HierarchyMode hierarchyMode() default HierarchyMode.CURRENT_LEVEL;
409412
}
410413

0 commit comments

Comments
 (0)