1

i am using µJava for mutation testing of my java program. as i am learning mutation testing.

i have 2 classes

1 : Parent

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

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

and i am applying IOD operation of muJava. `hence it is generating mutant . it is deleting overridden method printTemp of child class.

my TestCase is :

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

But when i am running Mutation Testing , i have found the mutant lived. i want to kill it ! What can i do ??

KrunalParmar
  • 1,062
  • 2
  • 18
  • 31
  • No idea what µJava is and I do not see anything in the code you provided that is not vanilla Java. As a side note: `c1` is also of type `Parent` as `Child` is a subclass of `Parent` – hotzst Oct 03 '15 at 20:02

1 Answers1

1

MuJava has switched its testing infrastructure to JUnit (see https://cs.gmu.edu/~offutt/mujava/, Section III.3). This means that you should write a JUnit test, which not only covers the code but also asserts on the result.

Example:

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

If you apply the IOD mutation operator, the first test should detect that mutant (i.e., it should fail because printTemp returns "temp is : Child20").

As an additional comment, the reference p2 in your test code is also an instance of Child, so c1.printTemp() and p2.printTemp() both invoke the method printTemp in your class Child.

Rene Just
  • 81
  • 2
  • Can you justify that why i need to put assert ? and what it actually does ? – KrunalParmar Oct 05 '15 at 18:29
  • A call to assertEquals raises an AssertionError (which will make the test fail) if the expected value and the actual value differ. You might want to take a look at [wiki](https://github.com/junit-team/junit/wiki/Assertions) and [javadoc](http://junit.org/apidocs/org/junit/Assert.html) for more details on JUnit's assert methods. Keep in mind that the overall goal is to make a test fail on a mutant. So, unless your test includes an assertion that checks the actual outcome of the tested method, it can only detect a mutant if that mutant raises an unexpected exception. – Rene Just Oct 07 '15 at 17:05