Skip to content

Instantly share code, notes, and snippets.

@avmohan
Created June 10, 2023 09:12
Show Gist options
  • Save avmohan/63aef344037af4f2a09344e9428b4a29 to your computer and use it in GitHub Desktop.
Save avmohan/63aef344037af4f2a09344e9428b4a29 to your computer and use it in GitHub Desktop.
Mockito argument matcher that satisfies assertj assertions
import java.util.function.Consumer;
import org.assertj.core.matcher.AssertionMatcher;
import org.mockito.hamcrest.MockitoHamcrest;
public class CustomMatchers {
private CustomMatchers() {}
public static <T> T argSatisfying(Consumer<T> assertions) {
return MockitoHamcrest.argThat(
new AssertionMatcher<T>() {
public void assertion(T actual) throws AssertionError {
assertions.accept(actual);
}
});
}
}
// verify(aMock).aMethod(argThat(actual -> assertThat(actual).containsExactlyInAnyOrder(aValue, anotherValue)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment