I'm using "Parameterized" runner to execute some tests. I am generating the test data from XML file. This works fine.
Now I want to apply this behavior to multiple test classes without having to replicate the test data generation code. So I made an abstract class which does the job of populating the test data from "an" XML file. If the sub-classes can tell which XML file to use, I'd achieve my goal.
But I've been unable to find a way to do this.
This is my super-class
@RunWith(Parameterized.class)
public abstract class AbstractXMLDrivenTest{
@@Parameters
public static Collection<Object[]> generateData () {
/* Reads an XML File and returns test input data */
}
}
Here's is a sub-class
public class TestSomeThing extends AbstractXMLDrivenTest {
public TestSomeThing(args) {
/* Args are populated by generateData in super开发者_JS百科class */
}
}
This works fine if I use XML file intended for TestSomething in AbstractXMLDrivenTest. I want the XML file to be defined by the sub-class i.e. TestSomething so that I can use the super-class with other test cases as well. I hope this is clear now.
There is no inheritance for static methods in Java. Also, it makes no difference to a static method whether its class is abstract or not -- a class only has a scoping function for static methods.
In order to use inheritance, please use non-static methods. This way you will be able to extract the XML file name from a subclass non-static method or non-static field.
No, subclasses can't override static methods.
http://www.coderanch.com/how-to/java/OverridingVsHiding
Static methods cannot be overriden in Java -- regardless whether the class is abstract or not.
If your xml filename is stored as a static member of your superclass, have your subclass assign the value before generateTestData is called.
Just one subclass is enough. Instead of multiplying the testing subclasses, just call your test method on your different test files. Actually, you'd better not even make your testing class a subclass, but just a separate class on its own.
If you want to use industry standard testing, look at JUnit.
EDIT:
You did not read the doc carefully. You do not need subclasses at all:
you build your various test cases as the array build by generateData
.
In your case you would have an array of pairs of the form {("test1.xml", objectResult1), ("test2.xml", objectResult2),...}
. The constructor of the test class is of the form
ParametrizedTest(String fileXML, ObjectResult result)
.
精彩评论