I have a private method which is used by multiple processes and I'm writing unit test for a public method, during pitest a mutation which removes call to a line survives so in order to fix it(without telling test to ignore that mutation) I want to check that object is updated with certain value in middle of process and another value at the end of process. Is it possible?
public class MyClass {
public List<Mango> publicMethod() {
List<Mango> mangos = apiCall();
privateMethod1(mangos);
privateMethod2(mangos);
return mangos;
}
private void privateMethod1(List<Mango> mangos) {
mangos.forEach(mango -> {
if(mango.getName("indigenous") {
mango.setAmount(100);
mango.setQuantity(10);
}
});
}
private void privateMethod2(List<Mango> mangos) {
mangos.forEach(mango -> {
if(mango.getName("indigenous") {
mango.setAmount(50);
}
}
});
}
In unit test for publicMethod(), removed call to mango.setAmount(100); survives as it gets updated again in privateMethod2 but privateMethod1 is used by multiple processes so it should not be updated. In actual scenario there are multiple things happening in those methods but to explain issue I'm using simple set method to an object. In place of mango.setAmount(100); in actual code there is a private method which will update some values of the object.
To make pitest not to ignore that line I tried to use Mockito.verify() to check that private method is being called twice, but mokito doesn't allow us to verify private methods.
You shouldn't be concerned with the behavior of the private methods, as the unit tests would only be concerned with the input/output of the public method.
If you really need to achieve this, you could try to create a spy of
Mangoobject (Mango mango = Mockito.spy(Mango.class)) and check that the setters were called as you expected them:verify(mango).setAmount(100)andverify(mango).setAmount(50)