Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Tom Avatar asked Nov 06 '22 20:11

Tom


1 Answers

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.

like image 133
Yishai Avatar answered Nov 12 '22 17:11

Yishai