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 7 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,81 @@
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 statementThrowingAssumptionViolatedException = new Fail(new AssumptionViolatedException("expected"));
ExpectException expectException = new ExpectException(statementThrowingAssumptionViolatedException, AssumptionViolatedException.class);
Copy link
Member

Choose a reason for hiding this comment

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

If you want to keep the long variable name for the Statement arg then please wrap this line to make it readable:

ExpectException expectException = new ExpectException(
        statementThrowingAssumptionViolatedException,
        AssumptionViolatedException.class);

But I really prefer delegate or delegateStatement because that is what the role of the Statement is for the sut. The variable is declared one line up, so the long name isn't needed and makes the code harder to scan IMHO.


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 whenExpectingAssumptionViolatedExceptionStatementsThrowingSubclassShouldPass() {
Statement statementThrowingAssumptionViolatedExceptionSubclass = new Fail(new org.junit.AssumptionViolatedException("expected"));
ExpectException expectException = new ExpectException(statementThrowingAssumptionViolatedExceptionSubclass, AssumptionViolatedException.class);

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

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingDifferentExceptionShouldFail() {
Statement statementThrowingSomeException = new Fail(new SomeException("not expected"));
ExpectException expectException = new ExpectException(statementThrowingSomeException, 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();
fail("ExpectException should throw when the given statement passes");
Copy link
Member

Choose a reason for hiding this comment

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

This call throws AssertionError so this won't work. You want:

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");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the catch block I verify the exception message, so it works, but I agree it's not nice.
Your solution is correct but a little hard to read.

As in the other comment, the best would be to have the SUT throw a more specific exception. I'm not sure about the name though..ExpectedException?

Copy link
Member

Choose a reason for hiding this comment

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

The idiom.I wrote above is quite common in JUnit's tests, so let's go with that. We can't simply rely on checking the exception message because it also can result in tests passing that shouldn't pass, plus the failure messages you get if the test fails are confusing.

I don't think a new exception type is worth it, bu the other maintainers might disagree with me and agree with you :-(

Verifying that a method or constructor call throws a specific exception would be easier if we could use lambdas and assertThrows

Copy link
Member

Choose a reason for hiding this comment

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

I agree with Kevin. Let's not introduce a new exception class.

@alb-i986 Please follow @kcooney's suggestion and rewrite the try-catch-block accordingly.

} catch (AssertionError e) {
assertThat(e.getMessage(), containsString("Expected exception: " + AssumptionViolatedException.class.getName()));
}
}

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

private static class SomeException extends RuntimeException {
public SomeException(String message) {
super(message);
}
}
}
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());
}
}