Permalink
Browse files

Cleaned up and added to documentation of Paramterized to clarify vagu…

…e usecases. Added constructor to facilitate subclassing. Added test to clarify parameter inheritance.
  • Loading branch information...
1 parent c42c8a4 commit 5685520c83b347d1e6c74fc7d5a4a7165f307346 pbaker committed Jun 16, 2009
@@ -55,7 +55,19 @@
* <p>
* Each instance of <code>FibonacciTest</code> will be constructed using the
* two-argument constructor and the data values in the
- * <code>&#064;Parameters</code> method.
+ * <code>&#064;Parameters</code> method. If multiple methods in a class or
+ * any of its superclasses are annotated:
+ * <ul>
+ * <li>Inherited methods do not need to be repeated, but may be overridden.</li>
+ * <li>The first one in a class file is given preference.</li>
+ * <li>Overridden inherited methods are given preference over declared
+ * methods.</li>
+ * <li>Declared methods are given preference over inherited methods
+ * that have not been overridden.</li>
+ * </ul>
+ *
+ * The <code>&#064;Parameters</code> method is
+ * called before any <code>&#064;BeforeClass</code> methods.
* </p>
*/
public class Parameterized extends Suite {
@@ -123,16 +135,29 @@ protected Statement classBlock(RunNotifier notifier) {
private final ArrayList<Runner> runners= new ArrayList<Runner>();
/**
- * Only called reflectively. Do not use programmatically.
+ * Only called reflectively. Do not use programmatically. Initializes
+ * one runner for each test for each set of parameters.
*/
public Parameterized(Class<?> klass) throws Throwable {
- super(klass, Collections.<Runner>emptyList());
+ this(klass, Collections.<Runner>emptyList());
List<Object[]> parametersList= getParametersList(getTestClass());
for (int i= 0; i < parametersList.size(); i++)
runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(),
parametersList, i));
}
+ /**
+ * Called by this class and subclasses once the runners making
+ * up the suite have been determined. Avoids runner initialization.
+ *
+ * @param klass root of the suite
+ * @param runners for each class in the suite, a {@link Runner}
+ * @throws Throwable
+ */
+ protected Parameterized(Class<?> klass, List<Runner> runners) throws Throwable {
+ super(klass, runners);
+ }
+
@Override
protected List<Runner> getChildren() {
return runners;
@@ -78,6 +78,42 @@ public void plansNamedCorrectly() throws Exception {
assertEquals("[0]", description.getChildren().get(0).getDisplayName());
}
+ @RunWith(Parameterized.class)
+ static public class ExtendedParameters extends FibonacciTest {
+ public ExtendedParameters(int input, int expected) {
+ super(input, expected);
+ }
+ }
+
+ @Test
+ public void runsExtendedParametersClass() {
+ Result result= JUnitCore.runClasses(ExtendedParameters.class);
+ assertEquals(7, result.getRunCount());
+ assertEquals(6, result.getFailureCount());
+ }
+
+ @RunWith(Parameterized.class)
+ static public class OverriddenParameters extends FibonacciTest {
+ public OverriddenParameters(int input, int expected) {
+ super(input, expected);
+ }
+ @Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 } });
+ }
+ @Parameters
+ public static Collection<Object[]> moreData() {
+ return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 } });
+ }
+ }
+
+ @Test
+ public void runsOverriddenParametersClass() {
+ Result result= JUnitCore.runClasses(OverriddenParameters.class);
+ assertEquals(2, result.getRunCount());
+ assertEquals(1, result.getFailureCount());
+ }
+
private static String fLog;
@RunWith(Parameterized.class)
@@ -133,6 +169,30 @@ public void validateClassCatchesNoParameters() {
}
@RunWith(Parameterized.class)
+ static public class MultipleParametersMethods {
+ @Test
+ public int test() {
+ return 0;
+ }
+
+ @Parameters
+ public static Collection<Object[]> data() {
+ return Arrays.asList(new Object[][] { { 0 } });
+ }
+
+ @Parameters
+ public static Collection<Object[]> moreData() {
+ return Arrays.asList(new Object[][] { { 0 }, { 0 } });
+ }
+ }
+
+ @Test
+ public void firstParametersMethodUsed() {
+ Result result= JUnitCore.runClasses(MultipleParametersMethods.class);
+ assertEquals(1, result.getRunCount());
+ }
+
+ @RunWith(Parameterized.class)
static public class IncorrectTest {
@Test
public int test() {

1 comment on commit 5685520

dsaff commented on 5685520 Aug 21, 2009

I'm concerned about this. As far as I know, there's no guarantee that source-file order of methods will match class-file order of methods. Right?

Please sign in to comment.