Mutation of Getters and Setter

911 views
Skip to first unread message

Daryl Handley

unread,
Nov 6, 2015, 2:19:53 PM11/6/15
to PIT Users
Hi, 

I am playing around with pitest and trying to create a simple example that I present to coworkers to explain mutation testing and pitest. The goal is to show my coworkers what pitest can do to help improve our tests and give an overview of how mutation testing works. I created a Person class and a PersonTest class that provides 100% code coverage but really doesn't test anything. 

Here is my class 

public class Person {

public String firstName;
public String lastName;
public Integer age;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public boolean isOldEnoughToDrink() {
return age != null && age.intValue() >= 19;
}
}


and here is my test 


@Test
public void testPerson() {
Person person = new Person();
person.setFirstName("Money");
person.setLastName("Penny");
person.setAge(new Integer(21));

Assert.assertNotNull(person.getAge());
Assert.assertNotNull(person.getFirstName());
Assert.assertNotNull(person.getLastName());

person.isOldEnoughToDrink();

person.setAge(null);
person.isOldEnoughToDrink();

person.setAge(new Integer(17));
person.isOldEnoughToDrink();

}


Now my test is kind of useless because it doesn't really assert anything except for non nullables. However when I run pitest it actually passes on all mutations on the getters and setters. This seems strange to me as my test is obviously no good. If I rem

Is there a mutator I can use that will mutate the return value of my getters (example add a "1" at the end) ? 
Is there a mutator I can use that will mutate the set value of my setters (similar idea as above) ? 

Any other ways I can prove that this test is inadequate ? 

Thanks
Daryl 

henry

unread,
Nov 12, 2015, 10:18:26 AM11/12/15
to PIT Users

On Friday, 6 November 2015 19:19:53 UTC, Daryl Handley wrote:

Is there a mutator I can use that will mutate the return value of my getters (example add a "1" at the end) ? 
Is there a mutator I can use that will mutate the set value of my setters (similar idea as above) ? 


There is not currently a mutator that will modify strings, it ought to be easy to write but I'm not sure how much value it would add. It would result in a failing mutation in the specific case you present here, but I think it would also produce a lot of not very valuable mutations.

The member variable mutator ought to mutate the assignments in the setters as you suggest.

http://pitest.org/quickstart/mutators/#EXPERIMENTAL_MEMBER_VARIABLE

This is disabled by default as it can create equivalent mutations in constructors. As I'm writing this it has become obvious that a variant of this mutator that did not create mutant in constructors would be valuable.

Thanks

Henry
Reply all
Reply to author
Forward
0 new messages