From 6c4cbd2aab6a49cbd36438007ca296006e305a3a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Wed, 25 Oct 2023 16:12:12 +0200 Subject: [PATCH] =?UTF-8?q?Support=20ContextLoader=20config=20in=20@?= =?UTF-8?q?=E2=81=A0SpringJUnit[Web]Config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../test/context/junit/jupiter/SpringJUnitConfig.java | 10 +++++++++- .../junit/jupiter/web/SpringJUnitWebConfig.java | 10 +++++++++- .../jupiter/SpringExtensionParameterizedTests.java | 5 +++-- .../junit/jupiter/web/WebSpringExtensionTests.java | 5 +++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringJUnitConfig.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringJUnitConfig.java index af6f8370482..f493ad5b2cd 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringJUnitConfig.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringJUnitConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.context.ApplicationContextInitializer; import org.springframework.core.annotation.AliasFor; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.ContextLoader; /** * {@code @SpringJUnitConfig} is a composed annotation that combines @@ -91,6 +92,13 @@ public @interface SpringJUnitConfig { @AliasFor(annotation = ContextConfiguration.class) boolean inheritInitializers() default true; + /** + * Alias for {@link ContextConfiguration#loader}. + * @since 6.1 + */ + @AliasFor(annotation = ContextConfiguration.class) + Class loader() default ContextLoader.class; + /** * Alias for {@link ContextConfiguration#name}. */ diff --git a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/web/SpringJUnitWebConfig.java b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/web/SpringJUnitWebConfig.java index 614faa7003b..595695ee892 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/web/SpringJUnitWebConfig.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit/jupiter/web/SpringJUnitWebConfig.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.context.ApplicationContextInitializer; import org.springframework.core.annotation.AliasFor; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.ContextLoader; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.web.WebAppConfiguration; @@ -96,6 +97,13 @@ public @interface SpringJUnitWebConfig { @AliasFor(annotation = ContextConfiguration.class) boolean inheritInitializers() default true; + /** + * Alias for {@link ContextConfiguration#loader}. + * @since 6.1 + */ + @AliasFor(annotation = ContextConfiguration.class) + Class loader() default ContextLoader.class; + /** * Alias for {@link ContextConfiguration#name}. */ diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java index fc3165b0b04..f7632dc8e63 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -27,6 +27,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.test.context.junit.jupiter.comics.Cat; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; +import org.springframework.test.context.support.AnnotationConfigContextLoader; import static org.assertj.core.api.Assertions.assertThat; @@ -40,7 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @see SpringExtension * @see ParameterizedTest */ -@SpringJUnitConfig(TestConfig.class) +@SpringJUnitConfig(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class) class SpringExtensionParameterizedTests { @ParameterizedTest diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/WebSpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/WebSpringExtensionTests.java index 134078bc458..efe1fdcda5c 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/WebSpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/web/WebSpringExtensionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 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. @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.web.AnnotationConfigWebContextLoader; import org.springframework.test.web.servlet.MockMvc; import org.springframework.web.context.WebApplicationContext; @@ -49,7 +50,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC * @see org.springframework.test.context.junit.jupiter.SpringExtensionTests * @see org.springframework.test.context.junit.jupiter.ComposedSpringExtensionTests */ -@SpringJUnitWebConfig(WebConfig.class) +@SpringJUnitWebConfig(classes = WebConfig.class, loader = AnnotationConfigWebContextLoader.class) @DisplayName("Web SpringExtension Tests") class WebSpringExtensionTests {