gpt4 book ai didi

java - µJava不是在杀变种人吗?

转载 作者:行者123 更新时间:2023-11-30 07:54:06 26 4
gpt4 key购买 nike

我正在使用μ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: child

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

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

最佳答案

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 方法。

关于java - µJava不是在杀变种人吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32925644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com