'How to mock ResourceBundle.getString()?

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

This is my code:

ResourceBundle schemaBundle = Mockito.mock(ResourceBundle.class);
Mockito.when(schemaBundle.getString("testKey_testPropertyName_ect")).thenReturn("testString1");

This gives the following exception on the second line:

java.util.MissingResourceException: Can't find resource for bundle $java.util.ResourceBundle$$EnhancerByMockitoWithCGLIB$$9e259f03, key testKey_testPropertyName_ect
    at java.util.ResourceBundle.getObject(ResourceBundle.java:374)
    at java.util.ResourceBundle.getString(ResourceBundle.java:334)
    at com.foo.bar.resource.PropertyResourceTest.testGet(PropertyResourceTest.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

To me this looks as if schemaBundle isn't mocked at all. But the debuger clearly shows, that the instance is wrapped by mockito.

I also tried with

Mockito.doReturn("testString1").when(schemaBundle).getString("testKey_testPropertyName_ect");

but this returns the same exception.

Any idea what's wrong?



Solution 1:[1]

Instead of mocking you can create a dummy ResourceBundle implementation, and then pass it as an argument to .thenReturn(resourceBundle):

    import java.util.ResourceBundle;

    ResourceBundle dummyResourceBundle = new ResourceBundle() {
        @Override
        protected Object handleGetObject(String key) {
            return "fake_translated_value";
        }

        @Override
        public Enumeration<String> getKeys() {
            return Collections.emptyEnumeration();
        }
    };

    // Example usage
    when(request.getResourceBundle(any(Locale.class))).thenReturn(dummyResourceBundle)

If you need the actual keys and values, then you'll need to provide an implementation for getKeys(), e.g. a hashmap for storage and key lookup.

Solution 2:[2]

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"));
    }

}

Solution 3:[3]

I figured out a way to mock ResourceBundle by subclassing ResourceBundle.Control. My answer is here:

https://stackoverflow.com/a/28640458/290254

I prefer to avoid the dynamic bytecode rewriting (to remove final) of PowerMock, JMockit and friends, since Jacoco and other things seem to hate it.

Solution 4:[4]

@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"}
                };
            };

        }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Community
Solution 4 Kumar Abhishek