Browse Source

Support @TestConstructor config via JUnit Platform config param

This commit introduces support for setting the
spring.test.constructor.autowire.mode property via a JUnit Platform
configuration parameter -- for example, via the
junit-platform.properties file.

Closes gh-24285
pull/24571/merge
Sam Brannen 5 years ago
parent
commit
1b4b4c3302
  1. 35
      spring-test/src/main/java/org/springframework/test/context/TestConstructor.java
  2. 13
      spring-test/src/main/java/org/springframework/test/context/junit/jupiter/SpringExtension.java
  3. 42
      spring-test/src/main/java/org/springframework/test/context/support/PropertyProvider.java
  4. 87
      spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java
  5. 120
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorIntegrationTests.java
  6. 5
      src/docs/asciidoc/testing.adoc

35
spring-test/src/main/java/org/springframework/test/context/TestConstructor.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -23,6 +23,11 @@ import java.lang.annotation.Retention; @@ -23,6 +23,11 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
/**
* {@code @TestConstructor} is a type-level annotation that is used to configure
* how the parameters of a test class constructor are autowired from components
@ -78,6 +83,9 @@ public @interface TestConstructor { @@ -78,6 +83,9 @@ public @interface TestConstructor {
* <p>May alternatively be configured via the
* {@link org.springframework.core.SpringProperties SpringProperties}
* mechanism.
* <p>As of Spring Framework 5.3, this property may also be configured as a
* <a href="https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params">JUnit
* Platform configuration parameter</a>.
* @see #autowireMode
*/
String TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME = "spring.test.constructor.autowire.mode";
@ -124,6 +132,31 @@ public @interface TestConstructor { @@ -124,6 +132,31 @@ public @interface TestConstructor {
*/
ANNOTATED;
private static final Log logger = LogFactory.getLog(AutowireMode.class);
/**
* Get the {@code AutowireMode} enum constant with the supplied name,
* ignoring case.
*
* @param name the name of the enum constant to retrieve
* @return the corresponding enum constant or {@code null} if not found
* @since 5.3
* @see AutowireMode#valueOf(String)
*/
@Nullable
public static AutowireMode from(@Nullable String name) {
try {
return AutowireMode.valueOf(name.trim().toUpperCase());
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Failed to parse autowire mode from '%s': %s", name,
ex.getMessage()));
}
}
return null;
}
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -39,6 +39,7 @@ import org.springframework.context.ApplicationContext; @@ -39,6 +39,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.support.PropertyProvider;
import org.springframework.test.context.support.TestConstructorUtils;
import org.springframework.util.Assert;
@ -148,8 +149,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes @@ -148,8 +149,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
* <ol>
* <li>The {@linkplain ParameterContext#getDeclaringExecutable() declaring
* executable} is a {@link Constructor} and
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class)}
* returns {@code true}.</li>
* {@link TestConstructorUtils#isAutowirableConstructor(Constructor, Class, PropertyProvider)}
* returns {@code true}. Note that {@code isAutowirableConstructor()} will be
* invoked with a fallback {@link PropertyProvider} that delegates its lookup
* to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
* <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
* <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
* </ol>
@ -167,7 +170,9 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes @@ -167,7 +170,9 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
Parameter parameter = parameterContext.getParameter();
Executable executable = parameter.getDeclaringExecutable();
Class<?> testClass = extensionContext.getRequiredTestClass();
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass) ||
PropertyProvider junitPropertyProvider = propertyName ->
extensionContext.getConfigurationParameter(propertyName).orElse(null);
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) ||
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
}

42
spring-test/src/main/java/org/springframework/test/context/support/PropertyProvider.java

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
/*
* Copyright 2002-2020 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.test.context.support;
import org.springframework.lang.Nullable;
/**
* Strategy for providing named properties &mdash; for example, for looking up
* key-value pairs in a generic fashion.
*
* <p>Primarily intended for use within the framework.
*
* @author Sam Brannen
* @since 5.3
*/
@FunctionalInterface
public interface PropertyProvider {
/**
* Get the value of the named property.
*
* @param name the name of the property to retrieve
* @return the value of the property or {@code null} if not found
*/
@Nullable
String get(String name);
}

