-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@BeforeParam/@AfterParam for Parameterized runner #1435
Changes from 1 commit
066e1d6
45064f7
3390335
1de2311
15ac6ad
70245b3
e270f2c
570ec30
754732b
35bbd8f
301a6e0
98e061a
f406970
f7e3ad3
76c1779
03e598f
4958ea8
ab15e7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,15 +2,18 @@ | |
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.BlockJUnit4ClassRunner; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameter; | ||
import org.junit.runners.model.FrameworkField; | ||
import org.junit.runners.model.FrameworkMethod; | ||
import org.junit.runners.model.InitializationError; | ||
import org.junit.runners.model.MultipleFailureException; | ||
import org.junit.runners.model.Statement; | ||
|
||
/** | ||
|
@@ -134,8 +137,72 @@ protected void validateFields(List<Throwable> errors) { | |
} | ||
|
||
@Override | ||
protected Statement classBlock(RunNotifier notifier) { | ||
return childrenInvoker(notifier); | ||
protected Statement classBlock(final RunNotifier notifier) { | ||
Statement statement = childrenInvoker(notifier); | ||
statement = withBeforeParams(statement); | ||
statement = withAfterParams(statement); | ||
return statement; | ||
} | ||
|
||
private Statement withBeforeParams(Statement statement) { | ||
final List<FrameworkMethod> befores = getTestClass() | ||
.getAnnotatedMethods(Parameterized.BeforeParam.class); | ||
return befores.isEmpty() ? statement : new RunBeforeParams(statement, befores); | ||
} | ||
|
||
private class RunBeforeParams extends Statement { | ||
private final Statement next; | ||
private final List<FrameworkMethod> befores; | ||
|
||
RunBeforeParams(Statement next, List<FrameworkMethod> befores) { | ||
this.next = next; | ||
this.befores = befores; | ||
} | ||
|
||
@Override | ||
public void evaluate() throws Throwable { | ||
for (FrameworkMethod before : befores) { | ||
final int paramCount = before.getMethod().getParameterTypes().length; | ||
before.invokeExplosively(null, paramCount == 0 ? (Object[]) null : parameters); | ||
} | ||
next.evaluate(); | ||
} | ||
} | ||
|
||
private Statement withAfterParams(Statement statement) { | ||
final List<FrameworkMethod> afters = getTestClass() | ||
.getAnnotatedMethods(Parameterized.AfterParam.class); | ||
return afters.isEmpty() ? statement : new RunAfterParams(statement, afters); | ||
} | ||
|
||
private class RunAfterParams extends Statement { | ||
private final Statement next; | ||
private final List<FrameworkMethod> afters; | ||
|
||
RunAfterParams(Statement next, List<FrameworkMethod> afters) { | ||
this.next = next; | ||
this.afters = afters; | ||
} | ||
|
||
@Override | ||
public void evaluate() throws Throwable { | ||
final List<Throwable> errors = new ArrayList<Throwable>(); | ||
try { | ||
next.evaluate(); | ||
} catch (Throwable e) { | ||
errors.add(e); | ||
} finally { | ||
for (FrameworkMethod each : afters) { | ||
try { | ||
final int paramCount = each.getMethod().getParameterTypes().length; | ||
each.invokeExplosively(null, paramCount == 0 ? (Object[]) null : parameters); | ||
} catch (Throwable e) { | ||
errors.add(e); | ||
} | ||
} | ||
} | ||
MultipleFailureException.assertEmpty(errors); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we reuse |
||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
import static java.util.Arrays.asList; | ||
import static org.hamcrest.CoreMatchers.allOf; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import static org.junit.experimental.results.PrintableResult.testResult; | ||
|
@@ -16,6 +16,7 @@ | |
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.JUnitCore; | ||
|
@@ -24,6 +25,7 @@ | |
import org.junit.runner.RunWith; | ||
import org.junit.runner.Runner; | ||
import org.junit.runner.notification.Failure; | ||
import org.junit.runners.MethodSorters; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameter; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
@@ -259,6 +261,92 @@ public void beforeAndAfterClassAreRun() { | |
assertEquals("before after ", fLog); | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public static class BeforeParamAndAfterParam { | ||
@BeforeClass | ||
public static void before() { | ||
fLog += "beforeClass "; | ||
} | ||
|
||
@Parameterized.BeforeParam | ||
public static void beforeParam(String x) { | ||
fLog += "before(" + x + ") "; | ||
} | ||
|
||
@Parameterized.AfterParam | ||
public static void afterParam() { | ||
fLog += "afterParam "; | ||
} | ||
|
||
@AfterClass | ||
public static void after() { | ||
fLog += "afterClass "; | ||
} | ||
|
||
private final String x; | ||
|
||
public BeforeParamAndAfterParam(String x) { | ||
this.x = x; | ||
} | ||
|
||
@Parameters | ||
public static Collection<String> data() { | ||
return Arrays.asList("A", "B"); | ||
} | ||
|
||
@Test | ||
public void first() { | ||
fLog += "first(" + x + ") "; | ||
} | ||
|
||
@Test | ||
public void second() { | ||
fLog += "second(" + x + ") "; | ||
} | ||
} | ||
|
||
@Test | ||
public void beforeParamAndAfterParamAreRun() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great to have tests for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kcooney I've added some more tests and validation for number of parameters of the BeforeParam/AfterParam methods. |
||
fLog = ""; | ||
final Result result = JUnitCore.runClasses(BeforeParamAndAfterParam.class); | ||
assertEquals(0, result.getFailureCount()); | ||
assertEquals("beforeClass before(A) first(A) second(A) afterParam " | ||
+ "before(B) first(B) second(B) afterParam afterClass ", fLog); | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
public static class BeforeParamAndAfterParamError { | ||
@Parameterized.BeforeParam | ||
public void beforeParam(String x) { | ||
} | ||
|
||
@Parameterized.AfterParam | ||
private static void afterParam() { | ||
} | ||
|
||
public BeforeParamAndAfterParamError(String x) { | ||
} | ||
|
||
@Parameters | ||
public static Collection<String> data() { | ||
return Arrays.asList("A", "B"); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
} | ||
} | ||
|
||
@Test | ||
public void beforeParamAndAfterParamValidation() { | ||
fLog = ""; | ||
final Result result = JUnitCore.runClasses(BeforeParamAndAfterParamError.class); | ||
assertEquals(1, result.getFailureCount()); | ||
assertThat(result.getFailures().get(0).getMessage(), containsString("beforeParam() should be static")); | ||
assertThat(result.getFailures().get(0).getMessage(), containsString("afterParam() should be public")); | ||
} | ||
|
||
@RunWith(Parameterized.class) | ||
static public class EmptyTest { | ||
@BeforeClass | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reuse
org.junit.internal.runners.statements.RunBefores
and passparameters
to it?