>我创建了一个自定义 FEST 条件来验证我的实际字符串是否与预期的字符串匹配或等于
public class StringMatchesOrIsEqualTo extends Condition<String>{
private String expectedStringOrExpression;
public StringMatchesOrIsEqualTo(final String expectedStringorExpression){
this.expectedStringOrExpression = expectedStringorExpression;
}
@Override
public boolean matches(String value) {
return value.matches(expectedStringOrExpression) || value.equals(expectedStringOrExpression);
}
}
每当条件失败时,我希望它显示一条消息,向我显示原始字符串和预期的字符串是什么
当前显示字符串为
actual value:<'Some String'> should satisfy condition:<StringMatchesOrIsEqualTo>
有没有办法让此消息也显示匹配对象?
我尝试重写类中的 toString 方法
@Override
public String toString() {
return "string matches or is equal to : " + expectedStringOrExpression;
}
但这似乎行不通。
你想设置description
,这可以通过调用Condition(String)
构造函数来完成:
public StringMatchesOrIsEqualTo(final String expectedStringorExpression){
super("A String that matches, or is equal to, '" + expectedStringorExpression "'");
this.expectedStringOrExpression = expectedStringorExpression;
}
或者,您可以覆盖description()
:
@Override
public String description()
{
return "A String that matches, or is equal to, '" + expectedStringorExpression "'");
}