我正在使用 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);
}