I've been playing around with AutoCloseableSoftAssertions, as I don't want to have to remind myself to call .assertAll()
But I also wanted to see if I can replace entire rows of assertThat statements in a simple manner, without many changes.
And I think I have found a way in Kotlin.
Let's start with the basics:
Let's try some SoftAssertions, so we can have many asserts that are all evaluated, instead of the Test stopping upon the first failed assert.
Okay, now AutoCloseableSoftAssertions is a convenient class that calls the .assertAll() as part of the final block in a try-with-resources:
Now, in the case above, we have to prefix our assertThat() calls with "it.". There's one more step that we can use to remove this.
Wrapping all this in a function for easy use will end up in:
Now, a stack of assertThat() statements can be encompassed with assertAll{} without any other changes to get the full advantages of SoftAssertions.
Might be overkill, but this was a fun example in Kotlin.
Try-with-resources
So you might have noticed that AutoClosableSoftAssertions is used in a try-with-resources block.
The small advantage you have, is that if there is an unexpected Exception thrown during your asserts, the ".assertAll()" will still be called, because it's part of the "AutoCloseable.close()".
I don't know how much this advantage is, as the unexpected Exception breaks the test anyways, but there it is.