JMockit模拟静态私有调用

时间:2014-07-20 13:51:04

标签: java-ee jmockit

HOW TO MOCK getProperty(string)方法,引用是使用JMockit的私有静态字段.........

[...]
private static LWPropertyResource props =  null;
props = LWSupportFactoryImpl.getInstance().getPropertyResource(VALIDATE_HANDLER_PROPS);
        ,
        endDate = props.getProperty("endDate");

[...]

1 个答案:

答案 0 :(得分:2)

声明所需类型的模拟字段或模拟参数,并记录您需要的期望:

@Test
public someTest(@Mocked final LWPropertyResource res)
{
    final Date endDate = new Date();

    new NonStrictExpectations() {{
        res.getProperty("endDate"); result = endDate;
    }};

    // Call the code under test.
}