17

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?

BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • 4
    See http://stackoverflow.com/questions/3793791/final-method-mocking – axtavt Jul 30 '14 at 07:12
  • My bad, I missed that getString is final. Feel free to add your comment as an answer in order you get the credits. – BetaRide Jul 30 '14 at 07:18
  • You may consider introducing one more bean - let's say `Messages`. It will have initialization block (locale setup, `getBundle` call) and `get(String key, String ... args)`. Next use through out your application calls to `Messages` instead of `ResourceBundle` coupling. And finally you may mock `Messages` in unit tests without any difficulties. – Aleksei Egorov May 06 '16 at 10:46
  • 1
    I've found another option, https://stackoverflow.com/a/10816819/1060779 just adding the messages_xx_XX.properties in src/test/resources and it worked. – maxivis May 29 '17 at 19:24
  • Another question being a kind of duplicate: https://stackoverflow.com/q/18377160/772981. Also contains an answer "no need to mock". – Jarekczek Jul 24 '18 at 07:43

5 Answers5

17

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.

ccpizza
  • 28,968
  • 18
  • 162
  • 169
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"));
    }

}
user2049200
  • 127
  • 2
  • 10
2

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

        }
}
Kumar Abhishek
  • 3,004
  • 33
  • 29
1

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.

Community
  • 1
  • 1
Julius Musseau
  • 4,037
  • 23
  • 27
0

If You want mock such a situation:

ResourceBundle messages = ResourceBundle.getBundle("messages");
var msg = messages.getString("company.wizard.field.IsEnabledFromToReqDto.isNull");

U should use the next working variant:

@Test
void testMethodName() {
        //normally mock static
        try (MockedStatic<ResourceBundle> contextMock = mockStatic(ResourceBundle.class)) {
            //mock an object of mocked static
            ResourceBundle messages = mock(ResourceBundle.class);
            contextMock.when(() -> ResourceBundle.getBundle("messages")).thenReturn(messages);
            when(messages.getString(anyString())).thenReturn("Error message"); //or your own mock for messages

            //... your test...
            //... your verifying...
        }
    }