truth: Policy describing the difference between "check failed" and "could not evaluate check"

assertThat(myList).containsExactly("foo", "bar");

If myList is null, we throw NullPointerException. This is different than calling fail() – and not just because NullPointerException is different than AssertionError. The more important difference is when the user uses or assume() rather than assert(). In that case, the difference is between a failing test and a skipped test. (A less severe case but still problematic case is expect().)

I think that we’re doing the right thing in throwing NullPointerException: “Does null contain "foo"?” is best answered not with “No, it doesn’t” but rather with: “The question you’re asking is malformed. Please fix it.” But we should officially Decide this and then document it.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 16 (16 by maintainers)

Commits related to this issue

Most upvoted comments

As seen above, this has come up over the years, and I’m now pretty well convinced that there’s not a meaningful distinction to be made. Yeah, you can imagine cases in which it would be useful for assume. But more often, we make things worse for users, as discussed in posts above and elsewhere:

  • It replaces useful failure messages with exception messages that are often worse [edit: and also without stack-trace cleaning].
  • That in turn encourages users to perform “redundant” assertions like assertThat(millis).isNotNull(); assertThat(millis).isGreaterThan(1000); so that they get the better message in the null case.
  • When users use Expect, this policy still produces exceptions that immediately fail tests, rather than allowing us to collect multiple failures.
    • Similarly(?), this policy can produce failures on a Subject that came from ignoreCheck. We generally don’t want to report anything else after ignoreCheck.

As already noted, we are inconsistent. One particular thing I notice is that we already have cases in which both assertThat(x).foo(y) and assertThat(x).notFoo(y) fail, as in StringSubject.

I think the best remaining argument against changing course here is just that it requires some amount of work. But I want to at least give it a try. If it gets to be too much, then I’ll keep the improvements I’ve made so far and save the rest for later. (If nothing else, I don’t expect to go to much effort for null expected values (or otherwise bogus values). Conveniently, some of those can’t be null (like in isWithin(...).of(double))—though maybe it would occasionally be nice for Kotlin users if they could be. But if someone writes assertThat(string).contains(null), I’m relatively less interested in providing a “proper” failure message there.)

Happy to peek at it if there’s nothing that’s inappropriate for external consumption.

I’ve been out of this game for quite some time, but I think the most compelling argument for me to make the change is your point about Expect failing on exceptions, rather than gathering the failure. I am of two minds, having originally felt quite strongly about the distinction. But it isn’t the case that all runners will honor the distinction, and the purpose, to be honest, was to get an IDE or CI report to show the distinction, to ease discovering errors, identifying flakes, etc. I no longer am so convinced that this is worth the trade-off, especially if it breaks Expect on a fairly normal case of an NPE.

So I’m in support of the attempt to change the policy, though I know I have, at best, an emeritus voice here. 😃