Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added AfterParameterize and BeforeParameterize to allow methods to ru…

…n before and after parameterized tests

Signed-off-by: dsaff <david@saff.net>
  • Loading branch information...
commit 6abd4e65122ac6e3b56cf53a9a7540b61e4e687e 1 parent 5ace933
Mike Forsberg authored dsaff committed
View
47 src/main/java/org/junit/runners/Parameterized.java
@@ -9,6 +9,8 @@
import java.util.Collections;
import java.util.List;
+import org.junit.internal.runners.statements.RunAfters;
+import org.junit.internal.runners.statements.RunBefores;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
@@ -72,6 +74,24 @@
public static @interface Parameters {
}
+ /**
+ * Annotation for a method which executes before a instance of <code>Parameterized</code> tests
+ * are ran.
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.METHOD)
+ public static @interface AfterParameterize {
+ }
+
+ /**
+ * Annotation for a method which executes before a instance of <code>Parameterized</code> tests
+ * are ran.
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.METHOD)
+ public static @interface BeforeParameterize {
+ }
+
private class TestClassRunnerForParameters extends
BlockJUnit4ClassRunner {
private final int fParameterSetNumber;
@@ -120,7 +140,32 @@ protected void validateZeroArgConstructor(List<Throwable> errors) {
@Override
protected Statement classBlock(RunNotifier notifier) {
- return childrenInvoker(notifier);
+ Statement statement= childrenInvoker(notifier);
+ statement= withBeforeParameterize(statement);
+ statement= withAfterParameterize(statement);
+ return statement;
+ }
+
+ /**
+ * Returns a {@link Statement}: run all non-overridden {@code @AfterParameterize} methods on this class
+ * and superclasses before executing {@code statement}; if any throws an
+ * Exception, stop execution and pass the exception on.
+ */
+ protected Statement withAfterParameterize(Statement statement) {
+ List<FrameworkMethod> afterParameterizes = getTestClass().getAnnotatedMethods(AfterParameterize.class);
+ statement= new RunAfters(statement, afterParameterizes, null);
+ return statement;
+ }
+
+ /**
+ * Returns a {@link Statement}: run all non-overridden {@code @BeforeParameterize} methods on this class
+ * and superclasses before executing {@code statement}; if any throws an
+ * Exception, stop execution and pass the exception on.
+ */
+ protected Statement withBeforeParameterize(Statement statement) {
+ List<FrameworkMethod> beforeParameterizes = getTestClass().getAnnotatedMethods(BeforeParameterize.class);
+ statement= new RunBefores(statement, beforeParameterizes, null);
+ return statement;
}
}
View
90 src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java
@@ -19,6 +19,8 @@
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.AfterParameterize;
+import org.junit.runners.Parameterized.BeforeParameterize;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.model.InitializationError;
@@ -111,6 +113,94 @@ public void beforeAndAfterClassAreRun() {
fLog= "";
JUnitCore.runClasses(BeforeAndAfter.class);
assertEquals("before after ", fLog);
+ }
+
+ @RunWith(Parameterized.class)
+ static public class BeforeAndAfterParameterize {
+ @BeforeParameterize
+ public static void beforeParameterize() {
+ fLog+= "beforeParameterize ";
+ }
+
+ @AfterParameterize
+ public static void afterParameterize() {
+ fLog+= "afterParameterize ";
+ }
+
+ public BeforeAndAfterParameterize(int x) {
+
+ }
+
+ @Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(new Object[][] { { 3 }, { 4 } });
+ }
+
+ @Test
+ public void aTest() {
+ fLog+= "aTest ";
+ }
+
+ @Test
+ public void bTest() {
+ fLog+= "bTest ";
+ }
+ }
+
+ @Test
+ public void beforeAndAfterParameterizeAreRun() {
+ fLog= "";
+ JUnitCore.runClasses(BeforeAndAfterParameterize.class);
+ assertEquals("beforeParameterize aTest bTest afterParameterize beforeParameterize aTest bTest afterParameterize ", fLog);
+ }
+
+ @RunWith(Parameterized.class)
+ static public class BeforeAfterParameterizeAndBeforeAfterClass {
+ @BeforeParameterize
+ public static void beforeParameterize() {
+ fLog+= "beforeParameterize ";
+ }
+
+ @AfterParameterize
+ public static void afterParameterize() {
+ fLog+= "afterParameterize ";
+ }
+
+ @BeforeClass
+ public static void beforeClass() {
+ fLog+= "beforeClass ";
+ }
+
+ @AfterClass
+ public static void afterClass() {
+ fLog+= "afterClass ";
+ }
+
+ public BeforeAfterParameterizeAndBeforeAfterClass(int x) {
+
+ }
+
+ @Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(new Object[][] { { 3 }, { 4 } });
+ }
+
+ @Test
+ public void aTest() {
+ fLog+= "aTest ";
+ }
+
+ @Test
+ public void bTest() {
+ fLog+= "bTest ";
+ }
+ }
+
+ @Test
+ public void beforeAfterParameterizeAndBeforeAfterClassAreRun() {
+ fLog= "";
+ JUnitCore.runClasses(BeforeAfterParameterizeAndBeforeAfterClass.class);
+ assertEquals("beforeClass beforeParameterize aTest bTest afterParameterize beforeParameterize aTest bTest afterParameterize afterClass ", fLog);
}
@RunWith(Parameterized.class)
Please sign in to comment.
Something went wrong with that request. Please try again.