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

Tests expecting AssumptionViolatedException should be marked as passed, not skipped #1291

Merged
merged 9 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public void evaluate() throws Exception {
next.evaluate();
complete = true;
} catch (AssumptionViolatedException e) {
throw e;
if (!expected.isAssignableFrom(e.getClass())) {
throw e;
}
} catch (Throwable e) {
if (!expected.isAssignableFrom(e.getClass())) {
String message = "Unexpected exception, expected<"
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/junit/internal/AllInternalTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.internal.matchers.StacktracePrintingMatcherTest;
import org.junit.internal.matchers.ThrowableCauseMatcherTest;
import org.junit.internal.runners.ErrorReportingRunnerTest;
import org.junit.internal.runners.statements.ExpectExceptionTest;
import org.junit.internal.runners.statements.FailOnTimeoutTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
Expand All @@ -13,6 +14,7 @@
@SuiteClasses({
AnnotatedBuilderTest.class,
ErrorReportingRunnerTest.class,
ExpectExceptionTest.class,
FailOnTimeoutTest.class,
MethodSorterTest.class,
StacktracePrintingMatcherTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.junit.internal.runners.statements;

import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runners.model.Statement;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
* Integration tests can be found in {@link org.junit.tests.running.methods.ExpectedTest}.
* See e.g. {@link org.junit.tests.running.methods.ExpectedTest#expectsAssumptionViolatedException()}
*/
public class ExpectExceptionTest {

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingItShouldPass() {
Statement delegate = new Fail(new AssumptionViolatedException("expected"));
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);

try {
expectException.evaluate();
// then AssumptionViolatedException should not be thrown
} catch (Throwable e) { // need to explicitly catch and re-throw as an AssertionError or it might be skipped
fail("should not throw anything, but was thrown: " + e);
}
}

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingSubclassShouldPass() {
Statement delegate = new Fail(new AssumptionViolatedExceptionSubclass("expected"));
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);

try {
expectException.evaluate();
// then no exception should be thrown
} catch (Throwable e) {
fail("should not throw anything, but was thrown: " + e);
Copy link
Member

@kcooney kcooney May 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to catch unexpected exceptions; if evaluate throws an exception then the test would fail.

Edit: Woops! I see why you need to do an explicit catch; without your fix this test would not fail because of the assumption failure exception being thrown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya right! Tricky uh? :) Testing JUnit with JUnit is tricky itself

}
}

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingDifferentExceptionShouldFail() {
Statement delegate = new Fail(new SomeException("not expected"));
ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);

try {
expectException.evaluate();
fail("should throw 'Unexpected exception' when statement throws an exception which is not the one expected");
} catch (Exception e) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more specific exception that can be caught?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not, at the minute. I was going to open a new PR to change the exception thrown in case of unexpected exception to be of a new type UnexpectedException extends AssertionError.

assertThat(e.getMessage(), equalTo("Unexpected exception, expected<org.junit.internal.AssumptionViolatedException> " +
"but was<org.junit.internal.runners.statements.ExpectExceptionTest$SomeException>"));
}
}

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsPassingShouldFail() throws Exception {
ExpectException expectException = new ExpectException(new PassingStatement(), AssumptionViolatedException.class);

try {
expectException.evaluate();
} catch (AssertionError e) {
assertThat(e.getMessage(), containsString("Expected exception: " + AssumptionViolatedException.class.getName()));
return;
}
fail("ExpectException should throw when the given statement passes");
}

private static class PassingStatement extends Statement {
public void evaluate() throws Throwable {
// nop
}
}

private static class SomeException extends RuntimeException {
public SomeException(String message) {
super(message);
}
}

private static class AssumptionViolatedExceptionSubclass extends AssumptionViolatedException {
public AssumptionViolatedExceptionSubclass(String assumption) {
super(assumption);
}
}
}
13 changes: 13 additions & 0 deletions src/test/java/org/junit/tests/running/methods/ExpectedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.internal.AssumptionViolatedException;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
Expand Down Expand Up @@ -67,4 +68,16 @@ public void throwsSubclass() {
public void expectsSuperclass() {
assertTrue(new JUnitCore().run(ExpectSuperclass.class).wasSuccessful());
}

public static class ExpectAssumptionViolatedException {
@Test(expected = AssumptionViolatedException.class)
public void throwsAssumptionViolatedException() {
throw new AssumptionViolatedException("expected");
}
}

@Test
public void expectsAssumptionViolatedException() {
assertTrue(new JUnitCore().run(ExpectAssumptionViolatedException.class).wasSuccessful());
}
}