关于junit测试,构造器常见的错误Test class can only have one constructor和Test class should have exactly one......

junit测试常见的错误

第一,Test class can only have one constructor

被@Test注解修饰的方法所在的类只能有一个构造器

第二,Test class should have exactly one public zero-argument constructor

被@Test注解修饰的方法所在的类只能有一个修饰符为public公有的无参的构造器

public class Demo2 {

    public Demo2 (int i,int j) {

    }

    public Demo2 (int i) {

    }

    public Demo2 (String str,int i) {
        System.out.println("String str,int i");
    }

    public Demo2 (int i,String str) {
        System.out.println("int i,String str");
    }


    @Test
    public void test1(){
        Demo2 d1 = new Demo2("a",1);
    }

    @Test
    public void test2(){
        Demo2 d2 = new Demo2(1,"a");
    }

    public static void main(String[] args) {
        Demo2 d1 = new Demo2("a",1);
        Demo2 d2 = new Demo2(1,"a");
    }

}

以上示例代码,运行main方法:正常
运行test1或test2:报错

总结:类默认有无参构造器,使用junit测试@Test注解,加任何带参的显式的构造器都不被允许;如果要显式声明无参构造器,也只能是public的,不能是其他修饰符。

你可能感兴趣的:(关于junit测试,构造器常见的错误Test class can only have one constructor和Test class should have exactly one......)