diff --git a/spring-test/src/main/java/org/springframework/test/annotation/Repeat.java b/spring-test/src/main/java/org/springframework/test/annotation/Repeat.java index e81bb9c856..dfa062ceb2 100644 --- a/spring-test/src/main/java/org/springframework/test/annotation/Repeat.java +++ b/spring-test/src/main/java/org/springframework/test/annotation/Repeat.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -28,7 +28,11 @@ import java.lang.annotation.Target; * *

Note that the scope of execution to be repeated includes execution of the * test method itself as well as any set up or tear down of - * the test fixture. + * the test fixture. When used with the + * {@link org.springframework.test.context.junit4.rules.SpringMethodRule + * SpringMethodRule}, the scope additionally includes + * {@linkplain org.springframework.test.context.TestExecutionListener#prepareTestInstance + * preparation of the test instance}. * *

This annotation may be used as a meta-annotation to create custom * composed annotations. diff --git a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java index 30d67923e4..d93b78d88d 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestContextManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 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. @@ -221,8 +221,12 @@ public class TestContextManager { /** * Hook for preparing a test instance prior to execution of any individual - * test methods, for example for injecting dependencies, etc. Should be - * called immediately after instantiation of the test instance. + * test methods — for example, to inject dependencies. + *

This method should be called immediately after instantiation of the test + * class or as soon after instantiation as possible (as is the case with the + * {@link org.springframework.test.context.junit4.rules.SpringMethodRule + * SpringMethodRule}). In any case, this method must be called prior to any + * framework-specific lifecycle callbacks. *

The managed {@link TestContext} will be updated with the supplied * {@code testInstance}. *

An attempt will be made to give each registered diff --git a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java index a9b188e6e9..e10d2422bd 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -86,10 +86,14 @@ public interface TestExecutionListener { } /** - * Prepares the {@link Object test instance} of the supplied - * {@link TestContext test context}, for example by injecting dependencies. + * Prepares the {@linkplain Object test instance} of the supplied + * {@linkplain TestContext test context} — for example, to inject + * dependencies. *

This method should be called immediately after instantiation of the test - * instance but prior to any framework-specific lifecycle callbacks. + * class or as soon after instantiation as possible (as is the case with the + * {@link org.springframework.test.context.junit4.rules.SpringMethodRule + * SpringMethodRule}). In any case, this method must be called prior to any + * framework-specific lifecycle callbacks. *

The default implementation is empty. Can be overridden by * concrete classes as necessary. * @param testContext the test context for the test; never {@code null} @@ -123,8 +127,8 @@ public interface TestExecutionListener { /** * Pre-processes a test immediately before execution of the - * {@link java.lang.reflect.Method test method} in the supplied - * {@link TestContext test context} — for example, for timing + * {@linkplain java.lang.reflect.Method test method} in the supplied + * {@linkplain TestContext test context} — for example, for timing * or logging purposes. *

This method must be called after framework-specific * before lifecycle callbacks. @@ -143,8 +147,8 @@ public interface TestExecutionListener { /** * Post-processes a test immediately after execution of the - * {@link java.lang.reflect.Method test method} in the supplied - * {@link TestContext test context} — for example, for timing + * {@linkplain java.lang.reflect.Method test method} in the supplied + * {@linkplain TestContext test context} — for example, for timing * or logging purposes. *

This method must be called before framework-specific * after lifecycle callbacks. diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java b/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java index 639ee9d6f6..57478d4bd8 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -79,6 +79,10 @@ import org.springframework.test.context.junit4.statements.SpringRepeat; *

NOTE: As of Spring Framework 4.3, this class requires JUnit 4.12 or higher. * *

WARNING: Due to the shortcomings of JUnit rules, the + * {@code SpringMethodRule} + * {@linkplain org.springframework.test.context.TestExecutionListener#prepareTestInstance + * prepares the test instance} before {@code @Before} lifecycle methods instead of + * immediately after instantiation of the test class. In addition, the * {@code SpringMethodRule} does not support the * {@code beforeTestExecution()} and {@code afterTestExecution()} callbacks of the * {@link org.springframework.test.context.TestExecutionListener TestExecutionListener} diff --git a/src/docs/asciidoc/testing.adoc b/src/docs/asciidoc/testing.adoc index 81455ea5ac..90c463fbe6 100644 --- a/src/docs/asciidoc/testing.adoc +++ b/src/docs/asciidoc/testing.adoc @@ -1657,8 +1657,10 @@ before failing. times that the test method is to be run is specified in the annotation. The scope of execution to be repeated includes execution of the test method itself as -well as any setting up or tearing down of the test fixture. The following example shows -how to use the `@Repeat` annotation: +well as any setting up or tearing down of the test fixture. When used with the +<>, the scope additionally includes +preparation of the test instance by `TestExecutionListener` implementations. The +following example shows how to use the `@Repeat` annotation: [source,java,indent=0,subs="verbatim,quotes",role="primary"] .Java