子类可以修改 Java 中抽象超类中静态方法的行为吗?
我正在使用“参数化”运行器来执行一些测试。我正在从 XML 文件生成测试数据。这很好用。
现在我想将此行为应用于多个测试类,而不必复制测试数据生成代码。因此,我创建了一个抽象类,它负责从“一个”XML 文件填充测试数据。如果子类可以告诉使用哪个 XML 文件,我就实现了我的目标。
但我一直无法找到一种方法来做到这一点。
这是我的超类
@RunWith(Parameterized.class)
public abstract class AbstractXMLDrivenTest{
@@Parameters
public static Collection<Object[]> generateData () {
/* Reads an XML File and returns test input data */
}
}
这是一个子类
public class TestSomeThing extends AbstractXMLDrivenTest {
public TestSomeThing(args) {
/* Args are populated by generateData in superclass */
}
}
如果我在 AbstractXMLDrivenTest 中使用用于 TestSomething 的 XML 文件,则效果很好。 我希望 XML 文件由子类(即 TestSomething)定义,以便我也可以将超类与其他测试用例一起使用。我希望现在一切都清楚了。
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 superclass */
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java 中的静态方法没有继承。此外,无论静态方法的类是否是抽象的,对于静态方法来说都没有区别——类只具有静态方法的作用域函数。
为了使用继承,请使用非静态方法。这样您就可以从子类非静态方法或非静态字段中提取 XML 文件名。
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.
不可以,子类不能重写静态方法。
http://www.coderanch.com/how-to/java/OverridingVsHiding
No, subclasses can't override static methods.
http://www.coderanch.com/how-to/java/OverridingVsHiding
Java 中的静态方法不能被重写——无论该类是否是抽象类。
Static methods cannot be overriden in Java -- regardless whether the class is abstract or not.
如果您的 xml 文件名存储为超类的静态成员,请让您的子类在调用generateTestData 之前分配该值。
If your xml filename is stored as a static member of your superclass, have your subclass assign the value before generateTestData is called.
只需一个子类就足够了。无需增加测试子类,只需在不同的测试文件上调用测试方法即可。实际上,你最好甚至不要让你的测试类成为一个子类,而只是一个单独的类。
如果您想使用行业标准测试,请查看 JUnit。
编辑:
你没有仔细阅读文档。您根本不需要子类:
您可以通过
generateData
将各种测试用例构建为数组。在您的情况下,您将拥有一个
{("test1.xml", objectResult1), ("test2.xml", objectResult2),...}
形式的对数组。测试类的构造函数的形式为ParametrizedTest(字符串 fileXML,ObjectResult 结果)
。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 formParametrizedTest(String fileXML, ObjectResult result)
.