Mock config.getInt()

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));     } 
Add Comment
1 Answer(s)

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.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.