Assertj-core: lazy withFailMessage / overridingErrorMessage

Created on 16 Apr 2020  路  10Comments  路  Source: assertj/assertj-core

Summary

junit provide a lazy fail message description by using a supplier. The supplier is called only if an error occurs, otherwise it is not called.

        Assertions.assertTrue(true, () -> "I won't be called");
        Assertions.assertTrue(false, () -> "I will be called");

It is useful for at least two reason :

  • performance optimization if computing the custom error message is costly.
  • getting error message may not be possible if there is not an error, however assertThat try to get the error message even if there is no error.

Suggestion, add the same capability to withFailMEssage or overridingErrorMessage, example :

        assertThat(response.isError())
            .withFailMessage(() -> "my error")
            .isFalse();

Most helpful comment

Unfortunately it is proprietary software, but I will try to provide equivalent test. ETA next week.

All 10 comments

Replacing withFailMessage() with as() cut time in one of mine test from 4 minutes 34 seconds to 46 seconds.

I'm ok adding this because it can be useful in some cases.
@Mincol just pointing out that as() and withFailMessage() have different purposes.

For others and @joel-costigliola it does. But I wanted to point out how much real world usage of withFailMessage() can be costly. My usage ofas() is not using lambdas, yet it saved nearly 4 minutes.

So time gain must be in eager call to format within withFailMessage()

Can I work on it锛烮 think it's a good idea to improve the performance in some cases. We can discuss further.

Would be great, thanks @Eveneko.

@Mincol Can you provide your test cases? Thank you very much!

Unfortunately it is proprietary software, but I will try to provide equivalent test. ETA next week.

@Mincol Thx

Starting work on this.

@Mincol please sync with @Eveneko who also wanted to work on this. You can do together if you want!

Was this page helpful?
0 / 5 - 0 ratings