I am trying to make fun of calling the final ResourceBundle.getString() method. With PowerMock 1.4.12 and EasyMock 3.1, the call is not mocked; instead, the "real" method is called.
My test class:
@RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { ResourceBundle resourceBundleMock = PowerMock.createNiceMock(ResourceBundle.class); expect(resourceBundleMock.getString(BundleConstants.QUEUE)).andReturn("Queue"); PowerMock.replay(resourceBundleMock); beanBeingTested.setMessages(resourceBundleMock); } ... }
Code in BeanBeingTested:
private ResourceBundle messages; ... String label = messages.getString(BundleConstants.QUEUE);
Error message:
java.util.MissingResourceException: Can't find resource for bundle $java.util.ResourceBundle$$EnhancerByCGLIB$$e4a02557, key Queue at java.util.ResourceBundle.getObject(ResourceBundle.java:384) at java.util.ResourceBundle.getString(ResourceBundle.java:344) at com.yoyodyne.BeanBeingTested.setUpMenus(BeanBeingTested.java:87)
When I move on to the test case, the debugger shows the beanBeingTested.messages type as "EasyMock for the java.util.ResourceBundle class", so the layout is entered correctly. (In addition, there is no error during getString() in the call to expect() during setup).
With a simple mock instead of a nice layout, I get the following error:
java.lang.AssertionError: Unexpected method call handleGetObject("Queue"): getString("Queue"): expected: 1, actual: 0
Any idea what I'm doing wrong?
Thanks.
source share