【问题标题】:In Powermock, how do I express that I expect a static void method to be called?在 Powermock 中,我如何表达我希望调用静态 void 方法?
【发布时间】:2015-11-06 16:44:49
【问题描述】:

我正在使用 PowerMock v 1.6.2 和 JUnit 4.11。如何表达我希望调用静态 void 方法?我有以下

import static org.easymock.EasyMock.expect;
import static org.powermock.api.easymock.PowerMock.mockStatic;
…
    mockStatic(SecurityContextHolder.class);
    expect(SecurityContextHolder.getContext()).andReturn(securityContext);
    expect(SecurityContextHolder.clearContext());
   replay(SecurityContextHolder.class);

但是第二个“expect”行给出了编译错误,“EasyMock 类型中的方法 expect(T) 不适用于参数 (void)”。如何表达我希望调用静态 void 方法?

【问题讨论】:

  • 对于你需要使用的void 方法,expectLastCall(.....) 而不是'expect'

标签: junit static mocking void powermock


【解决方案1】:

Void 方法没有返回值,因为它们不返回任何值。试试 expectLastCall()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2015-05-30
    • 2019-07-26
    • 1970-01-01
    • 2011-11-20
    • 2018-10-22
    相关资源
    最近更新 更多