如何模拟ResourceBundle.getString()?

时间:2014-07-30 07:01:51

标签: java mockito

我没有模仿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根本没有被嘲笑。但是,debuger清楚地表明,实例被mockito包裹着。

我也试过

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

但这会返回相同的异常。

知道什么是错的吗?

4 个答案:

答案 0 :(得分:12)

您可以创建一个虚拟的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()提供正确的实现,这是一个用于存储和键查找的哈希映射。

答案 1 :(得分:1)

您将在下面找到解决方案的示例:

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

}

答案 2 :(得分:1)

我找到了一种通过继承ResourceBundle.Control来模拟ResourceBundle的方法。我的答案在这里:

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

我更喜欢避免动态字节码重写(删除最后的)PowerMock,JMockit和朋友,因为Jacoco和其他东西似乎讨厌它。

答案 3 :(得分:0)

@Powermockito没有用,因为ResourceBundle.class有静态的最终方法,不容易模拟。

我试过了。

在Main Class中,在另一个公共方法中提取你的方法,然后用实现方法覆盖它。

这里ReviewCohannelApplicationMBean是我的控制器,我在那里覆盖了getBundle。

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

//这个类我的TestDataBuilder使用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"}
                };
            };

        }
}