Skip to content
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

assertArg example in JavaDoc should report multiple failures at once #3199

Open
rkrisztian opened this issue Dec 13, 2023 · 1 comment
Open

Comments

@rkrisztian
Copy link

I've seen that Mockito adds assertArg in version 5.3.0 (#2285):

verify(otherServiceMock).doStuff(assertArg(param -> {
    assertThat(param.getField1()).isEqualTo("value1");
    assertThat(param.getField2()).isEqualTo("value2");
}));

That is nice, I appreciate a lot, but my problem is, and this is often overlooked: we should report multiple failures at once (where assertions are independent of each other). To give a better example we can:

  • Use JUnit's assertAll
  • Use AssertJ's assertSoftly
  • Or use AssertJ's tuple and/or extracting (if lists are involved)

Example:

verify(otherServiceMock).doStuff(assertArg(param -> {
    assertThat(​tuple(param.getField1(), param.getField2())​)
            .isEqualTo(​tuple("value1", "value2"));
}));

Or whatever code style you find more readable.

Sadly, several frameworks/libraries get it wrong as well:

  • REST Assured
  • Spring MockMvcTest before adding andExpectAll in Spring Framework v5.3.10
  • Several JavaScript/TypeScript testing tools
  • (there could be more...)
@scordio
Copy link
Contributor

scordio commented Mar 24, 2024

Mockito has no way to catch multiple failures thrown by the Consumer of assertArg. If such behavior is desired, the writer of the Consumer should take care of it, e.g.:

verify(otherServiceMock).doStuff(assertArg(param -> 
    assertThat(param).satisfies(
        p -> assertThat(p.getField1()).isEqualTo("value1"),
        p -> assertThat(p.getField2()).isEqualTo("value2")
)));

(manually written, beware of typos)

To support this use case, Mockito could add a new assertArg(Consumer...) which behaves like AssertJ satisfies and would allow to write something like:

verify(otherServiceMock).doStuff(assertArg(
    param -> assertThat(param.getField1()).isEqualTo("value1"),
    param -> assertThat(param.getField2()).isEqualTo("value2")
));

Happy to raise a PR if the Mockito team likes the idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants