Skip to content

Commit

Permalink
Support parameter injection in @[Before|After]Transaction methods
Browse files Browse the repository at this point in the history
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 spring-projectsgh-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 spring-projectsgh-30736
  • Loading branch information
sbrannen committed Sep 10, 2023
1 parent 41904d4 commit ed83461
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ following features above and beyond the feature set that Spring supports for JUn
TestNG:

* Dependency injection for test constructors, test methods, and test lifecycle callback
methods. See xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-di[Dependency Injection with `SpringExtension`] for further details.
methods. See xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-di[Dependency Injection with the `SpringExtension`] for further details.
* Powerful support for link:https://junit.org/junit5/docs/current/user-guide/#extensions-conditions[conditional
test execution] based on SpEL expressions, environment variables, system properties,
and so on. See the documentation for `@EnabledIf` and `@DisabledIf` in
Expand Down Expand Up @@ -310,17 +310,19 @@ See the documentation for `@SpringJUnitConfig` and `@SpringJUnitWebConfig` in
xref:testing/annotations/integration-junit-jupiter.adoc[Spring JUnit Jupiter Testing Annotations] for further details.

[[testcontext-junit-jupiter-di]]
=== Dependency Injection with `SpringExtension`
=== Dependency Injection with the `SpringExtension`

`SpringExtension` implements the
The `SpringExtension` implements the
link:https://junit.org/junit5/docs/current/user-guide/#extensions-parameter-resolution[`ParameterResolver`]
extension API from JUnit Jupiter, which lets Spring provide dependency injection for test
constructors, test methods, and test lifecycle callback methods.

Specifically, `SpringExtension` can inject dependencies from the test's
`ApplicationContext` into test constructors and methods that are annotated with
`@BeforeAll`, `@AfterAll`, `@BeforeEach`, `@AfterEach`, `@Test`, `@RepeatedTest`,
`@ParameterizedTest`, and others.
Specifically, the `SpringExtension` can inject dependencies from the test's
`ApplicationContext` into into test constructors and methods that are annotated with
Spring's `@BeforeTransaction` and `@AfterTransaction` or JUnit's `@BeforeAll`,
`@AfterAll`, `@BeforeEach`, `@AfterEach`, `@Test`, `@RepeatedTest`, `@ParameterizedTest`,
and others.


[[testcontext-junit-jupiter-di-constructor]]
==== Constructor Injection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,44 @@ method in a test class or any `void` default method in a test interface with one
annotations, and the `TransactionalTestExecutionListener` ensures that your
before-transaction method or after-transaction method runs at the appropriate time.

[NOTE]
====
Generally speaking, `@BeforeTransaction` and `@AfterTransaction` methods must not accept
any arguments.
However, as of Spring Framework 6.1, for tests using the
xref:testing/testcontext-framework/support-classes.adoc#testcontext-junit-jupiter-extension[`SpringExtension`]
with JUnit Jupiter, `@BeforeTransaction` and `@AfterTransaction` methods may optionally
accept arguments which will be resolved by any registered JUnit `ParameterResolver`
extension such as the `SpringExtension`. This means that JUnit-specific arguments like
`TestInfo` or beans from the test's `ApplicationContext` may be provided to
`@BeforeTransaction` and `@AfterTransaction` methods, as demonstrated in the following
example.
[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
@BeforeTransaction
void verifyInitialDatabaseState(@Autowired DataSource dataSource) {
// Use the DataSource to verify the initial state before a transaction is started
}
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
----
@BeforeTransaction
fun verifyInitialDatabaseState(@Autowired dataSource: DataSource) {
// Use the DataSource to verify the initial state before a transaction is started
}
----
======
====

[TIP]
====
Any before methods (such as methods annotated with JUnit Jupiter's `@BeforeEach`) and any
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,20 +23,28 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -23,20 +23,28 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -287,8 +287,7 @@ else if (logger.isDebugEnabled()) {
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) {
Expand Down Expand Up @@ -323,8 +322,7 @@ else if (logger.isDebugEnabled()) {
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.junit.jupiter.transaction;

import javax.sql.DataSource;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;

/**
* JUnit Jupiter based integration tests which verify support for parameter
* injection in {@link BeforeTransaction @BeforeTransaction} and
* {@link AfterTransaction @AfterTransaction} lifecycle methods.
*
* @author Sam Brannen
* @since 6.1
*/
@SpringJUnitConfig
class TransactionLifecycleMethodParameterInjectionTests {

static boolean beforeTransactionInvoked = false;
static boolean afterTransactionInvoked = false;


@BeforeAll
static void checkInitialFlagState() {
assertThat(beforeTransactionInvoked).isFalse();
assertThat(afterTransactionInvoked).isFalse();
}

@BeforeTransaction
void beforeTransaction(TestInfo testInfo, ApplicationContext context, @Autowired DataSource dataSource) {
assertThatTransaction().isNotActive();
assertThat(testInfo).isNotNull();
assertThat(context).isNotNull();
assertThat(dataSource).isNotNull();
beforeTransactionInvoked = true;
}

@Test
@Transactional
void transactionalTest(TestInfo testInfo, ApplicationContext context, @Autowired DataSource dataSource) {
assertThatTransaction().isActive();
assertThat(testInfo).isNotNull();
assertThat(context).isNotNull();
assertThat(dataSource).isNotNull();
assertThat(beforeTransactionInvoked).isTrue();
assertThat(afterTransactionInvoked).isFalse();
}

@AfterTransaction
void afterTransaction(TestInfo testInfo, ApplicationContext context, @Autowired DataSource dataSource) {
assertThatTransaction().isNotActive();
assertThat(testInfo).isNotNull();
assertThat(context).isNotNull();
assertThat(dataSource).isNotNull();
afterTransactionInvoked = true;
}

@AfterAll
static void checkFinalFlagState() {
assertThat(beforeTransactionInvoked).isTrue();
assertThat(afterTransactionInvoked).isTrue();
}


@Configuration
static class Config {

@Bean
DataSourceTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean
DataSource dataSource() {
return new EmbeddedDatabaseBuilder().generateUniqueName(true).build();
}
}

}

0 comments on commit ed83461

Please sign in to comment.