Support parameter injection in @[Before|After]Transaction methods
Prior to this commit, @BeforeTransaction and @AfterTransaction
methods could not accept any arguments. This is acceptable with testing
frameworks such as JUnit 4 and TestNG that do not provide support for
parameter injection. However, users of JUnit Jupiter have become
accustomed to being able to accept arguments in lifecycle methods
annotated with JUnit's @BeforeEach, @AfterEach, etc.
As a follow up to the previous commit (see gh-31199), this commit
introduces first-class support for parameter injection in
@BeforeTransaction and @AfterTransaction methods, as demonstrated in
the following example.
@BeforeTransaction
void verifyInitialDatabaseState(@Autowired DataSource dataSource) {
// Use the DataSource to verify the initial DB state
}
Closes gh-30736
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -23,20 +23,28 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <p>Test annotation which indicates that the annotated {@code void} method
|
||||
* Test annotation which indicates that the annotated {@code void} method
|
||||
* should be executed <em>after</em> a transaction is ended for a test method
|
||||
* configured to run within a transaction via Spring's {@code @Transactional}
|
||||
* annotation.
|
||||
*
|
||||
* <p>Generally speaking, {@code @AfterTransaction} methods must not accept any
|
||||
* arguments. However, as of Spring Framework 6.1, for tests using the
|
||||
* {@link org.springframework.test.context.junit.jupiter.SpringExtension SpringExtension}
|
||||
* with JUnit Jupiter, {@code @AfterTransaction} methods may optionally accept
|
||||
* arguments which will be resolved by any registered JUnit
|
||||
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolver}
|
||||
* extension such as the {@code SpringExtension}. This means that JUnit-specific
|
||||
* arguments like {@link org.junit.jupiter.api.TestInfo TestInfo} or beans from
|
||||
* the test's {@code ApplicationContext} may be provided to {@code @AfterTransaction}
|
||||
* methods analogous to {@code @AfterEach} methods.
|
||||
*
|
||||
* <p>{@code @AfterTransaction} methods declared in superclasses or as interface
|
||||
* default methods will be executed after those of the current test class.
|
||||
*
|
||||
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
|
||||
* <em>composed annotations</em>.
|
||||
*
|
||||
* <p>As of Spring Framework 4.3, {@code @AfterTransaction} may also be
|
||||
* declared on Java 8 based interface default methods.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see org.springframework.transaction.annotation.Transactional
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -23,20 +23,28 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* <p>Test annotation which indicates that the annotated {@code void} method
|
||||
* Test annotation which indicates that the annotated {@code void} method
|
||||
* should be executed <em>before</em> a transaction is started for a test method
|
||||
* configured to run within a transaction via Spring's {@code @Transactional}
|
||||
* annotation.
|
||||
*
|
||||
* <p>Generally speaking, {@code @BeforeTransaction} methods must not accept any
|
||||
* arguments. However, as of Spring Framework 6.1, for tests using the
|
||||
* {@link org.springframework.test.context.junit.jupiter.SpringExtension SpringExtension}
|
||||
* with JUnit Jupiter, {@code @BeforeTransaction} methods may optionally accept
|
||||
* arguments which will be resolved by any registered JUnit
|
||||
* {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolver}
|
||||
* extension such as the {@code SpringExtension}. This means that JUnit-specific
|
||||
* arguments like {@link org.junit.jupiter.api.TestInfo TestInfo} or beans from
|
||||
* the test's {@code ApplicationContext} may be provided to {@code @BeforeTransaction}
|
||||
* methods analogous to {@code @BeforeEach} methods.
|
||||
*
|
||||
* <p>{@code @BeforeTransaction} methods declared in superclasses or as interface
|
||||
* default methods will be executed before those of the current test class.
|
||||
*
|
||||
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
|
||||
* <em>composed annotations</em>.
|
||||
*
|
||||
* <p>As of Spring Framework 4.3, {@code @BeforeTransaction} may also be
|
||||
* declared on Java 8 based interface default methods.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 2.5
|
||||
* @see org.springframework.transaction.annotation.Transactional
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2022 the original author or authors.
|
||||
* Copyright 2002-2023 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.
|
||||
@@ -287,8 +287,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
logger.debug("Executing @BeforeTransaction method [%s] for test class [%s]"
|
||||
.formatted(method, testClass.getName()));
|
||||
}
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
method.invoke(testContext.getTestInstance());
|
||||
testContext.getMethodInvoker().invoke(method, testContext.getTestInstance());
|
||||
}
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
@@ -323,8 +322,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
logger.debug("Executing @AfterTransaction method [%s] for test class [%s]"
|
||||
.formatted(method, testClass.getName()));
|
||||
}
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
method.invoke(testContext.getTestInstance());
|
||||
testContext.getMethodInvoker().invoke(method, testContext.getTestInstance());
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
Throwable targetException = ex.getTargetException();
|
||||
|
||||
Reference in New Issue
Block a user