0

This is the test class has 2 methods.

@Service
class TestClass {

    public void testingMethod() {
        if (isTimeToExecute("task1")) {
            // .. 
            // .. 
            // .. 
            // logic  
        }
    }

    private boolean isTimeToExecute(String taskName) {
        Calendar calender = Calendar.getInstance();
        boolean shouldBeExecuted = false;
        switch (taskName) {
            case "task1":
                shouldBeExecuted = calender.get(Calendar.HOUR_OF_DAY) == 23;
                break;
            case "task2":
                shouldBeExecuted = calender.get(Calendar.DAY_OF_WEEK) == 6 && calender.get(Calendar.HOUR_OF_DAY) == 23;
                break;
        }
        return shouldBeExecuted;
    }
}

I want to test testingMethod() and need to spyOn(isTimeToExecute()) to always return true.

@Inject
private TestClass testClass;

@Test
public void testTestingMethod() {
    testClass.testingMethod();
}
kandarp
  • 991
  • 1
  • 14
  • 35
  • it's not good to test & assert a void function. It's a good point to test and verify `isTimeToExecute()` as a separate unit test. – exudong Jun 24 '21 at 06:18
  • That I already did, however, I need to test that void function. It has very complex logic on the database level and I need to check it. But due to IsTimeToExecute() is not true I can't reach that point. In ways to achieve that? – kandarp Jun 24 '21 at 06:30
  • 1
    if you really want to do it you can refer https://stackoverflow.com/questions/7803944/how-to-mock-private-method-for-testing-using-powermock – exudong Jun 24 '21 at 06:42

0 Answers0