|
| 1 | +package com.baeldung.jupiter; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.extension.*; |
| 4 | +import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.context.ApplicationContext; |
| 6 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 7 | +import org.springframework.test.context.TestContextManager; |
| 8 | +import org.springframework.util.Assert; |
| 9 | + |
| 10 | +import java.lang.reflect.Constructor; |
| 11 | +import java.lang.reflect.Executable; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.lang.reflect.Parameter; |
| 14 | + |
| 15 | +public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver { |
| 16 | + |
| 17 | + private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class); |
| 18 | + |
| 19 | + @Override |
| 20 | + public void beforeAll(ContainerExtensionContext context) throws Exception { |
| 21 | + getTestContextManager(context).beforeTestClass(); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public void afterAll(ContainerExtensionContext context) throws Exception { |
| 26 | + try { |
| 27 | + getTestContextManager(context).afterTestClass(); |
| 28 | + } finally { |
| 29 | + context |
| 30 | + .getStore(namespace) |
| 31 | + .remove(context |
| 32 | + .getTestClass() |
| 33 | + .get()); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { |
| 39 | + getTestContextManager(context).prepareTestInstance(testInstance); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void beforeEach(TestExtensionContext context) throws Exception { |
| 44 | + Object testInstance = context.getTestInstance(); |
| 45 | + Method testMethod = context |
| 46 | + .getTestMethod() |
| 47 | + .get(); |
| 48 | + getTestContextManager(context).beforeTestMethod(testInstance, testMethod); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void afterEach(TestExtensionContext context) throws Exception { |
| 53 | + Object testInstance = context.getTestInstance(); |
| 54 | + Method testMethod = context |
| 55 | + .getTestMethod() |
| 56 | + .get(); |
| 57 | + Throwable testException = context |
| 58 | + .getTestException() |
| 59 | + .orElse(null); |
| 60 | + getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { |
| 65 | + Parameter parameter = parameterContext.getParameter(); |
| 66 | + Executable executable = parameter.getDeclaringExecutable(); |
| 67 | + return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) { |
| 72 | + Parameter parameter = parameterContext.getParameter(); |
| 73 | + Class<?> testClass = extensionContext |
| 74 | + .getTestClass() |
| 75 | + .get(); |
| 76 | + ApplicationContext applicationContext = getApplicationContext(extensionContext); |
| 77 | + return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext); |
| 78 | + } |
| 79 | + |
| 80 | + private ApplicationContext getApplicationContext(ExtensionContext context) { |
| 81 | + return getTestContextManager(context) |
| 82 | + .getTestContext() |
| 83 | + .getApplicationContext(); |
| 84 | + } |
| 85 | + |
| 86 | + private TestContextManager getTestContextManager(ExtensionContext context) { |
| 87 | + Assert.notNull(context, "ExtensionContext must not be null"); |
| 88 | + Class<?> testClass = context |
| 89 | + .getTestClass() |
| 90 | + .get(); |
| 91 | + ExtensionContext.Store store = context.getStore(namespace); |
| 92 | + return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class); |
| 93 | + } |
| 94 | +} |
0 commit comments