I write up a simple function below to illustrate what I try to achieve.
public class MyTempClass {
private boolean myVariable = false;
private void setupMyVariable() {
// Some callback etc from a web service that will determine the myVariable value
myVariable = true;
}
public void doSomethingIfVaraibleIsTrue() {
if (myVariable) {
// Do something.
}
}
}
I would want to unit test MyTempClass doSomethingIfVaraibleIsTrue
function is doing something when myVariable
is true, and not doing something when myVariable
is false. However myVariable
is a private boolean, and not easily set (e.g. it is set from a callback of a service).
I also don't want to make a setter for myVariable
as it is really private to the class. Is there any way to Mock/Stud or make myVariable
true so that I could unit test my doSomethingIfVaraibleIsTrue
?