PowerMock - IllegalStateException:必须重播类XXX才能获得配置的期望 [英] PowerMock - IllegalStateException: Must replay Class XXX to get configured expectation

查看:145
本文介绍了PowerMock - IllegalStateException:必须重播类XXX才能获得配置的期望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@PrepareForTest({...,..., XXX.class})
...
@Test
public void testMethodToBeTested(){
    XXX mockInstance = PowerMock.createMock(XXX.class); 
    ...
    PowerMock.expectNew(XXX.class, p1, p2, p3, p4, p5).andReturn(mockInstance);
    mockInstance.methodWithNoReturnVal();
    expect(mockInstance.getSearchVal()).andReturn(1);
    PowerMock.replay(mockInstance);
    Whitebox.invokeMethod(objInstance, "methodToBeTested");

    PowerMock.verify(mockInstance);
}

基本上,我想测试私有方法:methodToBeTested()。此方法实例化XXX.class类型的对象(使用new运算符)并将其存储到实例变量中;我嘲笑这个对象。然后,它通过mock调用methodWithNoReturnVal(),然后调用我配置为返回1的getter方法。

Essentially, I want to test a private method: methodToBeTested(). This method instantiates an object (using the new operator) of type XXX.class and stores it into an instance variable; I mocked this object. Then, it calls methodWithNoReturnVal() via the mock, and then a getter method which I configured to return 1.

我收到错误IllegalStateException:必须重播类XXX。获得配置期望的类。

I get the error "IllegalStateException: Must replay class XXX.class to get configured expectation."

感谢任何帮助。我还是PowerMock和EasyMock的新手;因为我需要使用expectNew(...)方法,所以我确保在测试方法中使用所有PowerMock api。

Any help is appreciated. I am still new to PowerMock and EasyMock; since I needed to use the expectNew(...) method, I made sure to use all PowerMock api inside of the test method.

推荐答案

你需要一个 PowerMock.replay(XXX.class); 在Powermock的方法中拦截对象的构造。见下文。

You need to have a PowerMock.replay(XXX.class); in the method for Powermock to intercept the construction of the object. See below.

@PrepareForTest({...,..., XXX.class})
...
@Test
public void testMethodToBeTested(){
    XXX mockInstance = PowerMock.createMock(XXX.class); 
    ...
    PowerMock.expectNew(XXX.class, p1, p2, p3, p4, p5).andReturn(mockInstance);
    mockInstance.methodWithNoReturnVal();
    expect(mockInstance.getSearchVal()).andReturn(1);
    PowerMock.replay(mockInstance, XXX.class);
    Whitebox.invokeMethod(objInstance, "methodToBeTested");

    PowerMock.verify(mockInstance);
}

这篇关于PowerMock - IllegalStateException:必须重播类XXX才能获得配置的期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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