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
Assertions on parameters during verification #2285
Comments
I also like the idea about removing this But it seems there is some limitation about this, see : Ideally, I think it would be great if Mockito provide an elegant way to use AssertJ to match arguments and/or more generally provide a way to assert arguments instead of matching it. Waiting, you could implement your own : public static <T> T assertArg(Consumer<T> assertion) {
return argThat(argument -> {
assertion.accept(argument);
return true;
});
} The AssertJ oriented (depend of AssertJ and unfortunately Hamcrest too... - source : https://gist.github.com/superbob/091489c61ce9bd79d14a4d3d731c9c49?permalink_comment_id=3863242#gistcomment-3863242) import org.assertj.core.matcher.AssertionMatcher;
import org.mockito.hamcrest.MockitoHamcrest;
import java.util.function.Consumer;
public class TestUtil {
public static <T> T assertArg(Consumer<T> assertions) {
return MockitoHamcrest.argThat(new AssertionMatcher<T>() {
@Override
public void assertion(T actual) throws AssertionError {
assertions.accept(actual);
}
});
}
} I didn't dig to understand what is the benefits of AssertJ oriented solution compare to general way ... maybe better miss match report ? 🤷 |
Suggestion
I would like to suggest adding an API allowing for assertions during parameter verification without argument captors in simple cases.
Motivation
In my projects I generally use Mockito and AssertJ for testing. For non trivial method parameters, I often encounter the following ArgumentCaptor pattern:
This is fine, but a little bit verbose.
An alternative pattern I find myself using lately is using the
argThat
argument matcher to eliminate the captor, resulting in the verify statement looking something like this:In my opinion, at least in simple cases where only assertions on a single parameter are desired, this looks a little cleaner. Just the return statement is a bit pointless.
My suggestion is adding an API that would allow eliminating the return statement in this pattern, for example:
Or is there already an API that would allow a similar pattern that I've missed?
The text was updated successfully, but these errors were encountered: