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

Throwable assertion support for hasMessageNormalizingNewlines(String) #3405

Conversation

armandino
Copy link
Contributor

I propose adding hasMessageNormalizingNewlines(String) method for asserting Throwable messages.

Currently, this can be done using workarounds, e.g.

Throwable ex = catchThrowable(() -> someMethod());
      
assertThat(ex.getMessage()).isEqualToNormalizingNewlines("multiline\nmessage");

Check List:

  • Fixes #??? (ignore if not applicable)
  • Unit tests : YES
  • Javadoc with a code example (on API only) : YES
  • PR meets the contributing guidelines

Following the contributing guidelines will make it easier for us to review and accept your PR.

@joel-costigliola
Copy link
Member

@armandino sorry for the late reply, you already have a way to do that (nicely I think) with message():

assertThat(ex).message().isEqualToNormalizingNewlines("multiline\nmessage");

@armandino
Copy link
Contributor Author

@joel-costigliola thank you for the suggestion. That definitely works. The only downside is that it requires getting an instance of the thrown exception, as in my snippet above.

I usually use assertThatThrownBy:

assertThatThrownBy(() -> someMethod())
    .isInstanceOf(SomeException.class)
    .hasMessage("...");

but this doesn't does not support the following usage:

assertThatThrownBy(() -> someMethod())
    .isInstanceOf(SomeException.class)
    .hasMessageNormalizingNewlines("...");  // would be nice to have

With the introduction of text blocks (https://openjdk.org/jeps/378), hasMessageNormalizingNewlines() would be a useful addition, unless there's another AssertJ APi that supports doing something like this that I'm not aware of.

@joel-costigliola
Copy link
Member

joel-costigliola commented May 5, 2024

@armandino the following snippet works because assertThatThrownBy returns AbstractThrowableAssert which exposes message(), that should work for you too, right ?

assertThatThrownBy(() -> {})
   .isInstanceOf(Exception.class)
   .message()
   .isEqualToNormalizingNewlines("...");

@armandino
Copy link
Contributor Author

@joel-costigliola yes, that does work. I wasn't aware the message() method is exposed there. Thanks again! I'll close the PR.

@armandino armandino closed this May 5, 2024
@armandino armandino deleted the throwable-message-normalizing-new-lines branch May 5, 2024 22:43
@scordio scordio added the status: declined A suggestion or change that we don't feel we should currently apply label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants