|
|
|
|
@ -27,13 +27,11 @@ import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
@@ -27,13 +27,11 @@ import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
|
|
|
|
|
import org.junit.jupiter.api.extension.BeforeAllCallback; |
|
|
|
|
import org.junit.jupiter.api.extension.BeforeEachCallback; |
|
|
|
|
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; |
|
|
|
|
import org.junit.jupiter.api.extension.ContainerExtensionContext; |
|
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext; |
|
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; |
|
|
|
|
import org.junit.jupiter.api.extension.ExtensionContext.Store; |
|
|
|
|
import org.junit.jupiter.api.extension.ParameterContext; |
|
|
|
|
import org.junit.jupiter.api.extension.ParameterResolver; |
|
|
|
|
import org.junit.jupiter.api.extension.TestExtensionContext; |
|
|
|
|
import org.junit.jupiter.api.extension.TestInstancePostProcessor; |
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
@ -52,6 +50,7 @@ import org.springframework.util.Assert;
@@ -52,6 +50,7 @@ import org.springframework.util.Assert;
|
|
|
|
|
* |
|
|
|
|
* @author Sam Brannen |
|
|
|
|
* @since 5.0 |
|
|
|
|
* @see org.springframework.test.context.junit.jupiter.EnabledIf |
|
|
|
|
* @see org.springframework.test.context.junit.jupiter.DisabledIf |
|
|
|
|
* @see org.springframework.test.context.junit.jupiter.SpringJUnitConfig |
|
|
|
|
* @see org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig |
|
|
|
|
@ -72,7 +71,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -72,7 +71,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Delegates to {@link TestContextManager#beforeTestClass}. |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void beforeAll(ContainerExtensionContext context) throws Exception { |
|
|
|
|
public void beforeAll(ExtensionContext context) throws Exception { |
|
|
|
|
getTestContextManager(context).beforeTestClass(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -80,12 +79,12 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -80,12 +79,12 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Delegates to {@link TestContextManager#afterTestClass}. |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void afterAll(ContainerExtensionContext context) throws Exception { |
|
|
|
|
public void afterAll(ExtensionContext context) throws Exception { |
|
|
|
|
try { |
|
|
|
|
getTestContextManager(context).afterTestClass(); |
|
|
|
|
} |
|
|
|
|
finally { |
|
|
|
|
context.getStore(NAMESPACE).remove(context.getTestClass().get()); |
|
|
|
|
context.getStore(NAMESPACE).remove(getRequiredTestClass(context)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -101,9 +100,9 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -101,9 +100,9 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Delegates to {@link TestContextManager#beforeTestMethod}. |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void beforeEach(TestExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = context.getTestInstance(); |
|
|
|
|
Method testMethod = context.getTestMethod().get(); |
|
|
|
|
public void beforeEach(ExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = getRequiredTestInstance(context); |
|
|
|
|
Method testMethod = getRequiredTestMethod(context); |
|
|
|
|
getTestContextManager(context).beforeTestMethod(testInstance, testMethod); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -111,9 +110,9 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -111,9 +110,9 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Delegates to {@link TestContextManager#beforeTestExecution}. |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void beforeTestExecution(TestExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = context.getTestInstance(); |
|
|
|
|
Method testMethod = context.getTestMethod().get(); |
|
|
|
|
public void beforeTestExecution(ExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = getRequiredTestInstance(context); |
|
|
|
|
Method testMethod = getRequiredTestMethod(context); |
|
|
|
|
getTestContextManager(context).beforeTestExecution(testInstance, testMethod); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -121,10 +120,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -121,10 +120,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Delegates to {@link TestContextManager#afterTestExecution}. |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void afterTestExecution(TestExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = context.getTestInstance(); |
|
|
|
|
Method testMethod = context.getTestMethod().get(); |
|
|
|
|
Throwable testException = context.getTestException().orElse(null); |
|
|
|
|
public void afterTestExecution(ExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = getRequiredTestInstance(context); |
|
|
|
|
Method testMethod = getRequiredTestMethod(context); |
|
|
|
|
Throwable testException = context.getExecutionException().orElse(null); |
|
|
|
|
getTestContextManager(context).afterTestExecution(testInstance, testMethod, testException); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -132,10 +131,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -132,10 +131,10 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Delegates to {@link TestContextManager#afterTestMethod}. |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void afterEach(TestExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = context.getTestInstance(); |
|
|
|
|
Method testMethod = context.getTestMethod().get(); |
|
|
|
|
Throwable testException = context.getTestException().orElse(null); |
|
|
|
|
public void afterEach(ExtensionContext context) throws Exception { |
|
|
|
|
Object testInstance = getRequiredTestInstance(context); |
|
|
|
|
Method testMethod = getRequiredTestMethod(context); |
|
|
|
|
Throwable testException = context.getExecutionException().orElse(null); |
|
|
|
|
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -149,11 +148,11 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -149,11 +148,11 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* that is annotated with {@code @Autowired}, Spring will assume the responsibility |
|
|
|
|
* for resolving all parameters in the constructor. Consequently, no other registered |
|
|
|
|
* {@link ParameterResolver} will be able to resolve parameters. |
|
|
|
|
* @see #resolve |
|
|
|
|
* @see #resolveParameter |
|
|
|
|
* @see ParameterAutowireUtils#isAutowirable |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) { |
|
|
|
|
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { |
|
|
|
|
Parameter parameter = parameterContext.getParameter(); |
|
|
|
|
Executable executable = parameter.getDeclaringExecutable(); |
|
|
|
|
return (executable instanceof Constructor && |
|
|
|
|
@ -165,14 +164,14 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -165,14 +164,14 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
* Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by |
|
|
|
|
* retrieving the corresponding dependency from the test's {@link ApplicationContext}. |
|
|
|
|
* <p>Delegates to {@link ParameterAutowireUtils#resolveDependency}. |
|
|
|
|
* @see #supports |
|
|
|
|
* @see #supportsParameter |
|
|
|
|
* @see ParameterAutowireUtils#resolveDependency |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
@Nullable |
|
|
|
|
public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) { |
|
|
|
|
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) { |
|
|
|
|
Parameter parameter = parameterContext.getParameter(); |
|
|
|
|
Class<?> testClass = extensionContext.getTestClass().get(); |
|
|
|
|
Class<?> testClass = getRequiredTestClass(extensionContext); |
|
|
|
|
ApplicationContext applicationContext = getApplicationContext(extensionContext); |
|
|
|
|
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext); |
|
|
|
|
} |
|
|
|
|
@ -195,9 +194,45 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
@@ -195,9 +194,45 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
|
|
|
|
*/ |
|
|
|
|
private static TestContextManager getTestContextManager(ExtensionContext context) { |
|
|
|
|
Assert.notNull(context, "ExtensionContext must not be null"); |
|
|
|
|
Class<?> testClass = context.getTestClass().get(); |
|
|
|
|
Class<?> testClass = getRequiredTestClass(context); |
|
|
|
|
Store store = context.getStore(NAMESPACE); |
|
|
|
|
return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the test class associated with the supplied {@code ExtensionContext}. |
|
|
|
|
* @return the test class
|
|
|
|
|
* @throws IllegalStateException if the extension context does not contain |
|
|
|
|
* a test class
|
|
|
|
|
*/ |
|
|
|
|
private static Class<?> getRequiredTestClass(ExtensionContext context) throws IllegalStateException { |
|
|
|
|
Assert.notNull(context, "ExtensionContext must not be null"); |
|
|
|
|
return context.getTestClass().orElseThrow( |
|
|
|
|
() -> new IllegalStateException("JUnit failed to supply the test class in the ExtensionContext")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the test instance associated with the supplied {@code ExtensionContext}. |
|
|
|
|
* @return the test instance |
|
|
|
|
* @throws IllegalStateException if the extension context does not contain |
|
|
|
|
* a test instance |
|
|
|
|
*/ |
|
|
|
|
private static Object getRequiredTestInstance(ExtensionContext context) throws IllegalStateException { |
|
|
|
|
Assert.notNull(context, "ExtensionContext must not be null"); |
|
|
|
|
return context.getTestInstance().orElseThrow( |
|
|
|
|
() -> new IllegalStateException("JUnit failed to supply the test instance in the ExtensionContext")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the test method associated with the supplied {@code ExtensionContext}. |
|
|
|
|
* @return the test method |
|
|
|
|
* @throws IllegalStateException if the extension context does not contain |
|
|
|
|
* a test method |
|
|
|
|
*/ |
|
|
|
|
private static Method getRequiredTestMethod(ExtensionContext context) throws IllegalStateException { |
|
|
|
|
Assert.notNull(context, "ExtensionContext must not be null"); |
|
|
|
|
return context.getTestMethod().orElseThrow( |
|
|
|
|
() -> new IllegalStateException("JUnit failed to supply the test method in the ExtensionContext")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|