自定义 FEST 断言:显示可读消息
Custom FEST Assertions : Displaying readable message with
我创建了自定义 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>
有没有办法让这条消息也显示匹配的对象?
我尝试覆盖 class
中的 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 "'");
}
我创建了自定义 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>
有没有办法让这条消息也显示匹配的对象?
我尝试覆盖 class
中的 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 "'");
}