com.xebialabs.deployit.test.support.junit
Class Parameterized

java.lang.Object
  extended by org.junit.runner.Runner
      extended by org.junit.runners.ParentRunner<org.junit.runner.Runner>
          extended by org.junit.runners.Suite
              extended by com.xebialabs.deployit.test.support.junit.Parameterized
All Implemented Interfaces:
org.junit.runner.Describable, org.junit.runner.manipulation.Filterable, org.junit.runner.manipulation.Sortable

public class Parameterized
extends org.junit.runners.Suite

The custom runner Parameterized implements parameterized tests. When running a parameterized test class, instances are created for the cross-product of the test methods and the test data elements.

For example, to test a Fibonacci function, write:

 @RunWith(Parameterized.class)
 public class FibonacciTest {
        @Parameters(name= "fib({0})={1}")
        public static Iterable<Object[]> data() {
                return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
     }

        private int fInput;

        private int fExpected;

        public FibonacciTest(int input, int expected) {
                fInput= input;
                fExpected= expected;
     }

        @Test
        public void test() {
                assertEquals(fExpected, Fibonacci.compute(fInput));
     }
 }
 

Each instance of FibonacciTest will be constructed using the two-argument constructor and the data values in the @Parameters method.

In order that you can easily identify the individual tests, you may provide a name for the @Parameters annotation. This name is allowed to contain placeholders, which are replaced at runtime. The placeholders are

{index}
the current parameter index
{0}
the first parameter value
{1}
the second parameter value
...
In the example given above, the Parameterized runner creates names like fib(3)=2. If you don't use the name parameter, then the current parameter index is used as name.

You can also write:

 @RunWith(Parameterized.class)
 public class FibonacciTest {
        @Parameters
        public static Iterable<Object[]> data() {
                return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
     }
        @Parameter(0)
        public int fInput;

        @Parameter(1)
        public int fExpected;

        @Test
        public void test() {
                assertEquals(fExpected, Fibonacci.compute(fInput));
     }
 }
 

Each instance of FibonacciTest will be constructed without constructor and fields annoted by @Parameter will be initialized with the data values in the @Parameters method.


Nested Class Summary
static interface Parameterized.Parameter
          Annotation for fields of the test class which will be initialized by the method annoted by Parameters
By using directly this annotation, the test class constructor isn't needed.
Index range must start at 0.
static interface Parameterized.Parameters
          Annotation for a method which provides parameters to be injected into the test class constructor by Parameterized
 
Nested classes/interfaces inherited from class org.junit.runners.Suite
org.junit.runners.Suite.SuiteClasses
 
Constructor Summary
Parameterized(java.lang.Class<?> klass)
          Only called reflectively.
 
Method Summary
protected  java.util.List<org.junit.runner.Runner> getChildren()
           
 
Methods inherited from class org.junit.runners.Suite
describeChild, emptySuite, runChild
 
Methods inherited from class org.junit.runners.ParentRunner
childrenInvoker, classBlock, classRules, collectInitializationErrors, filter, getDescription, getName, getRunnerAnnotations, getTestClass, run, runLeaf, setScheduler, sort, validatePublicVoidNoArgMethods, withAfterClasses, withBeforeClasses
 
Methods inherited from class org.junit.runner.Runner
testCount
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Parameterized

public Parameterized(java.lang.Class<?> klass)
              throws java.lang.Throwable
Only called reflectively. Do not use programmatically.

Throws:
java.lang.Throwable
Method Detail

getChildren

protected java.util.List<org.junit.runner.Runner> getChildren()
Overrides:
getChildren in class org.junit.runners.Suite