0

我正在使用 Randoop 生成 Junit,并且当参数不是预置类型时它总是传递空值,例如我有 ClassA 类,它具有非预置类型的方法作为参数,例如当 mehtod methodString(String str, Entity entity) 被测试生成的 Junits 具有传递的实体值的空值,并且它没有创建可以传递的有效对象。

编辑:我正在使用 eclipse 运行测试,所以我添加了在类路径中运行 Randoop 所需的必要 jar,下面是我通过的 args

gentests --testclass=test.ClassA --time-limit=60

注意:我还不能添加图像。所以我无法共享 Eclipse 运行命令窗口。

如何配置 Randoop 为 Junit 测试传递非空值。

A类

  package test;

import java.util.Calendar;

public class ClassA {

    public void method(String str, Calendar date) {

        String  a ="a";
        String  b ="b";
        String  c ="c";
        String  d ="d";

        if(a.equals(str)) {
            System.out.println("True");
        }else if(a.equals(str)) {
            System.out.println("True");
        }else if(c.equals(str)) {
            System.out.println("True");
        }else if(b.equals(str)) {
            System.out.println("True");
        }else if(d.equals(str)) {
            System.out.println("True");
        }
    }

public void method2(String str, Entity entity) {

        String  a ="a";
        String  b ="b";
        String  c ="c";
        String  d ="d";

        if(a.equals(str)) {
            System.out.println("True");
        }else if(a.equals(str)) {
            System.out.println("True");
        }else if(c.equals(str)) {
            System.out.println("True");
        }else if(b.equals(str)) {
            System.out.println("True");
        }else if(d.equals(str)) {
            System.out.println("True");
        }

        if(entity.getName().equals("Sting")) {
            System.out.println("name");
        } else if (entity.getEmail().equals("Sting")){
            System.out.println("email");
        }       
    }
}

实体类

package test;

public class Entity {

    private String name;
    private String email;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    } 

}

