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

Assumptions don't work in tests with expected exceptions #121

Closed
petur opened this issue Jul 13, 2010 · 4 comments
Closed

Assumptions don't work in tests with expected exceptions #121

petur opened this issue Jul 13, 2010 · 4 comments

Comments

@petur
Copy link

petur commented Jul 13, 2010

The following test should be ignored, but fails with JUnit 4.8.2:

@Test(expected = UnsupportedOperationException.class)
public void testExpectedVsAssumption() {
    Assume.assumeTrue(false);
}

This test succeeds if run with @RunWith(JUnit4ClassRunner.class). The reason it works with the older runner, but fails with the new runner seems to be that org.junit.internal.runners.MethodRoadie.runTestMethod() has special handling for AssumptionViolatedException, but this handling is missing from org.junit.internal.runners.statements.ExpectException.evaluate().

@petur
Copy link
Author

petur commented Jul 13, 2010

It would also be nice if ExpectException didn't wrap AssertionError. I would expect the following test to fail with an AssertionError, but it fails with an Exception instead:

@Test(expected = UnsupportedOperationException.class)
public void testExpectedVsAssertion() {
    Assert.assertTrue(false);
}

The stack trace is:

java.lang.Exception: Unexpected exception, expected<java.lang.UnsupportedOperationException> but was<java.lang.AssertionError>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
Caused by: java.lang.AssertionError:
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.assertTrue(Assert.java:43)
at org.junit.Assert.assertTrue(Assert.java:54)
at AssumptionVsExpectedTest.testExpectedVsAssertion(AssumptionVsExpectedTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:21)
... 15 more

@petur
Copy link
Author

petur commented Jul 13, 2010

The same problem appears when using rules instead. This fails, but should probably succeed:

@Rule
public ExpectedException ex = ExpectedException.none();

@Test
public void testExpectedVsAssumption() {
    ex.expect(UnsupportedOperationException.class);

    Assume.assumeTrue(false);
}

In this case, it is easy to move the assumption above the expect, but that is not possible when using @test(expected = xxx.class).

@stefanbirkner
Copy link
Contributor

The introductory issue is a duplicate of issue #98 and is already fixed in 4.9.

stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Sep 11, 2011
An AssumptionViolatedException thrown by the Statement is rethrown.
Therefore tests are ignored if assume... fails, regardless whether
an expected exception is thrown or not.

Fix junit-team#121. Thanks to petur for discovering this bug.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Sep 11, 2011
An AssertionError thrown by the Statement is rethrown. Therefore tests
fail because of the AssertionError and not because of a wrong expected
exception.

Fix junit-team#121. Thanks to petur for discovering this bug.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Sep 21, 2011
ExpectedException rule no longer handles AssertionErrors and
AssumptionViolatedExceptions by default. This means that the
rule doesn't intercept if your test fails because of an violated
assertion or assumption.

If you want to test assertions or assumptions you have to tell
the rule to handle such exceptions via handleAssertionErrors()
or handleAssumptionViolatedExceptions().
@stefanbirkner
Copy link
Contributor

I fixed ExpectedException to work with assume and don't wrap AssertionErrors. AssertionErrors are still wrapped if you're using the expected=Foo.class notation. Please use ExpectedException to avoid wrapping.

@dsaff dsaff closed this as completed in c4279e4 Apr 9, 2012
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Aug 17, 2013
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Aug 17, 2013
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Aug 17, 2013
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Aug 17, 2013
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Sep 3, 2013
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Sep 3, 2013
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Oct 16, 2013
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Oct 16, 2013
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Oct 16, 2013
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Oct 16, 2013
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
stefanbirkner added a commit to stefanbirkner/junit that referenced this issue Jan 23, 2014
Still fixes junit-team#121, but the original fix was superfluous. Added documentation
about handling assumes and asserts in conjunction with the
ExpectedException rule.
josephw added a commit to josephw/hamcrest-junit that referenced this issue Jun 1, 2015
This class's behaviour changed in JUnit 4.12
(mainly due to junit-team/junit4#121).

Bring across the fix, and its tests, along with class
reformatting.
josephw added a commit to josephw/hamcrest-junit that referenced this issue Jun 1, 2015
This class's behaviour changed in JUnit 4.12
(mainly due to junit-team/junit4#121).

Bring across the fix, and its tests, along with class
reformatting.
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

3 participants