I've tried many method, but I still cannot see the description inside matchesSafely function. And I've tried to research any related articles but there are still few articles that discuss it.
For make it simple, lets take an example of WithHintMatcher class on androidx.test.espresso.matcher like shown below:
@VisibleForTesting
static final class WithHintMatcher extends BoundedDiagnosingMatcher<View, TextView> {
@RemoteMsgField(order = 0)
private final Matcher<String> stringMatcher;
@RemoteMsgConstructor
private WithHintMatcher(Matcher<String> stringMatcher) {
super(TextView.class);
this.stringMatcher = stringMatcher;
}
@Override
protected void describeMoreTo(Description description) {
description.appendText("view.getHint() matching: ");
stringMatcher.describeTo(description);
}
@Override
protected boolean matchesSafely(TextView textView, Description mismatchDescription) {
CharSequence hint = textView.getHint();
mismatchDescription.appendText("view.getHint() was ").appendValue(hint);
return stringMatcher.matches(hint);
}
}
Explanation
As we can see, there is a mismatchDescription parameter on matchesSafely function (mismatchDescription.appendText("view.getHint() was ").appendValue(hint);).
How do I get that description in the test logs? "view.getHint() was ...", because the logs only shows the description that we defined at describeMoreTo function.