Support automatic constructor injection in JUnit Jupiter

Prior to this commit, dependency injection of all arguments in a test
class constructor invoked by JUnit Jupiter was only possible if the
constructor was explicitly annotated with @Autowired.

This commit introduces support for a configurable "test constructor
autowire" mode which defaults to false in order to remain backwards
compatible.

Specifically, this mode can be configured globally for an entire test
suite via a new "spring.test.constructor.autowire" JVM system property
that can alternatively be configured via the SpringProperties
mechanism. In addition, the global "test constructor autowire" mode can
be overridden locally on a per-class basis via the new @TestConstructor
annotation.

Closes gh-22286
This commit is contained in:
Sam Brannen
2019-05-06 19:34:50 +02:00
parent eb16e853b1
commit 77eef47483
5 changed files with 402 additions and 14 deletions

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2002-2019 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.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite;
import org.springframework.test.context.junit.jupiter.comics.Dog;
import org.springframework.test.context.junit.jupiter.comics.Person;
import static org.junit.jupiter.api.Assertions.*;
/**
* Integration tests which demonstrate support for automatically
* {@link Autowired @Autowired} test class constructors in conjunction with the
* {@link TestConstructor @TestConstructor} annotation
*
* <p>To run these tests in an IDE that does not have built-in support for the JUnit
* Platform, simply run {@link SpringJUnitJupiterTestSuite} as a JUnit 4 test.
*
* @author Sam Brannen
* @since 5.2
* @see SpringExtension
* @see SpringJUnitJupiterAutowiredConstructorInjectionTests
* @see SpringJUnitJupiterConstructorInjectionTests
*/
@SpringJUnitConfig(TestConfig.class)
@TestPropertySource(properties = "enigma = 42")
@TestConstructor(autowire = true)
class TestConstructorAnnotationIntegrationTests {
final ApplicationContext applicationContext;
final Person dilbert;
final Dog dog;
final Integer enigma;
TestConstructorAnnotationIntegrationTests(ApplicationContext applicationContext, Person dilbert, Dog dog,
@Value("${enigma}") Integer enigma) {
this.applicationContext = applicationContext;
this.dilbert = dilbert;
this.dog = dog;
this.enigma = enigma;
}
@Test
void applicationContextInjected() {
assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring");
assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class));
}
@Test
void beansInjected() {
assertNotNull(this.dilbert, "Dilbert should have been @Autowired by Spring");
assertEquals("Dilbert", this.dilbert.getName(), "Person's name");
assertNotNull(this.dog, "Dogbert should have been @Autowired by Spring");
assertEquals("Dogbert", this.dog.getName(), "Dog's name");
}
@Test
void propertyPlaceholderInjected() {
assertNotNull(this.enigma, "Enigma should have been injected via @Value by Spring");
assertEquals(Integer.valueOf(42), this.enigma, "enigma");
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2002-2019 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 java.lang.reflect.Constructor;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestConstructor;
import static org.junit.Assert.*;
/**
* Unit tests for {@link TestConstructorUtils}.
*
* @author Sam Brannen
* @since 5.2
*/
public class TestConstructorUtilsTests {
@After
public void clearGlobalFlag() {
System.clearProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME);
}
@Test
public void notAutowirable() throws Exception {
assertNotAutowirable(NotAutowirableTestCase.class);
}
@Test
public void autowiredAnnotation() throws Exception {
assertAutowirable(AutowiredAnnotationTestCase.class);
}
@Test
public void testConstructorAnnotation() throws Exception {
assertAutowirable(TestConstructorAnnotationTestCase.class);
}
@Test
public void automaticallyAutowired() throws Exception {
setGlobalFlag();
assertAutowirable(AutomaticallyAutowiredTestCase.class);
}
@Test
public void automaticallyAutowiredButOverriddenLocally() throws Exception {
setGlobalFlag();
assertNotAutowirable(TestConstructorAnnotationOverridesGlobalFlagTestCase.class);
}
private void assertAutowirable(Class<?> testClass) throws NoSuchMethodException {
Constructor<?> constructor = testClass.getDeclaredConstructor();
assertTrue(TestConstructorUtils.isAutowirableConstructor(constructor, testClass));
}
private void assertNotAutowirable(Class<?> testClass) throws NoSuchMethodException {
Constructor<?> constructor = testClass.getDeclaredConstructor();
assertFalse(TestConstructorUtils.isAutowirableConstructor(constructor, testClass));
}
private void setGlobalFlag() {
System.setProperty(TestConstructor.TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME, "true");
}
static class NotAutowirableTestCase {
}
@TestConstructor(autowire = false)
static class AutowiredAnnotationTestCase {
@Autowired
AutowiredAnnotationTestCase() {
}
}
@TestConstructor(autowire = true)
static class TestConstructorAnnotationTestCase {
}
static class AutomaticallyAutowiredTestCase {
}
@TestConstructor(autowire = false)
static class TestConstructorAnnotationOverridesGlobalFlagTestCase {
}
}