【问题标题】:PowerMock MockStatic expect not working for EasyMock anyObjectPowerMock MockStatic 预计不适用于 EasyMock anyObject
【发布时间】:2014-02-21 00:57:12
【问题描述】:

这看起来不错

EasyMock.expect(URLDecoder.decode("test", "UTF-8")).andThrow(new UnsupportedEncodingException("This is a test"));

这不是

EasyMock.expect(URLDecoder.decode((String) EasyMock.anyObject(), "UTF-8")).andThrow(new UnsupportedEncodingException("This is a test"));

这会引发以下情况

java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
This exception usually occurs when matchers are mixed with raw values when recording a  method:
foo(5, eq(6));  // wrong
You need to use no matcher at all or a matcher for every single param:
foo(eq(5), eq(6));  // right
foo(5, 6);  // also right
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:47)
at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:40)
at org.easymock.internal.RecordState.invoke(RecordState.java:78)
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:40)
at 

【问题讨论】:

    标签: easymock powermock


    【解决方案1】:

    如果您打算使用 1 个匹配器,那么您需要使用所有匹配器。

    遗憾的是,这在您从 EasyMock 得到的错误消息中没有得到很好的解释,但是在另一个字符串周围添加一个 EasyMock.eq() 应该可以解决问题。

    EasyMock.expect(URLDecoder.decode((String) EasyMock.anyObject(), EasyMock.eq("UTF-8"))).andThrow(new UnsupportedEncodingException("This is a test"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多