junit test case generator

前端 未结 5 1541
自闭症患者
自闭症患者 2021-02-08 12:00

Is there a good tool out there to automatically generate jUnit test cases based on some primitive template? This is so that test cases can be written by engineers who do not ha

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 12:51

    Sample code to generate the test cases in Java:

    import java.util.ArrayList;
    import java.util.List;
    
    public class JunitGenerator {
    
        public static List s = new ArrayList();
    
        static String clname="employee";
        static String clBigname="Employee";
    
        public static void main(String[] args) {
    
            s.add("String;name;Name");
            s.add("int;age;Age");
            s.add("Boolean;isAge;IsAge");
            s.add("Double;amount;Amount");
    
            System.out.println("import static org.junit.Assert.*;");
            System.out.println("import static org.junit.Assert.assertEquals;");
    
            System.out.println("import org.junit.Test;");
            System.out.println();
            System.out.println("public class "+clBigname+"Test{");
            System.out.println(""+clBigname+" "+clname+"=new "+clBigname+"();");
    
            for(String s1:s) {
                String[] a=s1.split(";");
                System.out.println("@Test");
                System.out.println("public void get"+a[2]+"Test(){");
                if(a[0].equalsIgnoreCase("int"))
                    System.out.println(a[0]+" "+a[1]+" =0"+";");
                if(a[0].equalsIgnoreCase("String"))
                    System.out.println(a[0]+" "+a[1]+" =null"+";");
                if(a[0].equalsIgnoreCase("Double"))
                    System.out.println(a[0]+" "+a[1]+" =0.0"+";");
                if(a[0].equalsIgnoreCase("Boolean"))
                    System.out.println(a[0]+" "+a[1]+" =false"+";");
                System.out.println(""+ clname +".set"+a[2]+"("+a[1]+");");
                if(!a[0].equalsIgnoreCase("String"))
                    System.out.println("assertNotNull("+ clname +".get"+a[2]+"());");
                if(a[0].equalsIgnoreCase("String"))
                    System.out.println("assertEquals("+a[1]+","+ clname +".get"+a[2]+"());");
                System.out.println("}");
            }
    
            System.out.println("}");
    
        }
    
    }
    

提交回复
热议问题