Custom Fail Message #351
Replies: 13 comments
-
I'll take this |
Beta Was this translation helpful? Give feedback.
-
Sure! See linked issue for what api I was thinking, let me know if you want to discuss it. |
Beta Was this translation helpful? Give feedback.
-
Would it be implemented? |
Beta Was this translation helpful? Give feedback.
-
If anyone's wondering, the reason this hasn't been implemented is there's some design questions around it like:
assertThat(person).prop(Person::name).isEqualTo("Alice")
// [name] expected:<["Alice"]> but was:<["Bob"]> (Person(name=Alice))
assertThat(person, message = "custom message")
assertThat(person, message = { "custom message" })
assertThat(person, message = { actual -> "custom message ${show(actual.name)}" }) So I'm wondering what people's actual use cases are. In other words, what are some examples where you feel you need a custom message over the default one (that can't be solved better by improving the default one or creating a custom assertion)? |
Beta Was this translation helpful? Give feedback.
-
For me, I'd say the places where I'm tempted to use this are often places where a custom assertion would work as an alternative. It's usually where the assertion is "in the weeds" and lacks the context necessary to understand the problem. For instance, I wrote a test recently that for each plan code in an insurance domain, there should be cash value metadata for a given issue age, plan duration, and gender. By the time I work my way through all those factors, if I discover the plan isn't there, just getting a message that the object is null is not very useful. I want an error message that says that there's no metadata for a given plan code, issue age, plan duration and gender. In this case, I ended up finding it simpler to just write classic JUnit assertions with a custom failure message, but I probably could have made a custom assertion. I'm tempted to go back and try just to compare the two paths. A custom assertion feels like more work, but that's probably in part because I've only made one a few times, so it doesn't come naturally to me yet. |
Beta Was this translation helpful? Give feedback.
-
Since assertk was inspired by assertj, might sa well copy what assertj has
https://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html |
Beta Was this translation helpful? Give feedback.
-
Assertion messages usually dont get much love. That's because if your tests pass, then you dont see the value of assertion messages. But if your tests fails, that's where you get the value. And it's hard to tell what the error is if you have multiple assertions and you dont have custom assertion messages. Without custom assertion messages, devs starts putting break points on their tests. |
Beta Was this translation helpful? Give feedback.
-
A custom description is already handled by the assertThat(frodo.age, name = "check ${frodo.name}'s age").isEqualTo(100)
I believe this issue is asking about replacing the message part. |
Beta Was this translation helpful? Give feedback.
-
Great! thanks @evant 😄 Hope that can be added in the README/docs 😄 |
Beta Was this translation helpful? Give feedback.
-
I was specifically looking into the assertThat(success).withFailMessage { "DB state: " + repository.findUser(USER_ID) }.hasSize(1) For now, I'll keep using assertJ for this use case. |
Beta Was this translation helpful? Give feedback.
-
hm, would it be useful to allow appending additional state to an assertion? Something like:
|
Beta Was this translation helpful? Give feedback.
-
yes, that would help a lot! also, another benefit of lambda is that it is evaluated after the assertion, not before. So if assertion changes state, that new state would be printed, not the previous one. |
Beta Was this translation helpful? Give feedback.
-
Cool, created a new issue to track this specifically #344. |
Beta Was this translation helpful? Give feedback.
-
It would be nice to have a
.withFailMessage(...)
method to be able to customize failures.Beta Was this translation helpful? Give feedback.
All reactions