Upgrade to JUnit Jupiter 5.0 M5

Issue: SPR-15728
This commit is contained in:
Sam Brannen
2017-07-04 14:47:10 +02:00
parent 9c93521512
commit 75a71accea
6 changed files with 101 additions and 100 deletions

View File

@@ -23,10 +23,10 @@ import java.util.function.Function;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ContainerExecutionCondition;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionCondition;
import org.springframework.beans.factory.config.BeanExpressionContext;
import org.springframework.beans.factory.config.BeanExpressionResolver;
@@ -39,9 +39,9 @@ import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Abstract base class for implementations of {@link ContainerExecutionCondition}
* and {@link TestExecutionCondition} that evaluate expressions configured via
* annotations to determine if a container or test is enabled.
* Abstract base class for implementations of {@link ExecutionCondition} that
* evaluate expressions configured via annotations to determine if a container
* or test is enabled.
*
* <p>Expressions can be any of the following.
*
@@ -61,7 +61,7 @@ import org.springframework.util.StringUtils;
* @see EnabledIf
* @see DisabledIf
*/
abstract class AbstractExpressionEvaluatingCondition implements ContainerExecutionCondition, TestExecutionCondition {
abstract class AbstractExpressionEvaluatingCondition implements ExecutionCondition {
private static final Log logger = LogFactory.getLog(AbstractExpressionEvaluatingCondition.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -17,24 +17,18 @@
package org.springframework.test.context.junit.jupiter;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ContainerExecutionCondition;
import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionCondition;
import org.junit.jupiter.api.extension.TestExtensionContext;
/**
* {@code DisabledIfCondition} is a composite {@link ContainerExecutionCondition}
* and {@link TestExecutionCondition} that supports the {@link DisabledIf @DisabledIf}
* annotation when using the <em>Spring TestContext Framework</em> in conjunction
* with JUnit 5's <em>Jupiter</em> programming model.
* {@code DisabledIfCondition} is an {@link ExecutionCondition} that supports the
* {@link DisabledIf @DisabledIf} annotation when using the <em>Spring TestContext
* Framework</em> in conjunction with JUnit 5's <em>Jupiter</em> programming model.
*
* <p>Any attempt to use the {@code DisabledIfCondition} without the presence of
* {@link DisabledIf @DisabledIf} will result in an <em>enabled</em>
* {@link ConditionEvaluationResult}.
*
* @author Sam Brannen
* @author Tadaya Tsuyukubo
* @since 5.0
* @see DisabledIf
* @see EnabledIf
@@ -43,26 +37,14 @@ import org.junit.jupiter.api.extension.TestExtensionContext;
public class DisabledIfCondition extends AbstractExpressionEvaluatingCondition {
/**
* Containers are disabled if {@code @DisabledIf} is present on the test class
* and the configured expression evaluates to {@code true}.
* Containers and tests are disabled if {@code @DisabledIf} is present on the
* corresponding test class or test method and the configured expression evaluates
* to {@code true}.
*/
@Override
public ConditionEvaluationResult evaluate(ContainerExtensionContext context) {
return evaluateDisabledIf(context);
}
/**
* Tests are disabled if {@code @DisabledIf} is present on the test method
* and the configured expression evaluates to {@code true}.
*/
@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
return evaluateDisabledIf(context);
}
private ConditionEvaluationResult evaluateDisabledIf(ExtensionContext context) {
return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason,
DisabledIf::loadContext, false, context);
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason, DisabledIf::loadContext,
false, context);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -17,17 +17,12 @@
package org.springframework.test.context.junit.jupiter;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ContainerExecutionCondition;
import org.junit.jupiter.api.extension.ContainerExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionCondition;
import org.junit.jupiter.api.extension.TestExtensionContext;
/**
* {@code EnabledIfCondition} is a composite {@link ContainerExecutionCondition}
* and {@link TestExecutionCondition} that supports the {@link EnabledIf @EnabledIf}
* annotation when using the <em>Spring TestContext Framework</em> in conjunction
* with JUnit 5's <em>Jupiter</em> programming model.
* {@code EnabledIfCondition} is an {@link ExecutionCondition} that supports the
* {@link EnabledIf @EnabledIf} annotation when using the <em>Spring TestContext
* Framework</em> in conjunction with JUnit 5's <em>Jupiter</em> programming model.
*
* <p>Any attempt to use the {@code EnabledIfCondition} without the presence of
* {@link EnabledIf @EnabledIf} will result in an <em>enabled</em>
@@ -42,26 +37,14 @@ import org.junit.jupiter.api.extension.TestExtensionContext;
public class EnabledIfCondition extends AbstractExpressionEvaluatingCondition {
/**
* Containers are enabled if {@code @EnabledIf} is present on the test class
* and the configured expression evaluates to {@code true}.
* Containers and tests are enabled if {@code @EnabledIf} is present on the
* corresponding test class or test method and the configured expression
* evaluates to {@code true}.
*/
@Override
public ConditionEvaluationResult evaluate(ContainerExtensionContext context) {
return evaluateEnabledIf(context);
}
/**
* Tests are enabled if {@code @EnabledIf} is present on the test method
* and the configured expression evaluates to {@code true}.
*/
@Override
public ConditionEvaluationResult evaluate(TestExtensionContext context) {
return evaluateEnabledIf(context);
}
private ConditionEvaluationResult evaluateEnabledIf(ExtensionContext context) {
return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason,
EnabledIf::loadContext, true, context);
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason, EnabledIf::loadContext,
true, context);
}
}

View File

@@ -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;
*
* @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
* 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
* 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
* 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
* 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
* 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
* 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
* 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
* 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
*/
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"));
}
}