【问题标题】:µJava Is not killing Mutants?µJava 不是要杀死 Mutants 吗?
【发布时间】:2015-12-31 18:06:32
【问题描述】:

我正在使用 µJava 对我的 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。

我的测试用例是:

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

但是当我运行 Mutation Testing 时,我发现突变体还活着。我想杀了它! 我该怎么办??

【问题讨论】:

  • 不知道 µJava 是什么,我在您提供的代码中看不到任何不是普通 Java 的东西。作为旁注:c1 也是Parent 类型,因为ChildParent 的子类

标签: java unit-testing mutation mutation-testing


【解决方案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 方法。

【讨论】:

  • 你能证明我为什么需要断言吗?它实际上做了什么?
  • 如果预期值和实际值不同,则调用 assertEquals 会引发 AssertionError(这将使测试失败)。您可能想查看wikijavadoc 以了解有关 JUnit 断言方法的更多详细信息。请记住,总体目标是使突变体的测试失败。因此,除非您的测试包含一个检查被测方法实际结果的断言,否则它只能在该突变体引发意外异常时检测到该突变体。
猜你喜欢
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 2012-02-04
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
相关资源
最近更新 更多