javajunittestngassertionsfest

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

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

@Override
public String toString() {      
    return "string matches or is equal to : " + expectedStringOrExpression;
}

but that does not seem to work.


Solution

  • You want to set the description, which can be done by calling the Condition(String) constructor:

    public StringMatchesOrIsEqualTo(final String expectedStringorExpression){
        super("A String that matches, or is equal to, '" + expectedStringorExpression "'");
        this.expectedStringOrExpression = expectedStringorExpression;
    }
    

    Alternatively, you could override description():

    @Override
    public String description()
    {
        return "A String that matches, or is equal to, '" + expectedStringorExpression "'");
    }