生成的示例 Junit

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RegressionTest3 {

    public static boolean debug = false;

    @Test
    public void test001() throws Throwable {
        if (debug)
            System.out.format("%n%s%n", "RegressionTest3.test001");
        test.ClassA classA0 = new test.ClassA();
        java.util.Calendar calendar2 = null;
        classA0.method("hi!", calendar2);
        java.util.Calendar calendar5 = null;
        classA0.method("hi!", calendar5);
        java.util.Calendar calendar8 = null;
        classA0.method("", calendar8);
        java.lang.Class<?> wildcardClass10 = classA0.getClass();
        java.util.Calendar calendar12 = null;
        classA0.method("hi!", calendar12);
        java.lang.Class<?> wildcardClass14 = classA0.getClass();
        java.util.Calendar calendar16 = null;
        classA0.method("hi!", calendar16);
        java.util.Calendar calendar19 = null;
        classA0.method("hi!", calendar19);
        test.Entity entity22 = null;
        try {
            java.lang.String str23 = classA0.methodString("hi!", entity22);
            org.junit.Assert.fail("Expected exception of type java.lang.NullPointerException; message: null");
        } catch (java.lang.NullPointerException e) {
        }
        org.junit.Assert.assertNotNull(wildcardClass10);
        org.junit.Assert.assertNotNull(wildcardClass14);
    }

    @Test
    public void test002() throws Throwable {
        if (debug)
            System.out.format("%n%s%n", "RegressionTest3.test002");
        test.ClassA classA0 = new test.ClassA();
        java.util.Calendar calendar2 = null;
        classA0.method("hi!", calendar2);
        java.util.Calendar calendar5 = null;
        classA0.method("hi!", calendar5);
        java.lang.Class<?> wildcardClass7 = classA0.getClass();
        java.lang.Class<?> wildcardClass8 = classA0.getClass();
        java.util.Calendar calendar10 = null;
        classA0.method("hi!", calendar10);
        java.util.Calendar calendar13 = null;
        classA0.method("", calendar13);
        java.util.Calendar calendar16 = null;
        classA0.method("hi!", calendar16);
        java.util.Calendar calendar19 = null;
        classA0.method("", calendar19);
        java.util.Calendar calendar22 = null;
        classA0.method("", calendar22);
        java.util.Calendar calendar25 = null;
        classA0.method("", calendar25);
        org.junit.Assert.assertNotNull(wildcardClass7);
        org.junit.Assert.assertNotNull(wildcardClass8);
    }

    @Test
    public void test003() throws Throwable {
        if (debug)
            System.out.format("%n%s%n", "RegressionTest3.test003");
        test.ClassA classA0 = new test.ClassA();
        java.util.Calendar calendar2 = null;
        classA0.method("", calendar2);
        java.util.Calendar calendar5 = null;
        classA0.method("", calendar5);
        java.lang.Class<?> wildcardClass7 = classA0.getClass();
        java.util.Calendar calendar9 = null;
        classA0.method("", calendar9);
        java.lang.Class<?> wildcardClass11 = classA0.getClass();
        java.lang.Class<?> wildcardClass12 = classA0.getClass();
        java.lang.Class<?> wildcardClass13 = classA0.getClass();
        java.util.Calendar calendar15 = null;
        classA0.method("hi!", calendar15);
        java.util.Calendar calendar18 = null;
        classA0.method("", calendar18);
        test.Entity entity21 = null;
        try {
            classA0.method2("", entity21);
            org.junit.Assert.fail("Expected exception of type java.lang.NullPointerException; message: null");
        } catch (java.lang.NullPointerException e) {
        }
        org.junit.Assert.assertNotNull(wildcardClass7);
        org.junit.Assert.assertNotNull(wildcardClass11);
        org.junit.Assert.assertNotNull(wildcardClass12);
        org.junit.Assert.assertNotNull(wildcardClass13);
    }
4

1 回答 1

0

问题是 Randoop 无法为抽象类生成测试用例,在本例中为 java.util.Calendar。

您可以在命令行或类列表文件中提供扩展抽象类的类的名称。例如,您可以使用 --classlist=myclasses 而不是 --testclass=test.ClassA 执行 Randoop;并在 myclasses 文件中(名称中没有任何扩展名),输入:

test.ClassA
java.util.GregorianCalendar

我这样做了,Randoop 会生成一些测试,例如:

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RegressionTest1 {

  public static boolean debug = false;

  @Test
  public void test001() throws Throwable {

    if (debug) { System.out.format("%n%s%n","RegressionTest1.test001"); }


    test.ClassA classA0 = new test.ClassA();
    java.util.GregorianCalendar gregorianCalendar2 = new java.util.GregorianCalendar();
    java.lang.String str3 = gregorianCalendar2.getCalendarType();
    classA0.method("java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"America/Argentina/Cordoba\",offset=-10800000,dstSavings=0,useDaylight=false,transitions=62,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=5,MINUTE=18,SECOND=8,MILLISECOND=53,ZONE_OFFSET=-10800000,DST_OFFSET=0]", (java.util.Calendar)gregorianCalendar2);
    java.util.GregorianCalendar gregorianCalendar6 = new java.util.GregorianCalendar();
    int i7 = gregorianCalendar6.getMinimalDaysInFirstWeek();
    java.util.Date date8 = gregorianCalendar6.getGregorianChange();
    int i10 = gregorianCalendar6.getMaximum(3);
    java.util.GregorianCalendar gregorianCalendar11 = new java.util.GregorianCalendar();
    int i12 = gregorianCalendar11.getMinimalDaysInFirstWeek();
    java.util.Date date13 = gregorianCalendar11.getGregorianChange();
    boolean b15 = gregorianCalendar11.isSet(16);
    int i17 = gregorianCalendar11.getActualMaximum(9);
    gregorianCalendar11.setLenient(false);
    int i20 = gregorianCalendar6.compareTo((java.util.Calendar)gregorianCalendar11);
    int i21 = gregorianCalendar6.getWeeksInWeekYear();
    int i23 = gregorianCalendar6.getGreatestMinimum(10);
    classA0.method("", (java.util.Calendar)gregorianCalendar6);
    java.util.Calendar calendar26 = null;
    classA0.method("java.util.GregorianCalendar[time=1528409887986,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"America/Argentina/Cordoba\",offset=-10800000,dstSavings=0,useDaylight=false,transitions=62,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=18,SECOND=7,MILLISECOND=986,ZONE_OFFSET=-10800000,DST_OFFSET=0]", calendar26);
    java.util.GregorianCalendar gregorianCalendar29 = new java.util.GregorianCalendar();
    int i30 = gregorianCalendar29.getMinimalDaysInFirstWeek();
    java.util.Date date31 = gregorianCalendar29.getGregorianChange();
    int i33 = gregorianCalendar29.getMaximum(3);
    gregorianCalendar29.setLenient(false);
    boolean b36 = gregorianCalendar29.isWeekDateSupported();
    classA0.method("java.util.GregorianCalendar[time=1528409888438,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"America/Argentina/Cordoba\",offset=-10800000,dstSavings=0,useDaylight=false,transitions=62,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=18,SECOND=8,MILLISECOND=438,ZONE_OFFSET=-10800000,DST_OFFSET=0]", (java.util.Calendar)gregorianCalendar29);
    java.util.GregorianCalendar gregorianCalendar39 = new java.util.GregorianCalendar();
    int i40 = gregorianCalendar39.getMinimalDaysInFirstWeek();
    java.util.Date date41 = gregorianCalendar39.getGregorianChange();
    int i43 = gregorianCalendar39.getMaximum(3);
    gregorianCalendar39.setLenient(false);
    boolean b46 = gregorianCalendar39.isWeekDateSupported();
    java.util.GregorianCalendar gregorianCalendar47 = new java.util.GregorianCalendar();
    gregorianCalendar47.setMinimalDaysInFirstWeek(0);
    int i51 = gregorianCalendar47.get(10);
    int i52 = gregorianCalendar47.getWeeksInWeekYear();
    boolean b53 = gregorianCalendar39.after((java.lang.Object)gregorianCalendar47);
    gregorianCalendar47.set(1, 30, 10, 3, 5);
    java.util.GregorianCalendar gregorianCalendar60 = new java.util.GregorianCalendar();
    int i61 = gregorianCalendar60.getMinimalDaysInFirstWeek();
    java.util.Date date62 = gregorianCalendar60.getGregorianChange();
    gregorianCalendar60.roll(4, false);
    java.lang.Object obj66 = null;
    boolean b67 = gregorianCalendar60.before(obj66);
    gregorianCalendar60.setMinimalDaysInFirstWeek(11);
    boolean b70 = gregorianCalendar47.before((java.lang.Object)gregorianCalendar60);
    classA0.method("java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"America/Argentina/Cordoba\",offset=-10800000,dstSavings=0,useDaylight=false,transitions=62,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=5,MINUTE=18,SECOND=8,MILLISECOND=658,ZONE_OFFSET=-10800000,DST_OFFSET=0]", (java.util.Calendar)gregorianCalendar47);
    test.Entity entity73 = null;
    // The following exception was thrown during execution in test generation
    try {
      classA0.method2("java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id=\"America/Argentina/Cordoba\",offset=-10800000,dstSavings=0,useDaylight=false,transitions=62,lastRule=null],firstDayOfWeek=2,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=5,WEEK_OF_YEAR=23,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=158,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=7,HOUR_OF_DAY=5,MINUTE=18,SECOND=7,MILLISECOND=993,ZONE_OFFSET=-10800000,DST_OFFSET=0]", entity73);
      org.junit.Assert.fail("Expected exception of type java.lang.NullPointerException");
    } catch (java.lang.NullPointerException e) {
      // Expected exception.
      if (! e.getClass().getCanonicalName().equals("java.lang.NullPointerException")) {
        org.junit.Assert.fail("Expected exception of type java.lang.NullPointerException, got " + e.getClass().getCanonicalName());
      }
    }
于 2018-06-07T22:29:28.333 回答