测试在Java中实现接口的所有类

时间:2022-09-25 12:15:30

Is there anything out there (for Java specifically) that allow you to automatically test the behavior of an interface? As an example, let's say I have a bunch of tests for the Comparable interface, that should apply to anything that implements Comparable. What I'd like is to be able to include "ComparableTests" automatically in the test fixtures for any of my classes which implement Comparable. Bonus points if this would work with generic interfaces.

是否有任何东西(特别是Java)允许您自动测试接口的行为?举个例子,假设我有一堆Comparable接口的测试,应该适用于实现Comparable的任何东西。我想要的是能够在我的任何实现Comparable的类的测试装置中自动包含“ComparableTests”。如果这适用于通用接口,则奖励积分。

I know the .NET framework mbUnit has something similar, and when you're using something like TestNG's generator functions you could set up a test fixture for Comparable and have the generator create an instance of each of your classes that implement Comparable. But I'd rather have it be automatic, and located at the test fixture for each of my classes (since I'll already have them around for testing other parts of that class).

我知道.NET框架mbUnit有类似的东西,当你使用像TestNG的生成器函数这样的东西时,你可以为Comparable设置一个测试夹具,并让生成器创建一个实现Comparable的每个类的实例。但我宁愿把它变成自动的,并且位于每个类的测试夹具上(因为我已经将它们用于测试该类的其他部分)。

Clarification: I could definitely build something like this. I was asking if there was anything out there that already enabled this.

澄清:我绝对可以建立这样的东西。我问是否有任何已经启用此功能的内容。

4 个解决方案

#1


1  

Based on your last paragraph, what you're trying to do is inject some 'extra methods' into unit testing since you're already testing a specific class. I do not know of a testing harness that allows you to attach tests based on the hierarchy of a class.

基于你的最后一段,你要做的是在单元测试中注入一些“额外的方法”,因为你已经在测试一个特定的类了。我不知道允许您根据类的层次结构附加测试的测试工具。

However, with your own suggestion of using TestNG for building something similar, I think you might be very close. You could very well incorporate some base code that adds your class to a list of 'default test classes', which are in turn tested if they implement a specific interface.

但是,根据你自己的建议使用TestNG来构建类似的东西,我想你可能会非常接近。您可以很好地合并一些基本代码,将您的类添加到“默认测试类”列表中,如果它们实现特定接口,则会对其进行测试。

Still, regarding the general case, I think you're out of luck, since the Java type system is one-way, you can only (easily) find out what interfaces a class implements, not the other way around. Furthermore, the problem is 'where to stop looking': if you have a test that checks all your comparable implementers, do you want it to check the validity of String's one too, since that is in your Java environment?

尽管如此,关于一般情况,我认为你运气不好,因为Java类型系统是单向的,你只能(轻松地)找出类实现的接口,而不是相反。此外,问题是“在哪里停止查看”:如果您有一个检查所有类似实施者的测试,您是否也希望它检查String的有效性,因为那是在您的Java环境中?

#2


1  

Try this http://www.xmlizer.biz/java/classloader/ClassList.java

试试这个http://www.xmlizer.biz/java/classloader/ClassList.java

#3


0  

In .NET it would be pretty simple to set up a method that looks through an assembly and identifies each class's inheritance/implementation hierarchy. I'm sure you could do it in Java, too, if you research the Java reflection API.

在.NET中,设置一个查看程序集并识别每个类的继承/实现层次结构的方法非常简单。如果你研究Java反射API,我相信你也可以用Java做。

You could then create an array of ITargetInterfaces and call a test method on each one.

然后,您可以创建一个ITargetInterfaces数组,并在每个数组上调用一个测试方法。

#4


0  

One way would be to search through the jar file for all the .class files (or search through the classes directory), use the Class.forName() method to load the class file and check MyInterface.class.isAssignableFrom(myClass).

一种方法是在jar文件中搜索所有.class文件(或通过classes目录搜索),使用Class.forName()方法加载类文件并检查MyInterface.class.isAssignableFrom(myClass)。

This wouldn't deal easily public inner static classes (you could parse the class file name), but would never work with private inner classes or anonymous inner classes.

这不会轻易处理公共内部静态类(您可以解析类文件名),但永远不会使用私有内部类或匿名内部类。

#1


1  

Based on your last paragraph, what you're trying to do is inject some 'extra methods' into unit testing since you're already testing a specific class. I do not know of a testing harness that allows you to attach tests based on the hierarchy of a class.

基于你的最后一段,你要做的是在单元测试中注入一些“额外的方法”,因为你已经在测试一个特定的类了。我不知道允许您根据类的层次结构附加测试的测试工具。

However, with your own suggestion of using TestNG for building something similar, I think you might be very close. You could very well incorporate some base code that adds your class to a list of 'default test classes', which are in turn tested if they implement a specific interface.

但是,根据你自己的建议使用TestNG来构建类似的东西,我想你可能会非常接近。您可以很好地合并一些基本代码,将您的类添加到“默认测试类”列表中,如果它们实现特定接口,则会对其进行测试。

Still, regarding the general case, I think you're out of luck, since the Java type system is one-way, you can only (easily) find out what interfaces a class implements, not the other way around. Furthermore, the problem is 'where to stop looking': if you have a test that checks all your comparable implementers, do you want it to check the validity of String's one too, since that is in your Java environment?

尽管如此,关于一般情况,我认为你运气不好,因为Java类型系统是单向的,你只能(轻松地)找出类实现的接口,而不是相反。此外,问题是“在哪里停止查看”:如果您有一个检查所有类似实施者的测试,您是否也希望它检查String的有效性,因为那是在您的Java环境中?

#2


1  

Try this http://www.xmlizer.biz/java/classloader/ClassList.java

试试这个http://www.xmlizer.biz/java/classloader/ClassList.java

#3


0  

In .NET it would be pretty simple to set up a method that looks through an assembly and identifies each class's inheritance/implementation hierarchy. I'm sure you could do it in Java, too, if you research the Java reflection API.

在.NET中,设置一个查看程序集并识别每个类的继承/实现层次结构的方法非常简单。如果你研究Java反射API,我相信你也可以用Java做。

You could then create an array of ITargetInterfaces and call a test method on each one.

然后,您可以创建一个ITargetInterfaces数组,并在每个数组上调用一个测试方法。

#4


0  

One way would be to search through the jar file for all the .class files (or search through the classes directory), use the Class.forName() method to load the class file and check MyInterface.class.isAssignableFrom(myClass).

一种方法是在jar文件中搜索所有.class文件(或通过classes目录搜索),使用Class.forName()方法加载类文件并检查MyInterface.class.isAssignableFrom(myClass)。

This wouldn't deal easily public inner static classes (you could parse the class file name), but would never work with private inner classes or anonymous inner classes.

这不会轻易处理公共内部静态类(您可以解析类文件名),但永远不会使用私有内部类或匿名内部类。