java Junit 内联比较器初始化错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1153708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Junit inline comparator initialization error
提问by Tom
I've created a SortedList Class, which has a Constructor that takes a java.util.Comparator as an argument.
我创建了一个 SortedList 类,它有一个以 java.util.Comparator 作为参数的构造函数。
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.
在我的机器上运行单元测试后(通过 eclipse 3.3.0),一切正常。但是,Hudson 抱怨说它不能实例化我的比较器。
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
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)
堆栈跟踪
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.() 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 注释,但到目前为止还没有运气。当我尝试时它不会编译
@Ignore class strcmp{}
Any ideas will be appreciated. Thanks in advance.
任何想法将不胜感激。提前致谢。
回答by Yishai
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.
问题是 Hudson 将 strcmp 视为它试图运行的测试类。这是一个内部类的事实并没有改变这一点。需要更改 Hudson 配置以将此类排除为测试类。
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.
如果您不能这样做,则将 strcmp 设为具有公共 noargs 构造函数的公共静态类,并向其添加一个不执行任何操作的测试方法(或者该类上的 ignore 标记可能会起作用)。但是,如果您能让测试运行程序根本不选择该类作为测试类,那就更好了。
回答by matt b
java.lang.Exception: Test class should have public zero-argument constructor at
java.lang.Exception: 测试类应该有公共零参数构造函数
Well, does your class with the @Testhave a public zero-arg constructor? Can't tell if this is true from what you posted.
好吧,您的类@Test是否具有公共零参数构造函数?无法从您发布的内容中判断这是否属实。
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...
您可能还想正确地为内部类提供可访问性修饰符(public、protected等)并正确命名(类应以大写字母开头)。如果这些类是通过反射调用的,那么 JUnit 运行器总是有可能对格式和命名规则做出某些假设......