87
spring-test/src/main/java/org/springframework/test/context/support/TestConstructorUtils.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -19,12 +19,10 @@ package org.springframework.test.context.support; @@ -19,12 +19,10 @@ package org.springframework.test.context.support;
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.SpringProperties;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestConstructor.AutowireMode;
@ -39,9 +37,6 @@ import org.springframework.test.context.TestConstructor.AutowireMode; @@ -39,9 +37,6 @@ import org.springframework.test.context.TestConstructor.AutowireMode;
*/
public abstract class TestConstructorUtils {
private static final Log logger = LogFactory.getLog(TestConstructorUtils.class);
private TestConstructorUtils() {
}
@ -49,17 +44,55 @@ public abstract class TestConstructorUtils { @@ -49,17 +44,55 @@ public abstract class TestConstructorUtils {
* Determine if the supplied executable for the given test class is an
* autowirable constructor.
*
* <p>This method delegates to {@link #isAutowirableConstructor(Constructor, Class)}
* if the executable is a constructor.
* <p>This method delegates to {@link #isAutowirableConstructor(Executable, Class, PropertyProvider)}
* will a value of {@code null} for the fallback {@link PropertyProvider}.
*
* @param executable an executable for the test class
* @param testClass the test class
* @return {@code true} if the executable is an autowirable constructor
* @see #isAutowirableConstructor(Constructor, Class)
* @see #isAutowirableConstructor(Executable, Class, PropertyProvider)
*/
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass) {
return isAutowirableConstructor(executable, testClass, null);
}
/**
* Determine if the supplied constructor for the given test class is
* autowirable.
*
* <p>This method delegates to {@link #isAutowirableConstructor(Constructor, Class, PropertyProvider)}
* will a value of {@code null} for the fallback {@link PropertyProvider}.
*
* @param constructor a constructor for the test class
* @param testClass the test class
* @return {@code true} if the constructor is autowirable
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
*/
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
return isAutowirableConstructor(constructor, testClass, null);
}
/**
* Determine if the supplied executable for the given test class is an
* autowirable constructor.
*
* <p>This method delegates to {@link #isAutowirableConstructor(Constructor, Class, PropertyProvider)}
* if the supplied executable is a constructor and otherwise returns {@code false}.
*
* @param executable an executable for the test class
* @param testClass the test class
* @param fallbackPropertyProvider fallback property provider used to look up
* the value for {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}
* if no such value is found in {@link SpringProperties}
* @return {@code true} if the executable is an autowirable constructor
* @since 5.3
* @see #isAutowirableConstructor(Constructor, Class, PropertyProvider)
*/
public static boolean isAutowirableConstructor(Executable executable, Class<?> testClass,
@Nullable PropertyProvider fallbackPropertyProvider) {
return (executable instanceof Constructor &&
isAutowirableConstructor((Constructor<?>) executable, testClass));
isAutowirableConstructor((Constructor<?>) executable, testClass, fallbackPropertyProvider));
}
/**
@ -75,17 +108,23 @@ public abstract class TestConstructorUtils { @@ -75,17 +108,23 @@ public abstract class TestConstructorUtils {
* <em>meta-present</em> on the test class with
* {@link TestConstructor#autowireMode() autowireMode} set to
* {@link AutowireMode#ALL ALL}.</li>
* <li>The default <em>test constructor autowire mode</em> has been changed
* to {@code ALL} (see
* <li>The default <em>test constructor autowire mode</em> has been set to
* {@code ALL} in {@link SpringProperties} or in the supplied fallback
* {@link PropertyProvider} (see
* {@link TestConstructor#TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME}).</li>
* </ol>
*
* @param constructor a constructor for the test class
* @param testClass the test class
* @param fallbackPropertyProvider fallback property provider used to look up
* the value for the default <em>test constructor autowire mode</em> if no
* such value is found in {@link SpringProperties}
* @return {@code true} if the constructor is autowirable
* @see #isAutowirableConstructor(Executable, Class)
* @since 5.3
*/
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass) {
public static boolean isAutowirableConstructor(Constructor<?> constructor, Class<?> testClass,
@Nullable PropertyProvider fallbackPropertyProvider) {
// Is the constructor annotated with @Autowired?
if (AnnotatedElementUtils.hasAnnotation(constructor, Autowired.class)) {
return true;
@ -99,18 +138,14 @@ public abstract class TestConstructorUtils { @@ -99,18 +138,14 @@ public abstract class TestConstructorUtils {
autowireMode = testConstructor.autowireMode();
}
else {
// Custom global default?
// Custom global default from SpringProperties?
String value = SpringProperties.getProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
if (value != null) {
try {
autowireMode = AutowireMode.valueOf(value.trim().toUpperCase());
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Failed to parse autowire mode '%s' for property '%s': %s", value,
TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME, ex.getMessage()));
}
}
autowireMode = AutowireMode.from(value);
// Use fallback provider?
if (autowireMode == null && fallbackPropertyProvider != null) {
value = fallbackPropertyProvider.get(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME);
autowireMode = AutowireMode.from(value);
}
}

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

@ -0,0 +1,120 @@ @@ -0,0 +1,120 @@
/*
* Copyright 2002-2020 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.test.context.junit.jupiter;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.SpringProperties;
import org.springframework.test.context.TestConstructor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.testkit.engine.EventConditions.event;
import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure;
import static org.junit.platform.testkit.engine.EventConditions.test;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf;
import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message;
import static org.springframework.test.context.TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME;
/**
* Integration tests for {@link TestConstructor @TestConstructor} support.
*
* @author Sam Brannen
* @since 5.3
*/
class TestConstructorIntegrationTests {
@BeforeEach
@AfterEach
void clearSpringProperty() {
setSpringProperty(null);
}
@Test
void autowireModeNotSetToAll() {
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(AutomaticallyAutowiredTestCase.class))
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(1).succeeded(0).failed(1))
.assertThatEvents().haveExactly(1, event(test("test"),
finishedWithFailure(
instanceOf(ParameterResolutionException.class),
message(msg -> msg.matches(".+for parameter \\[java\\.lang\\.String .+\\] in constructor.+")))));
}
@Test
void autowireModeSetToAllViaSpringProperties() {
setSpringProperty("all");
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(AutomaticallyAutowiredTestCase.class))
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0));
}
@Test
void autowireModeSetToAllViaJUnitPlatformConfigurationParameter() {
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(AutomaticallyAutowiredTestCase.class))
.configurationParameter(TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME, "all")
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0));
}
private void setSpringProperty(String flag) {
SpringProperties.setProperty(TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME, flag);
}
@SpringJUnitConfig
@FailingTestCase
static class AutomaticallyAutowiredTestCase {
private final String foo;
AutomaticallyAutowiredTestCase(String foo) {
this.foo = foo;
}
@Test
void test() {
assertThat(foo).isEqualTo("bar");
}
@Configuration
static class Config {
@Bean
String foo() {
return "bar";
}
}
}
}

5
src/docs/asciidoc/testing.adoc

@ -1826,7 +1826,10 @@ constructor takes precedence over both `@TestConstructor` and the default mode. @@ -1826,7 +1826,10 @@ constructor takes precedence over both `@TestConstructor` and the default mode.
=====
The default _test constructor autowire mode_ can be changed by setting the
`spring.test.constructor.autowire.mode` JVM system property to `all`. Alternatively, the
default mode may be changed via the `SpringProperties` mechanism.
default mode may be set via the `SpringProperties` mechanism.
As of Spring Framework 5.3, the default mode may also be configured as a
https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params[JUnit Platform configuration parameter].
If the `spring.test.constructor.autowire.mode` property is not set, test class
constructors will not be automatically autowired.

Loading…
Cancel
Save