使用PowerMock + EasyMock模拟最终方法 [英] Mocking a final method with PowerMock + EasyMock

查看:124
本文介绍了使用PowerMock + EasyMock模拟最终方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模拟对最终方法 ResourceBundle.getString()的调用。使用PowerMock 1.4.12和EasyMock 3.1时,不会模拟该呼叫。而是调用真实方法。

I'm trying to mock a call to the final method ResourceBundle.getString(). With PowerMock 1.4.12 and EasyMock 3.1, the call is not being mocked; instead, the "real" method is called.

我的测试类:

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

BeanBeing中的代码

Code in 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 的类型。作为类java.util.ResourceBundle的EasyMock,因此可以正确注入模拟。 (此外,在设置过程中,在 expect()调用中对 getString()的调用没有错误)。

When I step through the test case, the debugger shows the type of beanBeingTested.messages as "EasyMock for class java.util.ResourceBundle", so the mock is injected correctly. (Also, there's no error on the call to getString() within the expect() call during set up).

使用普通的模拟而不是漂亮的模拟,会出现以下错误:

With a plain mock instead of a nice mock, I get the following error:

java.lang.AssertionError: 
  Unexpected method call handleGetObject("Queue"): 
    getString("Queue"): expected: 1, actual: 0

知道我在做什么错吗?

谢谢。

推荐答案

您正在使用EasyMock创建实例。相反,在使用静态方法时,必须模拟 class (使用PowerMock)。

You are creating an instance using EasyMock. Instead, when working with static methods, you must mock the class (using PowerMock).

它应该像这样工作(在EasyMock 3.0中进行测试)和PowerMock 1.5,不过):

It should work like that (tested with EasyMock 3.0 and PowerMock 1.5, though):

@RunWith(PowerMockRunner.class)
@PrepareForTest(ResourceBundle.class) 
public class TestSuite {
    @Before
    public void setUp() throws Exception {
        // mock the class for one method only
        PowerMock.mockStaticNice(ResourceBundle.class, "getString");

        // define mock-behaviour on the class, when calling the static method
        expect(ResourceBundle.getString(BundleConstants.QUEUE)).andReturn("Queue");

        // start the engine
        PowerMock.replayAll();
    }
}

(我知道这个问题是几个月旧的,但可能对其他人有帮助)

(I'm aware this question is a few months old, but it might help others, though)

这篇关于使用PowerMock + EasyMock模拟最终方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