|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.junit.jupiter; |
| 18 | + |
| 19 | +import java.lang.reflect.Constructor; |
| 20 | +import java.lang.reflect.Executable; |
| 21 | +import java.lang.reflect.Method; |
| 22 | +import java.lang.reflect.Parameter; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 25 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 26 | +import org.junit.jupiter.api.extension.BeforeAllCallback; |
| 27 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 28 | +import org.junit.jupiter.api.extension.ContainerExtensionContext; |
| 29 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 30 | +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
| 31 | +import org.junit.jupiter.api.extension.ExtensionContext.Store; |
| 32 | +import org.junit.jupiter.api.extension.ParameterContext; |
| 33 | +import org.junit.jupiter.api.extension.ParameterResolver; |
| 34 | +import org.junit.jupiter.api.extension.TestExtensionContext; |
| 35 | +import org.junit.jupiter.api.extension.TestInstancePostProcessor; |
| 36 | + |
| 37 | +import org.springframework.beans.factory.annotation.Autowired; |
| 38 | +import org.springframework.context.ApplicationContext; |
| 39 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 40 | +import org.springframework.test.context.TestContextManager; |
| 41 | +import org.springframework.test.context.junit.jupiter.support.ParameterAutowireUtils; |
| 42 | +import org.springframework.util.Assert; |
| 43 | + |
| 44 | +/** |
| 45 | + * {@code SpringExtension} integrates the <em>Spring TestContext Framework</em> |
| 46 | + * into JUnit 5's <em>Jupiter</em> programming model. |
| 47 | + * |
| 48 | + * <p>To use this class, simply annotate a JUnit Jupiter based test class with |
| 49 | + * {@code @ExtendWith(SpringExtension.class)}. |
| 50 | + * |
| 51 | + * @author Sam Brannen |
| 52 | + * @since 5.0 |
| 53 | + * @see org.springframework.test.context.junit.jupiter.SpringJUnitConfig |
| 54 | + * @see org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig |
| 55 | + * @see org.springframework.test.context.TestContextManager |
| 56 | + */ |
| 57 | +public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, |
| 58 | + BeforeEachCallback, AfterEachCallback, ParameterResolver { |
| 59 | + |
| 60 | + /** |
| 61 | + * {@link Namespace} in which {@code TestContextManagers} are stored, keyed |
| 62 | + * by test class. |
| 63 | + */ |
| 64 | + private static final Namespace namespace = Namespace.create(SpringExtension.class); |
| 65 | + |
| 66 | + /** |
| 67 | + * Delegates to {@link TestContextManager#beforeTestClass}. |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public void beforeAll(ContainerExtensionContext context) throws Exception { |
| 71 | + getTestContextManager(context).beforeTestClass(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Delegates to {@link TestContextManager#afterTestClass}. |
| 76 | + */ |
| 77 | + @Override |
| 78 | + public void afterAll(ContainerExtensionContext context) throws Exception { |
| 79 | + try { |
| 80 | + getTestContextManager(context).afterTestClass(); |
| 81 | + } |
| 82 | + finally { |
| 83 | + context.getStore(namespace).remove(context.getTestClass().get()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Delegates to {@link TestContextManager#prepareTestInstance}. |
| 89 | + */ |
| 90 | + @Override |
| 91 | + public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { |
| 92 | + getTestContextManager(context).prepareTestInstance(testInstance); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Delegates to {@link TestContextManager#beforeTestMethod}. |
| 97 | + */ |
| 98 | + @Override |
| 99 | + public void beforeEach(TestExtensionContext context) throws Exception { |
| 100 | + Object testInstance = context.getTestInstance(); |
| 101 | + Method testMethod = context.getTestMethod().get(); |
| 102 | + getTestContextManager(context).beforeTestMethod(testInstance, testMethod); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Delegates to {@link TestContextManager#afterTestMethod}. |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public void afterEach(TestExtensionContext context) throws Exception { |
| 110 | + Object testInstance = context.getTestInstance(); |
| 111 | + Method testMethod = context.getTestMethod().get(); |
| 112 | + Throwable testException = context.getTestException().orElse(null); |
| 113 | + getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Determine if the value for the {@link Parameter} in the supplied |
| 118 | + * {@link ParameterContext} should be autowired from the test's |
| 119 | + * {@link ApplicationContext}. |
| 120 | + * <p>Returns {@code true} if the parameter is declared in a {@link Constructor} |
| 121 | + * that is annotated with {@link Autowired @Autowired} and otherwise delegates |
| 122 | + * to {@link ParameterAutowireUtils#isAutowirable}. |
| 123 | + * <p><strong>WARNING</strong>: if the parameter is declared in a {@code Constructor} |
| 124 | + * that is annotated with {@code @Autowired}, Spring will assume the responsibility |
| 125 | + * for resolving all parameters in the constructor. Consequently, no other |
| 126 | + * registered {@link ParameterResolver} will be able to resolve parameters. |
| 127 | + * |
| 128 | + * @see #resolve |
| 129 | + * @see ParameterAutowireUtils#isAutowirable |
| 130 | + */ |
| 131 | + @Override |
| 132 | + public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { |
| 133 | + Parameter parameter = parameterContext.getParameter(); |
| 134 | + Executable executable = parameter.getDeclaringExecutable(); |
| 135 | + return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) |
| 136 | + || ParameterAutowireUtils.isAutowirable(parameter); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Resolve a value for the {@link Parameter} in the supplied |
| 141 | + * {@link ParameterContext} by retrieving the corresponding dependency |
| 142 | + * from the test's {@link ApplicationContext}. |
| 143 | + * <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}. |
| 144 | + * @see #supports |
| 145 | + * @see ParameterAutowireUtils#resolveDependency |
| 146 | + */ |
| 147 | + @Override |
| 148 | + public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) { |
| 149 | + Parameter parameter = parameterContext.getParameter(); |
| 150 | + Class<?> testClass = extensionContext.getTestClass().get(); |
| 151 | + ApplicationContext applicationContext = getApplicationContext(extensionContext); |
| 152 | + return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get the {@link ApplicationContext} associated with the supplied |
| 157 | + * {@code ExtensionContext}. |
| 158 | + * @param context the current {@code ExtensionContext}; never {@code null} |
| 159 | + * @return the application context |
| 160 | + * @throws IllegalStateException if an error occurs while retrieving the |
| 161 | + * application context |
| 162 | + * @see org.springframework.test.context.TestContext#getApplicationContext() |
| 163 | + */ |
| 164 | + private ApplicationContext getApplicationContext(ExtensionContext context) { |
| 165 | + return getTestContextManager(context).getTestContext().getApplicationContext(); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Get the {@link TestContextManager} associated with the supplied |
| 170 | + * {@code ExtensionContext}. |
| 171 | + * @return the {@code TestContextManager}; never {@code null} |
| 172 | + */ |
| 173 | + private TestContextManager getTestContextManager(ExtensionContext context) { |
| 174 | + Assert.notNull(context, "ExtensionContext must not be null"); |
| 175 | + Class<?> testClass = context.getTestClass().get(); |
| 176 | + Store store = context.getStore(namespace); |
| 177 | + return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments