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

isInstanceOf() should issue a better failure message when the tested object is null #3428

Open
doublep opened this issue Apr 12, 2024 · 0 comments

Comments

@doublep
Copy link

doublep commented Apr 12, 2024

Background: I often use assertThat (catchException (...)).isInstanceOf (...) instead of assertThatThrowBy ... or something similar. I guess it's a matter of taste what to prefer. I have noticed that isInstanceOf() issues a pretty confusing failure message when nothing is actually thrown, i.e. when catchException() returns null.

To illustrate, when I run this code:

assertThat (catchException (() -> { })).isInstanceOf (RuntimeException.class);

I get this failure message:

Expecting actual not to be null

In comparison, when the tested object is not null, but of a wrong class, failure message is much more clear. For example:

assertThat (catchException (() -> { throw new Exception (); })).isInstanceOf (RuntimeException.class);

gives me this:

Expecting actual throwable to be an instance of:
  java.lang.RuntimeException
but was:
  java.lang.Exception

Suggestion

Rephrase the null-case message somewhat like this:

Expecting actual throwable to be an instance of:
  java.lang.RuntimeException
but was:
  null (no throwable)
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

1 participant