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

AssumptionViolatedException erroneously initializes cause to "null" #494

Closed
mmichaelis opened this issue Aug 26, 2012 · 6 comments
Closed

Comments

@mmichaelis
Copy link
Contributor

Digging into the code of AssumptionViolatedException you will find:

public AssumptionViolatedException(Object value, Matcher<?> matcher) {
    super(value instanceof Throwable ? (Throwable) value : null);
    fValue= value;
    fMatcher= matcher;
}

According the documentation of "cause" in java.lang.Throwable this states that there is no (known) cause for this exception. If instead the cause is not yet initialized the cause should point to this - which is the initial value in Throwable.

For the work I am doing I need to specify a message and a cause. Having the current API this is not possible:

  • There is no 2-arg constructor with message and cause
  • initCause() cannot be called because Throwable does not allow to set the cause twice (and it got set to null before)

For me the solution would be sufficient to introduce a constructor AssumptionViolatedException(message, cause). But the real problem (from my point of view) is the erroneous initialization with null.

@mmichaelis
Copy link
Contributor Author

This workaround works for me for now:

throw new AssumptionViolatedException(message) {
  @Override
  public Throwable getCause() {
    return lastException;
  }
};

@priav03
Copy link
Contributor

priav03 commented Aug 9, 2014

Is this still an open issue? How can I help?

@kcooney
Copy link
Member

kcooney commented Aug 9, 2014

It looks like it is still open, and should be an easy and safe enough fix to squeeze into 4.12.

@marcin-lawrowski
Copy link

So, the first constructor in org.junit.internal.AssumptionViolatedException class should be fixed to initialize superclass with "this" instead of "null" ?

@kcooney
Copy link
Member

kcooney commented Aug 19, 2014

I believe the first constructor should init the cause via initCause()

kcooney added a commit to kcooney/junit that referenced this issue Sep 6, 2014
kcooney added a commit to kcooney/junit that referenced this issue Sep 9, 2014
kcooney added a commit that referenced this issue Sep 10, 2014
Change AssumptionViolatedException to not set the cause to null; fixes issue #494
@marcphilipp
Copy link
Member

Fixed by #985.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants