1

我正在使用µJava对我的 java 程序进行突变测试。因为我正在学习突变测试。

我有 2 节课

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。

我的测试用例是:

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;
      }
   }

但是当我运行突变测试时,我发现突变体还活着。我想杀了它!我能做些什么 ??

4

1 回答 1

1

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 is : Child20”)。

作为补充说明,您的测试代码中的引用 p2 也是 Child 的一个实例,因此 c1.printTemp() 和 p2.printTemp() 都调用了 Child 类中的 printTemp 方法。

于 2015-10-05T18:15:39.820 回答