Junit inline comparator initialization error
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.
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.
java.lang.Exception: Test class should have public zero-argument constructor at
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...
上一篇: 在select上获取行号
下一篇: Junit内联比较器初始化错误