我试图模拟对 final 方法的调用ResourceBundle.getString()
。使用 PowerMock 1.4.12 和 EasyMock 3.1,调用不会被模拟;而是调用“真实”方法。
我的测试课:
@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);
}
...
}
BeanBeingTested 中的代码:
private ResourceBundle messages;
...
String label = messages.getString(BundleConstants.QUEUE);
错误信息:
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)
当我逐步完成测试用例时,调试器将类型显示beanBeingTested.messages
为“EasyMock for class java.util.ResourceBundle”,因此模拟被正确注入。(此外,在设置过程中调用getString()
内部调用没有错误)。expect()
使用普通模拟而不是漂亮模拟,我收到以下错误:
java.lang.AssertionError:
Unexpected method call handleGetObject("Queue"):
getString("Queue"): expected: 1, actual: 0
知道我做错了什么吗?
谢谢。