Browse Source

Preserve cause of AssertionError

… when converting to AssertionFailedError in JUnitTestRunner.
master
Dana Dahlstrom Stefan Bodewig 9 years ago
parent
commit
421ae34d70
2 changed files with 30 additions and 1 deletions
  1. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
  2. +29
    -1
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java

+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java View File

@@ -1240,6 +1240,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
final String msg = t.getMessage();
final AssertionFailedError failure = msg != null
? new AssertionFailedError(msg) : new AssertionFailedError();
failure.initCause(t.getCause());
failure.setStackTrace(t.getStackTrace());
testListener.addFailure(test, failure);
} else {


+ 29
- 1
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java View File

@@ -116,6 +116,20 @@ public class JUnitTestRunnerTest{
//assertTrue(error, error.indexOf("thrown on purpose") != -1);
}

// check that JUnit 4 synthetic AssertionFailedError gets message and cause from AssertionError
@Test
public void testJUnit4AssertionError(){
TestRunner runner = createRunnerForTestMethod(AssertionErrorTest.class,"throwsAssertionError");
runner.run();

AssertionFailedError failure = runner.getFormatter().getFailure();
assertEquals("failure message", failure.getMessage());

Throwable cause = failure.getCause();
assertEquals(RuntimeException.class, cause.getClass());
assertEquals("cause message", cause.getMessage());
}

protected TestRunner createRunner(Class<?> clazz){
return new TestRunner(new JUnitTest(clazz.getName()), null,
true, true, true);
@@ -145,6 +159,7 @@ public class JUnitTestRunnerTest{

// dummy formatter just to catch the error
private final static class ResultFormatter implements JUnitResultFormatter {
private AssertionFailedError failure;
private Throwable error;
public void setSystemOutput(String output){}
public void setSystemError(String output){}
@@ -153,7 +168,12 @@ public class JUnitTestRunnerTest{
public void setOutput(java.io.OutputStream out){}
public void startTest(junit.framework.Test t) {}
public void endTest(junit.framework.Test test) {}
public void addFailure(junit.framework.Test test, AssertionFailedError t) { }
public void addFailure(junit.framework.Test test, AssertionFailedError t) {
failure = t;
}
AssertionFailedError getFailure() {
return failure;
}
public void addError(junit.framework.Test test, Throwable t) {
error = t;
}
@@ -213,5 +233,13 @@ public class JUnitTestRunnerTest{
throw new NullPointerException("thrown on purpose");
}
}

public static class AssertionErrorTest {
@Test public void throwsAssertionError() {
AssertionError assertionError = new AssertionError("failure message");
assertionError.initCause(new RuntimeException("cause message"));
throw assertionError;
}
}
}


Loading…
Cancel
Save