Skip to content

Commit 6c4cbd2

Browse files
committed
Support ContextLoader config in @⁠SpringJUnit[Web]Config
Prior to this commit, @⁠SpringJUnitConfig and @⁠SpringJUnitWebConfig did not declare `loader` attributes that alias @⁠ContextConfiguration's `loader` attribute. Consequently, it was not possible to configure a custom ContextLoader via those annotations. The lack of those `loader` attributes was an oversight, and this commit introduces them to support custom ContextLoader configuration directly via the @⁠SpringJUnitConfig and @⁠SpringJUnitWebConfig annotations. Closes gh-31498
1 parent c076f44 commit 6c4cbd2

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringJUnitConfig.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.context.ApplicationContextInitializer;
2929
import org.springframework.core.annotation.AliasFor;
3030
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.ContextLoader;
3132

3233
/**
3334
* {@code @SpringJUnitConfig} is a <em>composed annotation</em> that combines
@@ -91,6 +92,13 @@
9192
@AliasFor(annotation = ContextConfiguration.class)
9293
boolean inheritInitializers() default true;
9394

95+
/**
96+
* Alias for {@link ContextConfiguration#loader}.
97+
* @since 6.1
98+
*/
99+
@AliasFor(annotation = ContextConfiguration.class)
100+
Class<? extends ContextLoader> loader() default ContextLoader.class;
101+
94102
/**
95103
* Alias for {@link ContextConfiguration#name}.
96104
*/

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/web/SpringJUnitWebConfig.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.context.ApplicationContextInitializer;
2929
import org.springframework.core.annotation.AliasFor;
3030
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.ContextLoader;
3132
import org.springframework.test.context.junit.jupiter.SpringExtension;
3233
import org.springframework.test.context.web.WebAppConfiguration;
3334

@@ -96,6 +97,13 @@
9697
@AliasFor(annotation = ContextConfiguration.class)
9798
boolean inheritInitializers() default true;
9899

100+
/**
101+
* Alias for {@link ContextConfiguration#loader}.
102+
* @since 6.1
103+
*/
104+
@AliasFor(annotation = ContextConfiguration.class)
105+
Class<? extends ContextLoader> loader() default ContextLoader.class;
106+
99107
/**
100108
* Alias for {@link ContextConfiguration#name}.
101109
*/

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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
import org.springframework.test.context.junit.jupiter.comics.Cat;
2828
import org.springframework.test.context.junit.jupiter.comics.Dog;
2929
import org.springframework.test.context.junit.jupiter.comics.Person;
30+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
3031

3132
import static org.assertj.core.api.Assertions.assertThat;
3233

@@ -40,7 +41,7 @@
4041
* @see SpringExtension
4142
* @see ParameterizedTest
4243
*/
43-
@SpringJUnitConfig(TestConfig.class)
44+
@SpringJUnitConfig(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
4445
class SpringExtensionParameterizedTests {
4546

4647
@ParameterizedTest

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/WebSpringExtensionTests.java

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

2222
import org.springframework.http.MediaType;
2323
import org.springframework.test.context.junit.jupiter.SpringExtension;
24+
import org.springframework.test.context.web.AnnotationConfigWebContextLoader;
2425
import org.springframework.test.web.servlet.MockMvc;
2526
import org.springframework.web.context.WebApplicationContext;
2627

@@ -49,7 +50,7 @@
4950
* @see org.springframework.test.context.junit.jupiter.SpringExtensionTests
5051
* @see org.springframework.test.context.junit.jupiter.ComposedSpringExtensionTests
5152
*/
52-
@SpringJUnitWebConfig(WebConfig.class)
53+
@SpringJUnitWebConfig(classes = WebConfig.class, loader = AnnotationConfigWebContextLoader.class)
5354
@DisplayName("Web SpringExtension Tests")
5455
class WebSpringExtensionTests {
5556

0 commit comments

Comments
 (0)