当前位置: 动力学知识库 > 问答 > 编程问答 >

junit - How do I mock a ResourceBundle using Spring and PowerMock?

问题描述:

I'm using PowerMock 1.5.1 and Spring 3.1.1.RELEASE trying to mock a ResourceBundle. I have tried

 ResourceBundle resourceBundleMock = createMock(ResourceBundle.class);

expect(resourceBundleMock.getString("error.invalid.login")).andReturn(errorMsg);

but am getting the exception below

java.util.MissingResourceException: Can't find resource for bundle $java.util.ResourceBundle$$EnhancerByCGLIB$$8b09d230, key error.invalid.login

at java.util.ResourceBundle.getObject(ResourceBundle.java:374)

at java.util.ResourceBundle.getString(ResourceBundle.java:334)

at org.mainco.subco.ebook.test.mvc.LoginControllerTest.testLoginError(LoginControllerTest.java:578)

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.internal.runners.TestMethod.invoke(TestMethod.java:66)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)

at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)

at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)

at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)

at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)

at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)

at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)

at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)

at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)

at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)

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)

How do I mock what is returned by a call to the resourceBundle.getString method for use in my Spring controller? I instantiate a ResourceBundle in Spring by using

 private static ResourceBundle resourceBundle = ResourceBundle.getBundle("UserMessages", Locale.ENGLISH);

网友答案:

K, figured it out -- wasn't injecting my mock properly. The following healed the pain

@RunWith(PowerMockRunner.class)
@PrepareForTest({ SecurityContextHolder.class })
public class LoginControllerTest 
{

    @Mock
    private ResourceBundle bundle;

    …

    @Test
    public final void testLoginError()
    {
        mockStatic(ResourceBundle.class);
        expect(ResourceBundle.getBundle("UserMessages", Locale.ENGLISH)).andReturn(bundle);

        final String errorMsg = "Invalid Login. Please reenter your username and password.";
        expect(bundle.getString("error.invalid.login")).andReturn(errorMsg);

        expect(loginService.getUrlProperties("localhost")).andReturn(url);
        replayAll();
分享给朋友:
您可能感兴趣的文章:
随机阅读: