Skip to content

Assert that exception message contains one of multiple strings #2376

Answered by scordio
sebthom asked this question in Q&A
Discussion options

You must be logged in to vote

hasMessageMatching might be an alternative for some cases. Not the best readability, though.

Another option that can be used is extracting(Function, InstanceOfAssertFactory):

assertThatThrownBy(() -> {
  /* code to test */
})
  .isInstanceOf(CertPathValidatorException.class)
  .extracting(Throwable::getMessage, as(InstanceOfAssertFactories.STRING))
  .containsAnyOf(
    "could not be determined",
    "could not determine revocation status",
    "revocation status check failed"
  );

We could add a navigational method to get the message as a string assertion, that would shorten a bit the extracting line. It would allow writing something like:

assertThatThrownBy(() -> {
  /* code to test */

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@sebthom
Comment options

@scordio
Comment options

Answer selected by sebthom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants