Skip to content

Commit

Permalink
Cleaned up and added to documentation of Paramterized to clarify vagu…
Browse files Browse the repository at this point in the history
…e usecases. Added constructor to facilitate subclassing. Added test to clarify parameter inheritance.
  • Loading branch information
pbaker committed Jun 16, 2009
1 parent c42c8a4 commit 5685520
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/main/java/org/junit/runners/Parameterized.java
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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)
Expand Down Expand Up @@ -132,6 +168,30 @@ public void validateClassCatchesNoParameters() {
assertEquals(1, result.getFailureCount());
}

@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
Expand Down

1 comment on commit 5685520

@dsaff
Copy link

@dsaff dsaff commented on 5685520 Aug 21, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.