Junit内联比较器初始化错误
我创建了一个 SortedList 类,它有一个采用 java.util.Comparator 作为参数的构造函数。
在我的机器上运行单元测试(通过 eclipse 3.3.0)后,一切正常。 然而,哈德森抱怨,因为它说它无法实例化我的比较器。
这是我的简单测试(片段)
public class SortedListTest {
class strcmp implements Comparator<String>{
public strcmp(){}
public int compare(String s1,String s2){
return s1.compareTo(s2);
}
}
@Test
public void testAdd(){
SortedList<String> list = new SortedList<String>(new strcmp());
}
}
或者另一种方法:
public void testAdd(){
SortedList<String> list = new SortedList<String>(new Comparator<String>(){
public int compare(String s1,String s2){
return s1.compareTo(s2);
}
});
}
Hudson 显示的错误是
ar.com.lib.SortedListTest$strcmp.initializationError0
错误消息
测试类应具有公共零参数构造函数
堆栈跟踪
java.lang.Exception:测试类应该 有公共零参数构造函数 在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:513) 造成原因: java.lang.NoSuchMethodException: ar.com.lib.SortedListTest$strcmp.() 在 java.lang.Class.getConstructor0(Class.java:2706) 在 java.lang.Class.getConstructor(Class.java:1657)
我尝试过使用 @Ignore 注释,但到目前为止没有运气。 当我尝试时它不会编译
@Ignore class strcmp{}
任何想法将不胜感激。 提前致谢。
I've created a SortedList Class, which has a Constructor that takes a java.util.Comparator as an argument.
After running Unit tests on my machine (through eclipse 3.3.0), everything was OK. However, Hudson complaints because it says it can't instantiate my comparator.
Here its my simple test (snippets)
public class SortedListTest {
class strcmp implements Comparator<String>{
public strcmp(){}
public int compare(String s1,String s2){
return s1.compareTo(s2);
}
}
@Test
public void testAdd(){
SortedList<String> list = new SortedList<String>(new strcmp());
}
}
Or an alternative way:
public void testAdd(){
SortedList<String> list = new SortedList<String>(new Comparator<String>(){
public int compare(String s1,String s2){
return s1.compareTo(s2);
}
});
}
The error Hudson shows is
ar.com.lib.SortedListTest$strcmp.initializationError0
Error Message
Test class should have public zero-argument constructor
Stacktrace
java.lang.Exception: Test class should
have public zero-argument constructor
at
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
Caused by:
java.lang.NoSuchMethodException:
ar.com.lib.SortedListTest$strcmp.()
at
java.lang.Class.getConstructor0(Class.java:2706)
at
java.lang.Class.getConstructor(Class.java:1657)
I-ve tried using the @Ignore annotation, but no luck so far. It wont compile when i try
@Ignore class strcmp{}
Any ideas will be appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 Hudson 将 strcmp 视为它尝试运行的测试类。 这是一个内部类这一事实并没有改变这一点。 需要更改 Hudson 配置以排除此类作为测试类。
如果你做不到这一点,那么使 strcmp 成为一个带有公共 noargs 构造函数的公共静态类,并向其中添加一个不执行任何操作的测试方法(或者类上的忽略标记可能起作用)。 但如果您能让测试运行者根本不选择该类作为测试类,那就更好了。
The problem is that Hudson is seeing strcmp as a test class that it tries to run. The fact that this is an inner class doesn't change that. The Hudson configuration needs to be changed to exclude this class as a test class.
If you can't do that, then make strcmp a public static class with a public noargs constructor and add a test method to it that does nothing (or the ignore tag on the class might work). But it would be much better if you could get the test runner to not pick that class up as a test class at all.
,您的带有
@Test
的类是否有公共零参数构造函数? 从您发布的内容无法判断这是否属实。您可能还想正确地为内部类提供可访问性修饰符(
public
、protected
等)并正确命名(类应以大写字母开头) 。 如果这些类是通过反射调用的,那么 JUnit 运行程序总是可能对格式和命名规则做出某些假设......Well, does your class with the
@Test
have a public zero-arg constructor? Can't tell if this is true from what you posted.You might also want to properly give the inner class an accessibility modifer (
public
,protected
, etc.) along with name it properly (class's should start with an upper case letter). If these classes are invoked by reflection, it's always possible that the JUnit runner makes certain assumptions about formatting and naming rules...