You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I know there are several ways how to overcome it (assign assertWithMessage(errorMessage).that(str) to variable and call assertions for it or by using regular expressions), but sequential invocation of assert methods over one object seems more natural to me
Please guide me, if such functionality already exists
The text was updated successfully, but these errors were encountered:
We don't provide an easier way to do this. We should write about why in the FAQ.
Most of the reason is that we were concerned about confusion between this sort of "chaining" and the sort of "chaining" that we provide with assertions like assertThat(exception).hasCauseThat().isInstanceOf(FooException.class). For example:
assertThat(someList)
.containsExactly(a, b)
.inOrder();
Is that an assertion that the list contains a and b, with a appearing before b? Or is it an assertion that the list contains a and b and that the entire list (which may contain more elements) is in lexicographical order? (I grant the actual answer is relatively obvious in this case, so I should come up with a better example :))
Another contributing factor is that this approach is only reliably possible if most Subject subclasses declare a "self type" type parameter. We found that people who wrote Truth extensions rarely did this, so chaining did not work in some cases. And in the cases in which people did declare self-type type parameters, they needed additional abstract classes with type parameters (and sometimes that's in combination with more type paramters) to make all the generics work out right.
(Kotlin provides a syntax that makes this work better, and we may try to make it work still better there: #572)
I'm going to re-purpose this issue to about about adding a FAQ entry.
cpovirk
changed the title
Combine multiple assertions over one object
FAQ entry: Why we don't support multiple chained calls to assertion methods on the same Subject
Jun 10, 2021
(That FAQ entry can also mention the specific approach you discuss in the PR you cross-linked -- using regex instead of startsWith+contains+endsWith. We might have other advice to give, too, like not necessarily worrying about calling isNotNull() before dereferencing a possibly null object.)
I expect everyone to want this at one time or another.
But our equation is basically: a moderate API convenience isn't worth even the smallest risk of user confusion (especially user confusion that can lead to false-negative assertions!).
(If we mention the regex workaround we should note that that switch could easily be a step backward in error message usefulness.)
Hi
I have the following assertions:
As you can see I make several assertions over one
str
objectIt would be easier to understand if all assertions were combined together like:
I know there are several ways how to overcome it (assign
assertWithMessage(errorMessage).that(str)
to variable and call assertions for it or by using regular expressions), but sequential invocation of assert methods over one object seems more natural to mePlease guide me, if such functionality already exists
The text was updated successfully, but these errors were encountered: