#Proper way to mock static method

1 messages · Page 1 of 1 (latest)

quasi depot
#

I am currently doing this:


    @Test
    public void testSumSumSingleAndBatchScansForPeriodReturnsCorrectSum() throws IOException {

        try(MockedStatic<CalculationUtil> calculationUtil = Mockito.mockStatic(CalculationUtil.class)){
            int expectedSum = 0;

            for(int i=0;i< treeNodes.length;i++){
                int finalI = i;
                calculationUtil.when(() -> CalculationUtil.getTotalScansByOrgId(Mockito.any(), eq(treeNodes[finalI].getId()), Mockito.any())).thenReturn(i);
                expectedSum+=i;
            }

            int actualSum = TreeUtil.sumSumSingleAndBatchScansForPeriod(root, Mockito.mock(ApiService.class), desiredDate);

            assertEquals(expectedSum, actualSum);
        }
    }

but it looks ugly as hell

How can I improve it?

alpine yewBOT
#

<@&987246883653156906> please have a look, thanks.

alpine yewBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

endar> mockedCalendar = Mockito.mockStatic(Calendar.class)) {
// mock the static method
mockedCalendar.when(Calendar::getInstance).thenReturn(calendar);

        // rest of the test code
    }
}

This is the proper way to mock a static method using `MockedStatic` in Mockito. The `MockedStatic` class allows you to mock static methods for a specific period of time within a test method. 

In this example, we are mocking the `getInstance` method of the `Calendar` class. We use the `when` method to specify that when `getInstance` is called, it should return our mocked `calendar` object.

After mocking the static method, you can proceed with writing your test code as usual. Once the test method completes, the original behavior of the static method will be restored.
olive barn
#

you could always redesign your code to avoid the need of mocking a static method, if thats an option

#

the ability to mock a static method exists for when your code depends on a third party library which you have no control over, and you're not able to introduce an intermediate type/facade which you could use in it's place

#

you could use PowerMockito for a cleaner solution, but you'd be adding a new dependency simply to mock a static method

little comet
#

don't

#

also another person asked a very similar thing with UUID.randomUUID