简体   繁体   中英

How can I override default failure message of a test in google.Truth?

I am writing a test that assert that a document does not contain a specific String. When the test fails, it prints the 'actual' value in the form

expected not to contain a match for: my_regex
but was                            : a huge document that is unreadable

The document is very long. It would be preferable to not print it and just print the name of the document. I tried assertWithMessage() but it only adds a message, not replace the default one.

Sorry, we've considered providing this feature occasionally but not pulled the trigger.

For starters, it would often make the assertion statement longer than writing the check yourself. Compare:

assertThat(doc.matches(".*the-regex.*")).isTrue();

assertThat(doc).displayedAs("the doc").containsMatch("the-regex");

(To be fair, there are cases in which it's not so easy to write the check yourself.)

And anyway, much of the goal of Truth is to produce informative failure messages. In cases in which people have good reasons to leave that information out, they can fall back to isTrue() assertions.

(To be fair again, the isTrue() failure produces basically no useful message, whereas you'd like to have "expected not to contain a match for: my_regex." You can of course add it back with assertWithMessage , as you've said, but now your assertion statement is getting long again, and you have to repeat "my_regex" if you want it in the message.)

(Plus, it's nice to be able to always write the assertion in the idiomatic Truth way, rather than switching to non-idiomatic when you want the override the message.)

As noted in all the parentheticals above, though, this feature would have its uses. The "real" concerns are mainly:

  • API size. Consider also that some people want to omit different parts of the message, so they might desire more than one method.
  • People may call this method by mistake, accidentally throwing information away.

There's a related feature request here, which is for Truth to truncate values after a certain length. We've actually gotten feedback complaining about cases in which we do truncate, so there's a balance we need to strike here :) But it seems reasonable for us to provide some kind of configurable limit, perhaps based on a system property. I invite you to file an issue (and another for the "override default failure message" thing, if you'd like, even if I suspect we won't do it), though I should warn you that the next quarter or two are probably not going to see a lot of Truth development.

Actually, I forgot: Contrary to what I said in my other answer , there's actually something of a way to do this: Extend StringSubject to override the string representation, and use your custom subject:

public static StringSubject assertThatAbbreviatedString(String actual) {
  return assertAbout(abbreviatedStrings()).that(actual);
}

public static Subject.Factory<StringSubject, String> abbreviatedStrings() {
  return AbbreviatedStringSubject::new;
}

private static final class AbbreviatedStringSubject extends StringSubject {
  AbbreviatedStringSubject(FailureMetadata metadata, String actual) {
    super(metadata, actual);
  }

  @Override
  protected String actualCustomStringRepresentation() {
    return "<actual value omitted>";
    // [Edit: Or maybe you can extract the title from the doc and return that?]
  }
}

That enables you to write:

assertThatAbbreviatedString("abcdefghijklmnopqrstuvwyz").containsMatch("foo");

And the output is something like:

expected to contain a match for: foo
but was                        : <actual value omitted>

If you want to be able to plug in a specific name, rather than <actual value omitted> , the simplest thing is probably to use assertWithMessage(...).about(...).that(...) , which you can again wrap in a helper method. (If assertWithMessage is a poor fit for some reason, there's at least one other approach I could get into.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM