-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document benefits of messageSupplier
in Assertions
#3153
Comments
Team decision: Change the documentation to contain a more meaningful example. |
@manueljordan Would you like to take a stab at this? |
@marcphilipp If I am able to do it, yes. But what could be the most adequate scenario to add it as an example? Who was the author of the addition of this "lazy approach"? He/She created that feature to solve/improve a situation. Having that info in hands would be a good starting point. At https://github.com/junit-team/junit5-samples, a sample would be nice to be added, but under what directory? BTW Does the current documentation include references to https://github.com/junit-team/junit5-samples to go deep, through those examples, to offer more details about any topic being covered? It for the developer, if he/she wants get more details. Thanks for your understanding |
It's the same rationale as in
I don't think this minor feature justifies a sample in junit5-samples. Improving the snippet in the User Guide should be enough. |
I think the best approach is add something like:
Where:
And indicate that:
In this case is easily confirmed when Therefore the current example is not clear because instead to do a method call is established directly a String message instead. |
Here, "Assertion messages can be lazily evaluated", means that the message is not evaluated immediately when the assertion is made, if failed, the message will be printed.
Lazily evaluating the assertion message can provide a performance benefit, especially if the message construction is complex or time-consuming, as it is only evaluated when the assertion fails. In the example provided in the JUnit 5 documentation, the assertion message is a concatenation of multiple strings, which could be time-consuming to construct. By using a lambda expression to evaluate the message lazily, the string concatenation is only performed if the assertion fails. However, it is worth noting that in cases where the assertion message is short and simple, there may be little to no performance benefit in using a lambda expression for lazily evaluating the message. |
@aliniko, your explanation would work nicely. @manueljordan, invoking another method in the lambda expression is much better than the simple string concatenation example we currently have. So, if I were to rewrite the example, I would:
Anyone interested in submitting a PR which does that? |
I think is wise keep the current example and add the other one, the lambda method approach, it to show the clear difference with two examples. I would do this PR - but based on both explanations together, from Aliniko and my explanation (about "1" and "2") - Are you agree Sam? |
Hello Is this issue open to be taken? I'd like to contribute to this project. Thank you! |
@sbrannen @manueljordan We can also write the tests as below to emphasize the fact that the 3rd parameter can be a plain expression or a lambda expression.
@Test
void standardAssertions() {
assertTrue('a' < 'b', "This expression will always evaluate irrespective of assertion result." + getMessage('a', 'b'));
assertTrue('a' < 'b', () -> "This lambda will only evaluate if assertion fails." + getMessage('a', 'b'));
}
private String getMessage(char p1, char p2) {
// It can be a complex operation, and must be evaluated, only when assertion fails, to avoid unncessary computation.
return String.format("ASCII value of '%c' (%d) is greater than '%c' (%d) ", p1, (int)p1, p2, (int)p2));
} |
@ankitwasankar, since no one else has submitted a PR yet, feel free to submit a PR based on your above proposal. |
messageSupplier
in Assertions
messageSupplier
in Assertions
messageSupplier
in Assertions
Hey @sbrannen looks like this PR is still open, id be happy to finish it off and get it over the line. Although at this point having read the thread a few times im unclear what you would like to be done as I think its pretty clear? If you let me know i will update the PR! :) This would be my first open source contribution so excited to start my journey here! |
About JUnit 5 documentation in the 2.5. Assertions section, exists the following
@Test
methodIs not clear what is the advantage to use
() -> "Assertion messages can be lazily evaluated -- ..."
and not simply"Assertion messages can be lazily evaluated -- ..."
It means:() -> ""
vs""
Therefore:
What does Assertion messages can be lazily evaluated mean? The explanation would be valuable for a better understanding and to know explicitly when use mandatorily an approach over the other. Currently in the complete/full documentation only appears through a sample code the
() -> "something"
approach just twice, but not more. Some example(s) is/are appreciate showing the two approaches together and to understand clearly the difference of use of one over the other. Therefore I want to know when would be mandatory use the lambda approach. So far, the current official documentation does not cover this explicitlyI already wrote a question on SO about this question at:
The answer has sense, but if it can be covered and expanded from the official source documentation would be nice for the community
Thanks for your understanding
The text was updated successfully, but these errors were encountered: