自定义FEST断言:显示可读消息

时间:2015-06-13 22:47:36

标签: java junit testng assertions fest

我创建了一个自定义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;
}

但这似乎不起作用。

1 个答案:

答案 0 :(得分:2)

您想要设置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 "'");
}