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

ThrowableSubject does not provide access to suppressed exceptions #717

Open
JakeWharton opened this issue Jul 30, 2020 · 1 comment
Open
Labels
P3 not scheduled type=addition A new feature

Comments

@JakeWharton
Copy link

I'm testing a testing library which aside from hurting my brain I have a need to assert on the suppressed exceptions which are coming out attached to the exceptions my library throws.

This is a Java 7 feature, so I'm not sure how you'd go about exposing it without requiring that I do Truth8.assertThat(throwable), but being able to do this is far more useful than how exactly it works. Especially given that I no longer have access to the underlying Throwable from the subject to do something like assertThat(throwable.suppressed[0]).isSameInstanceAs(other).

@cpovirk cpovirk added P3 not scheduled type=addition A new feature labels Jul 30, 2020
@cpovirk
Copy link
Member

cpovirk commented Jul 30, 2020

Thanks. I am shocked that I don't recall ever hearing a request for this before. Maybe I've just forgotten, but I at least don't think it's been a common request: Inside Google, assertions on the suppressed exceptions of a Throwable (manual ones, using getSuppressed()) appear to be less than a tenth of a percent as common as assertThat(Throwable) is.

We should someday have a look at what those people are doing. We have at least a couple decisions:

  • "Does it have a suppressed exception like X?" vs. "Return an IterableSubject with information about all suppressed exceptions"
  • Expose a suppressed-exception API solely in terms of Throwable or (also?) in terms of Class / String message?

That expands out into various possible APIs:

hasSuppressedException(Throwable) + hasSuppressedExceptionWithType(Class) + hasSuppressedExceptionWithMessage(String)

suppressedExceptions(), suppressedExceptionTypes(), suppressedExceptionMessages() --
and/or maybe we'd provide Correspondence instances to let you operate directly on suppressedExceptions()?

Maybe enough users have only one suppressed exception that we can afford to provide ThrowableSubject suppressedException() that throws if there's more than one?

Yet another possibility is to go down this road:

public MultipleSuppressedExceptionComparison hasSuppressedExceptionThat() {
  return new MultipleSuppressedExceptionComparison();
}

public static final class MultipleSuppressedExceptionComparison {
  public void isEqualTo(Throwable expected) { ... }

  public void hasMessage(String message) { ... }
  // or, to support regex, contains, etc.:
  public MultipleSuppressedExceptionMessageComparison hasMessageThat() { ... }

  // similarly for causes, exception types, etc.
}

Such a "subject" (though not a true Subject) that stands in for multiple "actual values" is a line we haven't crossed so far.

I think I'm beginning to recall why we decided to wait for demand before pursuing this :)

(On the plus side, if we found API solutions that we're happy with, I'm fairly confident that we could hack reflective or other calls to getSuppressed() in so that this could live in ThrowableSubject itself.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 not scheduled type=addition A new feature
Projects
None yet
Development

No branches or pull requests

2 participants