Skip to content

Commit 21c0e50

Browse files
committed
Document when prepareTestInstance() is invoked by the SpringMethodRule
Closes gh-27305
1 parent cecd2ee commit 21c0e50

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

spring-test/src/main/java/org/springframework/test/annotation/Repeat.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,11 @@
2828
*
2929
* <p>Note that the scope of execution to be repeated includes execution of the
3030
* test method itself as well as any <em>set up</em> or <em>tear down</em> of
31-
* the test fixture.
31+
* the test fixture. When used with the
32+
* {@link org.springframework.test.context.junit4.rules.SpringMethodRule
33+
* SpringMethodRule}, the scope additionally includes
34+
* {@linkplain org.springframework.test.context.TestExecutionListener#prepareTestInstance
35+
* preparation of the test instance}.
3236
*
3337
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
3438
* <em>composed annotations</em>.

spring-test/src/main/java/org/springframework/test/context/TestContextManager.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -221,8 +221,12 @@ public void beforeTestClass() throws Exception {
221221

222222
/**
223223
* Hook for preparing a test instance prior to execution of any individual
224-
* test methods, for example for injecting dependencies, etc. Should be
225-
* called immediately after instantiation of the test instance.
224+
* test methods &mdash; for example, to inject dependencies.
225+
* <p>This method should be called immediately after instantiation of the test
226+
* class or as soon after instantiation as possible (as is the case with the
227+
* {@link org.springframework.test.context.junit4.rules.SpringMethodRule
228+
* SpringMethodRule}). In any case, this method must be called prior to any
229+
* framework-specific lifecycle callbacks.
226230
* <p>The managed {@link TestContext} will be updated with the supplied
227231
* {@code testInstance}.
228232
* <p>An attempt will be made to give each registered

spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,10 +84,14 @@ default void beforeTestClass(TestContext testContext) throws Exception {
8484
}
8585

8686
/**
87-
* Prepares the {@link Object test instance} of the supplied
88-
* {@link TestContext test context}, for example by injecting dependencies.
87+
* Prepares the {@linkplain Object test instance} of the supplied
88+
* {@linkplain TestContext test context} &mdash; for example, to inject
89+
* dependencies.
8990
* <p>This method should be called immediately after instantiation of the test
90-
* instance but prior to any framework-specific lifecycle callbacks.
91+
* class or as soon after instantiation as possible (as is the case with the
92+
* {@link org.springframework.test.context.junit4.rules.SpringMethodRule
93+
* SpringMethodRule}). In any case, this method must be called prior to any
94+
* framework-specific lifecycle callbacks.
9195
* <p>The default implementation is <em>empty</em>. Can be overridden by
9296
* concrete classes as necessary.
9397
* @param testContext the test context for the test; never {@code null}
@@ -121,8 +125,8 @@ default void beforeTestMethod(TestContext testContext) throws Exception {
121125

122126
/**
123127
* Pre-processes a test <em>immediately before</em> execution of the
124-
* {@link java.lang.reflect.Method test method} in the supplied
125-
* {@link TestContext test context} &mdash; for example, for timing
128+
* {@linkplain java.lang.reflect.Method test method} in the supplied
129+
* {@linkplain TestContext test context} &mdash; for example, for timing
126130
* or logging purposes.
127131
* <p>This method <strong>must</strong> be called after framework-specific
128132
* <em>before</em> lifecycle callbacks.
@@ -141,8 +145,8 @@ default void beforeTestExecution(TestContext testContext) throws Exception {
141145

142146
/**
143147
* Post-processes a test <em>immediately after</em> execution of the
144-
* {@link java.lang.reflect.Method test method} in the supplied
145-
* {@link TestContext test context} &mdash; for example, for timing
148+
* {@linkplain java.lang.reflect.Method test method} in the supplied
149+
* {@linkplain TestContext test context} &mdash; for example, for timing
146150
* or logging purposes.
147151
* <p>This method <strong>must</strong> be called before framework-specific
148152
* <em>after</em> lifecycle callbacks.

spring-test/src/main/java/org/springframework/test/context/junit4/rules/SpringMethodRule.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,6 +79,10 @@
7979
* <p><strong>NOTE:</strong> As of Spring Framework 4.3, this class requires JUnit 4.12 or higher.
8080
*
8181
* <p><strong>WARNING:</strong> Due to the shortcomings of JUnit rules, the
82+
* {@code SpringMethodRule}
83+
* {@linkplain org.springframework.test.context.TestExecutionListener#prepareTestInstance
84+
* prepares the test instance} before {@code @Before} lifecycle methods instead of
85+
* immediately after instantiation of the test class. In addition, the
8286
* {@code SpringMethodRule} does <strong>not</strong> support the
8387
* {@code beforeTestExecution()} and {@code afterTestExecution()} callbacks of the
8488
* {@link org.springframework.test.context.TestExecutionListener TestExecutionListener}

src/docs/asciidoc/testing.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,8 +1640,10 @@ before failing.
16401640
times that the test method is to be run is specified in the annotation.
16411641

16421642
The scope of execution to be repeated includes execution of the test method itself as
1643-
well as any setting up or tearing down of the test fixture. The following example shows
1644-
how to use the `@Repeat` annotation:
1643+
well as any setting up or tearing down of the test fixture. When used with the
1644+
<<testcontext-junit4-rules, `SpringMethodRule`>>, the scope additionally includes
1645+
preparation of the test instance by `TestExecutionListener` implementations. The
1646+
following example shows how to use the `@Repeat` annotation:
16451647

16461648
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
16471649
.Java

0 commit comments

Comments
 (0)