Introduction
This JUnit Parameterized Test project provides a JUnit
4.4 Test Runner which implements the
Parameterized Test pattern
.
Why not use the ParameterizedTestRunner from JUnit?
As already stated this project was developed for the
JUnitRecorder project. It was necessary to overcome
some shortcomings of the JUnit
ParameterizedTestRunner which is directly provided
by JUnit.
Let's take a look at a plain JUnit test i with a
ParameterizedTestRunner:
@RunWith(Parameterized.class)
public class XStreamControlCharTest
{
private XStream myXStream;
private String myOrginalString;
private String myExpectedXml;
@Before
public void before() {
myXStream = new XStream(new HideControlCharXppDriver());
}
public XStreamControlCharTest(String orginalString, String expectedXML)
{
myOrginalString = orginalString;
myExpectedXml = expectedXML;
}
@Parameters
public static Collection getMocks() {
return Arrays.asList(new Object[][] {
/* 0 */{"HelloWorld", "<string>HelloWorld</string>"}
/* 1 */, {"Hello World", "<string>Hello World</string>"}
[...]
/* 21 */, {"\\{0}", "<string>\\\\{0}</string>"}});
}
@Test
public void runTest() {
String xml = myXStream.toXML(myOrginalString);
// Serialization Test
assertEquals(myExpectedXml, xml);
String neu = (String) myXStream.fromXML(xml);
// deserialization Test
assertEquals(myOrginalString, neu);
}
}
- One 'Data provider'
-
Per test class only one data Array can be
defined. It is possible to annotate more methods
with "@Parameters" but still per class only one
(merged) test data list is available. It's not
possible to specify test data lists per test
method.
- Data inside test
-
Not necessarily a problem, but having the test
data inside the tests can get ugly when the data
is getting bigger.
- Dependency injection only by constructor
-
The 'dependencies' are injected into the
Constructor so in most cases you will need a
private member per parameter to transport the
injected values into your actual test.
- One test class per method not per class
-
When writing manual tests you have normally per
class to test one corresponding test-class. In
the test-class there are multiple test methods
testing ALL your method. With the JUnit
ParameteriezdTestRunner you have one class per
method!
To overcome this shortcomings the JUnit
Parameterized Test module was created:
- One 'Data provider'
-
Per test method a data provider can be
specified.
- Data inside test
-
The default 'Data provider' reads the test Data
from an XML resource file. But the providers are
exchangeable so you can just provide your own,
which can retrieve the data from virtually
anywhere.
- Dependency injection only by constructor
-
The tests data is injected directly into your
test method. a constructor injection is not
possible.
- One test class per method not per class
-
Because test data are now provided per test
method you can specify multiple test data per
class and therefore one test class is possible
again.
How this is really achieved you should continue
reading the
Usage
.