Mock config.getInt()

338 Views Asked by At

How can I mock config.getInt("getNoOfDays",100) in MockitoJUnitRunner?

I have tried

     @Test(expected = IllegalStateException.class)
            public void populateAddress() {
                 Mockito.when(Integer.valueOf(Config.getInt("getNoOfDays", 100))).thenReturn(
                Integer.valueOf(100));
    }
1

There are 1 best solutions below

2
On

Mockito cannot mock statics methods since it is not a good approach to mock them. There is a test library PowerMock that helps to do it.

Here is the example how it can work:

PowerMockito.mockStatic(Integer.class);
BDDMockito.given(Integer.valueOf(...)).willReturn(...);

BTW: In your case you can mock the Config itself.