0

I perform mutation testing, and on this line

for(i=0;i<100;i++)

is created a mutant "Changed conditional boundary". I know that the operator '<' is changed to '<='. My question is how can I kill this mutant? In addition is the whole for loop.

public int addAccount(BankAccount acc) {
    int i=0;
    for(i=0;i<100;i++) {
        if(getAccounts()[i]==null) {
            break;
        }
    }
    getAccounts()[i]=acc;
    return i;
}
Programmer2B
  • 552
  • 3
  • 14

1 Answers1

0

It depends on what is happening inside the loop. Whatever that is, it will happen one less time when the boundary is changed.

Presumably the contents of the loop have some sort of observable effect, otherwise there would be no point in it happening, and it would be impossible to write a test.

henry
  • 5,923
  • 29
  • 46
  • I've updated my question. Can you be more specific? – Programmer2B Sep 05 '21 at 12:01
  • The code you posted still has no observable effect. You need to post enough to show how the things that happen inside the loop effect the outside world (e.g by changing the return value of the method, or causing some state to change somewhere). It may help if you also post the test you think covers this fucntionality. – henry Sep 05 '21 at 15:50
  • I've posted the whole method. I really don't have an idea what test will kill the mutant. – Programmer2B Sep 05 '21 at 16:32
  • There are two observable effects: the return value, and the modification to the arrary returned by getAccounts(). You need a test where getAccounts()[i] never returns null. The value of i returned from the method will change when the mutation is present. – henry Sep 05 '21 at 16:47
  • Right, the return value will change, but I get ArrayIndexOutOfBoundsException when I try to add more than 100 accounts to the array because the mutant is <=100. What should I do to kill it? – Programmer2B Sep 05 '21 at 18:32
  • 1
    Your test will need to pass in a larger array, or if the only reason this loop is coded to 100 is that happens to be the size of the array that's passed in, change the code so it iterates instead `for (BankAccount a : getAccounts())` This would get rid of the mutant and make the code easier to maintain. – henry Sep 05 '21 at 19:38
  • It's open-source code, I just need to test it, not change it. Thank you for the help, I really appreciate it! – Programmer2B Sep 05 '21 at 19:57