I have a class as
public class Authentication {
private Timer timer;
public Authentication(){
// constructor
}
public void authenticate(){
// Initialize timer in this method
timer = new RouterTimer(FAKE_PARAM);
testingMethod();
}
@VisibleForTesting
void testingMethod(){
timer.method();
// other stuff
}
}
I have a Timer object as a class attribute, but it isn't initialized in constructor, it gets initialized in authenticate().
I'm writing unit test for testingMethod(), but the tests failed because the timer is null and calling the method() will throw an NullPointerException. I can't get the timer initialized with the constructor of the class. I can't call authenticate() to initialize because that method will call testingMethod().
How do I mock or do something else in this case?