Skip to content

Add static factory methods that return Supplier<String> to format test errors #3841

@shahryarkiani

Description

@shahryarkiani

I think it would be useful for there to be an easy way for users to get different output formats for their tests.

For example, in the case of asserting equality on a collection, each list is outputted horizontally on a single line. That can be hard to compare when the elements are of varying length.

For reference, an example output looks like this:

expected: <[short, a longer string, even longer string that is annoying]> but was: <[i am a small, i am not so small, tiny]>
Expected :[short, a longer string, even longer string that is annoying]
Actual   :[i am a small, i am not so small, tiny]

I think a good solution would be to expose some static factory methods that return a Supplier<String> that formats the output in a desired format, and the user can then pass it in. So a user could do something like:
assertEquals(CollectionA, CollectionB, OutputFormat.ofVertical(CollectionA, CollectionB));
This wouldn't affect any existing user tests/code and would also be flexible to adding new formats. I think it's also not that easy for users to implement the functionality on their own, since getting the spacing right can be tough. A quick Supplier<String> I wrote up looks like this:

expected   |   actual
short | i am a small
a longer string | i am not so small
even longer string that is annoying | tiny

But if this seems like a good idea, I could write it so that we get something like this:

expected                            | actual
---------------------------------------------------
short                               | i am a small
a longer string                     | i am not so small
even longer string that is annoying | tiny

(Would also need to handle stuff like unequal collection sizes, maybe including indexes, etc.)

This output would appear above the existing default output, so it does duplicate some information.

I'm interested in implementing this myself and getting to contribute to JUnit.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions