From the Soda Journal, written on 30 June 06 by tim.
A hidden gem of parameterized tests discovered in Junit 4 javadocs.
While developing the tests for the new sodaplay.com, which needed to test that certain pages generated correct links no matter the name of the context path… That’s a pain because you want to repeat the same tests with just a single parameter difference (the context path). And I’m going to have to do that for each page that I want to test…
Ideally I’d define that at some higher abstract PageTestCase which I’d just then extend for each concrete Page I want to test. And I’m just too lazy to write “suite” methods. I mean that’s what recent programming frameworks are for - saving you from writing boiler plate code, right? Having just moved to eclipse 3.2 with its junit 4 support, I thought I’d poke my nose around in junit 4 to see if it might be easier in that new world. I started writing my own TestRunner to run a certain class of tests, but then realized this was going to be really hard work, especially as I would have to write that test first (which Kent Beck (accurately I think) described as “like doing brain surgery on yourself” (and then proceded to do that)). But then I discovered the Parameterized test runner - auto brain surgery avoided!
Basically you define a set of parameters (an array of (isomorphic? isogenous?) arrays of Objects - each instance of Object[] then represents the parameters to send to your constructor). You do this by marking the method that returns this Collection as @org.junit.runners.Parameterized.Parameters.
Then your test class should have a constructor taking the same set (of classes) of parameters as defined in each instance of Object[].
You then mark you test class with the handy @org.junit.runner.RunWith(org.junit.runners.Parameterized) and instances of your test class are created “for the cross-product of the test methods and the test data elements” as the java docs so succinctly put it.
comment formsee more from the Journal
Comments and Trackbacks
i like it
Commenting is not available in this section entry.