开发者

Why does EasyMock's IArgumentMatcher interface use a StringBuffer?

开发者 https://www.devze.com 2023-03-11 06:03 出处:网络
EasyMock allows you to create your own matchers so that you can specify what a mock should return for certain inputs. To do this, you create a custom implementation of their IArgumentMatcher interface

EasyMock allows you to create your own matchers so that you can specify what a mock should return for certain inputs. To do this, you create a custom implementation of their IArgumentMatcher interface.

This interface has two methods:

boolean matches(Object argument);
void appendTo(StringBuffer buffer) 

The appendTo() method is used for printing a human-readable message if a match fails. Why does it ask you to append the message to a StringBuffer and not to simply return a String? Why not have the following开发者_如何学C method on the interface instead?

String message();


StringBuffer here is actually a good choice. If you are using multiple matchers in a given situation, the use of StringBuffer allows all of the messages to be consolidated and retrieved a single time.

If you used the void message(); approach, you would have to call that for each matcher that you were concerned about (which is obviously sub-optimal).

0

精彩评论

暂无评论...
验证码 换一张
取 消