这似乎没问题
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 个匹配器,那么你需要使用所有匹配器。
可悲的是,这在您从 EasyMock 中得到的错误消息中没有得到很好的解释,但是在另一个字符串周围添加一个EasyMock.eq()
应该可以解决问题。
EasyMock.expect(URLDecoder.decode((String) EasyMock.anyObject(), EasyMock.eq("UTF-8"))).andThrow(new UnsupportedEncodingException("This is a test"));