How to mock ResourceBundle.getString()?

前端 未结 4 1062
盖世英雄少女心
盖世英雄少女心 2021-02-19 03:35

I\'m failing to mock ResourceBundle.getString().

This is my code:

ResourceBundle schemaBundle = Mockito.mock(ResourceBundle.class);
Mockito.         


        
4条回答
  •  眼角桃花
    2021-02-19 03:50

    You'll find an example of solution below :

    @RunWith(PowerMockRunner.class)
    @PrepareForTest({ ResourceBundle.class })
    public class ResourceBundleTest {
    
        @Test
        public void getStringByPowerMock() throws Exception {   
            ResourceBundle resourceBundle = PowerMockito.mock(ResourceBundle.class);
            Mockito.when(resourceBundle.getString(Mockito.anyString())).thenReturn("Hello world....");
            System.out.println(resourceBundle.getString("keyword"));
        }
    
    }
    

提交回复
热议问题