我使用对我的java程序进行变异测试。因为我正在学习突变测试。
我有两个班
1:父母
public class Parent
{
public String temp;
public int number;
public Parent(String temp)
{
this.temp = temp;
this.number = 20;
}
public String printTemp()
{
return "temp is : "+temp+number;
}
} 和2:儿童
public class Child extends Parent
{
public int number;
public Child(String temp)
{
super(temp);
this.number = 5;
}
public String printTemp()
{
String temp = "i am fake !";
int number = 766;
return "temp is : "+super.temp+this.number+"c";
}
}我正在应用muJava的IOD操作。“因此,它正在产生突变体。它正在删除子类的重写方法printTemp。
我的TestCase是:
public class MyTest
{
public String test1 ()
{
String result;
Parent p1 = new Parent("i am temp of parent");
Child c1 = new Child("i am temp of child");
Parent p2 = new Child("i am both !");
result = ""+ c1.printTemp() + p1.printTemp() + p2.printTemp();
return result;
}
}但是当我进行突变测试时,我发现这个突变体是活的。我想杀了它!我能做什么??
发布于 2015-10-05 18:15:39
MuJava已经将其测试基础设施转换为JUnit (参见https://cs.gmu.edu/~offutt/mujava/,第III.3节)。这意味着您应该编写一个JUnit测试,它不仅涵盖了代码,而且还声明了结果。
示例:
@Test
public void testPrintTempChild() {
Child c = new Child("Child");
String actual = c.printTemp();
String expected = "temp is : Child5c";
assertEquals(expected, actual);
}
@Test
public void testPrintTempParent() {
Parent p = new Parent("Parent");
String actual = p.printTemp();
String expected = "temp is : Parent20";
assertEquals(expected, actual);
}如果应用IOD突变操作符,第一个测试应该检测到该突变体(也就是说,它应该失败,因为printTemp返回"temp : Child20")。
另外,测试代码中的引用p2也是子实例,因此c1.printTemp()和p2.printTemp()都调用了类printTemp中的方法printTemp。
https://stackoverflow.com/questions/32925644
复制相似问题