Skip to content

Commit

Permalink
Support ContextLoader config in @⁠SpringJUnit[Web]Config
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Oct 25, 2023
1 parent c076f44 commit 6c4cbd2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,6 +28,7 @@
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 <em>composed annotation</em> that combines
Expand Down Expand Up @@ -91,6 +92,13 @@
@AliasFor(annotation = ContextConfiguration.class)
boolean inheritInitializers() default true;

/**
* Alias for {@link ContextConfiguration#loader}.
* @since 6.1
*/
@AliasFor(annotation = ContextConfiguration.class)
Class<? extends ContextLoader> loader() default ContextLoader.class;

/**
* Alias for {@link ContextConfiguration#name}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,6 +28,7 @@
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;

Expand Down Expand Up @@ -96,6 +97,13 @@
@AliasFor(annotation = ContextConfiguration.class)
boolean inheritInitializers() default true;

/**
* Alias for {@link ContextConfiguration#loader}.
* @since 6.1
*/
@AliasFor(annotation = ContextConfiguration.class)
Class<? extends ContextLoader> loader() default ContextLoader.class;

/**
* Alias for {@link ContextConfiguration#name}.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -27,6 +27,7 @@
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;

Expand All @@ -40,7 +41,7 @@
* @see SpringExtension
* @see ParameterizedTest
*/
@SpringJUnitConfig(TestConfig.class)
@SpringJUnitConfig(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
class SpringExtensionParameterizedTests {

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,6 +21,7 @@

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;

Expand Down Expand Up @@ -49,7 +50,7 @@
* @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 {

Expand Down

0 comments on commit 6c4cbd2

Please sign in to comment.