简体   繁体   English

Easymock匹配器的返回值

[英]Easymock matcher on return value

foo.toString() returns foo.toString()返回

"[id: <id>, stuffIdontCareAbout: 0]"

I want to test that the id is correct, but everything else in the string can have any value. 我想测试id是否正确,但是字符串中的其他所有值都可以具有任何值。 This is what I tried: 这是我尝试的:

expect(foo.toString()).andReturn(EasyMock.find("[id: 42,"));

but here's how it complained: 但是这是它抱怨的方式:

java.lang.IllegalStateException: 0 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

So, how do I use an EasyMock matcher on a return value? 那么,如何在返回值上使用EasyMock匹配器?

This doesn't sound like a mock. 这听起来不像是一个模拟。 It seems you only want to test the result of foo.toString() . 看来您只想测试foo.toString()的结果。

Using AssertJ (which I recommend), would mean doing 使用AssertJ(建议使用)意味着

assertThat(foo.toString()).startsWith("[id: 42,");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM