[英]Custom FEST Assertions : Displaying readable message with
I have created a custom FEST Condition to verify that my actual string either matches or is equal to an expected String 我创建了一个自定义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);
}
}
Whenever the conditon fails i want it to display a message that shows me what the original and expected String was 每当条件失败时,我希望它显示一条消息,向我显示原始字符串和预期字符串是什么
currently the display string is 当前显示字符串是
actual value:<'Some String'> should satisfy condition:<StringMatchesOrIsEqualTo>
is there a way that this message also displays what the match is made against ? 有没有办法使此消息也显示与之匹配的内容?
I tried overriding the toString method in the class 我尝试覆盖类中的toString方法
@Override
public String toString() {
return "string matches or is equal to : " + expectedStringOrExpression;
}
but that does not seem to work. 但这似乎不起作用。
You want to set the description
, which can be done by calling the Condition(String)
constructor: 您想要设置description
,这可以通过调用Condition(String)
构造函数来完成:
public StringMatchesOrIsEqualTo(final String expectedStringorExpression){
super("A String that matches, or is equal to, '" + expectedStringorExpression "'");
this.expectedStringOrExpression = expectedStringorExpression;
}
Alternatively, you could override description()
: 或者,您可以覆盖description()
:
@Override
public String description()
{
return "A String that matches, or is equal to, '" + expectedStringorExpression "'");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.