How to mock ResourceBundle.getString()?

前端 未结 4 1050
盖世英雄少女心
盖世英雄少女心 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:52

    @Powermockito did not worked as ResourceBundle.class have static final methods which were not easy to mock.

    I tried.

    In the Main Class extract your method inside another public method, and then overide it with implementaion.

    Here ReviewEChannelApplicationMBean is my Controller, where i overrriden the getBundle.

    ReviewEChannelApplicationMBean = spy(new ReviewEChannelApplicationMBean(){
                @Override
                public ResourceBundle getBundle(FacesContext fcContext) {
                    return TestDataBuilder.getResourceBundle();
                }
            });
    

    //This Class i my TestDataBuilder using ListResourceBundle

    public class TestDataBuilder {
        public static ResourceBundle getResourceBundle() {
                return new ListResourceBundle(){
    
                    @Override
                    protected Object[][] getContents() {
                        return contents;
                    }
    
                    private Object[][] contents = {
                            {"test1","01"},
                            {"test2","01"},
                            {"test3","01"},
                            {"test4","01"}
                    };
                };
    
            }
    }
    

提交回复
热议问题