我正在使用Randoop生成Junit,并且当参数不是premitive类型时它总是传递空值,例如我有类ClassA,它具有非premitive类型的方法作为参数,例如当mehtod methodString(String)测试str,实体实体)生成的Junits为传递的实体值具有空值,并且它不会创建可以传递的有效对象。
编辑:我正在使用eclipse来运行测试,所以我添加了在类路径中运行Randoop所需的必要的jar,下面是我传递的args
gentests --testclass = test.ClassA --time-limit = 60
注意:我还无法添加图片。所以我无法共享eclipse运行命令窗口。
如何配置Randoop为Junit测试传递非空值。
ClassA的
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);
}
答案 0 :(得分: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());
}
}