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
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for null expected `Optional` values. RELNOTES=n/a PiperOrigin-RevId: 769213237 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for null expected `Optional` values. RELNOTES=n/a PiperOrigin-RevId: 769602408 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(nullString).hasLength(...)`. Commentary: - I haven't addressed *all* the places where `StringSubject` will ... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(nullString).hasLength(...)`. Commentary: - I haven't addressed *all* the places where `StringSubject` will ... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(nullClass).isAssignableTo(...)`. I could see an argument that we might want to be more explicit here that th... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(nullClass).isAssignableTo(...)`. I could see an argument that we might want to be more explicit here that th... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(hasNoCause).hasCauseThat().hasMessageThat()`, and try to improve messages for null values. This resolves [an... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(hasNoCause).hasCauseThat().hasMessageThat()`, and try to improve messages for null values. This resolves [an... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for null `Comparable` and `Range` values. RELNOTES=n/a PiperOrigin-RevId: 769226817 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for null `Comparable` and `Range` values. RELNOTES=n/a PiperOrigin-RevId: 770167029 — committed to google/truth by cpovirk a year ago
- Improve some numeric failure cases: - [Fail instead of throwing an exception](https://github.com/google/truth/issues/207) for null actual values and invalid tolerances. - Clarify failure messages aro... — committed to google/truth by cpovirk a year ago
- Improve some numeric failure cases: - [Fail instead of throwing an exception](https://github.com/google/truth/issues/207) for null actual values and invalid tolerances. - Clarify failure messages aro... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(nullObjectArray).asList()....`. RELNOTES=n/a PiperOrigin-RevId: 769287161 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing NPE](https://github.com/google/truth/issues/207) for `assertThat(nullObjectArray).asList()....`. RELNOTES=n/a PiperOrigin-RevId: 770203364 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing](https://github.com/google/truth/issues/207) for null arrays and negative lengths. RELNOTES=n/a PiperOrigin-RevId: 770295848 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing](https://github.com/google/truth/issues/207) for null arrays and negative lengths. RELNOTES=n/a PiperOrigin-RevId: 770295848 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing](https://github.com/google/truth/issues/207) for null arrays and negative lengths. RELNOTES=n/a PiperOrigin-RevId: 770295848 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing](https://github.com/google/truth/issues/207) for null arrays and negative lengths. RELNOTES=n/a PiperOrigin-RevId: 770591220 — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing](https://github.com/google/truth/issues/207) for null arrays. (like cl/770203364 but for primitive arrays) Also, convert a couple more inner classes into `static` classes, ... — committed to google/truth by cpovirk a year ago
- [Fail instead of throwing](https://github.com/google/truth/issues/207) for null arrays. (like cl/770203364 but for primitive arrays) Also, convert a couple more inner classes into `static` classes, ... — committed to google/truth by cpovirk a year ago
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:assertThat(millis).isNotNull(); assertThat(millis).isGreaterThan(1000);so that they get the better message in the null case.Expect, this policy still produces exceptions that immediately fail tests, rather than allowing us to collect multiple failures.Subjectthat came fromignoreCheck. We generally don’t want to report anything else afterignoreCheck.As already noted, we are inconsistent. One particular thing I notice is that we already have cases in which both
assertThat(x).foo(y)andassertThat(x).notFoo(y)fail, as inStringSubject.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 writesassertThat(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. 😃