PowerMock MockStatic期望不适用于EasyMock anyObject

时间:2014-02-17 15:27:25

标签: easymock powermock

这似乎没问题

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 

1 个答案:

答案 0 :(得分:0)

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

可悲的是,在从EasyMock中获取的错误消息中没有很好地解释这一点,但是在另一个字符串中添加EasyMock.eq()应该可以解决问题。

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