如何模拟ResourceBundle.getString()方法?

17

我无法模拟 ResourceBundle.getString()

这是我的代码:

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

在第二行会出现以下异常:

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)

在我看来,这似乎是因为schemaBundle没有被模拟。但是调试器清楚地显示该实例被mockito包装。

我也尝试过使用

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

但是这会返回相同的异常。有什么想法是错了吗?

4
请参阅 https://dev59.com/vW865IYBdhLWcg3wd-ge。 - axtavt
我的错,我错过了getString是final的。请随意将您的评论添加为答案,以便获得积分。 - BetaRide
你可以考虑引入另一个bean,比如说Messages。它将有初始化块(区域设置、getBundle调用)和get(String key, String ... args)方法。接下来,在整个应用程序中使用Messages调用而不是ResourceBundle耦合。最后,你可以在单元测试中轻松地模拟Messages - Aleksei Egorov
1
我找到了另一个选项,https://dev59.com/H2TWa4cB1Zd3GeqPC2nx#10816819 只需将 messages_xx_XX.properties 添加到 src/test/resources 中即可。 - maxivis
另一个问题是一种重复:https://stackoverflow.com/q/18377160/772981。还包含一个回答“不需要模拟”。 - Jarekczek
5个回答

17

你可以创建一个虚假的 ResourceBundle 实现,然后将其作为参数传递给 .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)

如果你需要实际的键和值,那么你需要为 getKeys() 提供一个实现,比如一个用于存储和键查找的 hashmap。


2

@Powermockito无法正常工作,因为ResourceBundle.class具有静态final方法,难以模拟。

我尝试过了。

在主类中提取您的方法到另一个公共方法中,然后使用实现重写它。

这里ReviewEChannelApplicationMBean是我的控制器,在这里我重写了getBundle方法。

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

//这个类是使用ListResourceBundle创建测试数据的TestDataBuilder

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

        }
}

2
您将在下面找到一个解决方案的示例:
@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"));
    }

}

1
我想出了一种通过子类化ResourceBundle.Control来模拟ResourceBundle的方法。我的答案在这里:

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

我更倾向于避免使用PowerMock、JMockit等动态字节码重写工具(以删除final修饰符),因为Jacoco和其他工具似乎对此不友好。


0
如果你想嘲笑这样的情况:
ResourceBundle messages = ResourceBundle.getBundle("messages");
var msg = messages.getString("company.wizard.field.IsEnabledFromToReqDto.isNull");

您应该使用下一个可行的变体:
@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...
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接