Skip to content

Commit bc17d6f

Browse files
committed
Implementing junit-team#1036 - add BlockJUnit4ClassRunner#createTest(FrameworkMethod) to allow implementations to provide a custom instance of the test for each FrameworkMethod invocation
1 parent 60aaf96 commit bc17d6f

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package org.junit.runners;
22

3-
import static org.junit.internal.runners.rules.RuleMemberValidator.RULE_METHOD_VALIDATOR;
4-
import static org.junit.internal.runners.rules.RuleMemberValidator.RULE_VALIDATOR;
5-
6-
import java.util.List;
7-
import java.util.concurrent.ConcurrentHashMap;
8-
import java.util.concurrent.TimeUnit;
9-
103
import org.junit.After;
114
import org.junit.Before;
125
import org.junit.Ignore;
@@ -30,6 +23,13 @@
3023
import org.junit.runners.model.MultipleFailureException;
3124
import org.junit.runners.model.Statement;
3225

26+
import java.util.List;
27+
import java.util.concurrent.ConcurrentHashMap;
28+
import java.util.concurrent.TimeUnit;
29+
30+
import static org.junit.internal.runners.rules.RuleMemberValidator.RULE_METHOD_VALIDATOR;
31+
import static org.junit.internal.runners.rules.RuleMemberValidator.RULE_VALIDATOR;
32+
3333
/**
3434
* Implements the JUnit 4 standard test case class model, as defined by the
3535
* annotations in the org.junit package. Many users will never notice this
@@ -217,6 +217,14 @@ protected Object createTest() throws Exception {
217217
return getTestClass().getOnlyConstructor().newInstance();
218218
}
219219

220+
/**
221+
* Returns a new fixture to run a particular test {@code method} against.
222+
* Default implementation executes the no-argument {@link #createTest()} method.
223+
*/
224+
protected Object createTest(FrameworkMethod method) throws Exception {
225+
return createTest();
226+
}
227+
220228
/**
221229
* Returns the name that describes {@code method} for {@link Description}s.
222230
* Default implementation is the method's name
@@ -232,7 +240,7 @@ protected String testName(FrameworkMethod method) {
232240
* Here is an outline of the default implementation:
233241
*
234242
* <ul>
235-
* <li>Invoke {@code method} on the result of {@code createTest()}, and
243+
* <li>Invoke {@code method} on the result of {@link #createTest(org.junit.runners.model.FrameworkMethod)}, and
236244
* throw any exceptions thrown by either operation.
237245
* <li>HOWEVER, if {@code method}'s {@code @Test} annotation has the {@code
238246
* expecting} attribute, return normally only if the previous step threw an
@@ -257,13 +265,13 @@ protected String testName(FrameworkMethod method) {
257265
* This can be overridden in subclasses, either by overriding this method,
258266
* or the implementations creating each sub-statement.
259267
*/
260-
protected Statement methodBlock(FrameworkMethod method) {
268+
protected Statement methodBlock(final FrameworkMethod method) {
261269
Object test;
262270
try {
263271
test = new ReflectiveCallable() {
264272
@Override
265273
protected Object runReflectiveCall() throws Throwable {
266-
return createTest();
274+
return createTest(method);
267275
}
268276
}.run();
269277
} catch (Throwable e) {

0 commit comments

Comments
 (0)