FAQ
Preserve cause of AssertionError

… when converting to AssertionFailedError in JUnitTestRunner.


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/421ae34d
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/421ae34d
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/421ae34d

Branch: refs/heads/1.9.x
Commit: 421ae34d7037a8cbdb372e938e269caa081b7166
Parents: 3966616
Author: Dana Dahlstrom <dahlstrom+apache@googlers.com>
Authored: Mon Feb 8 21:40:56 2016 -0800
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sun May 22 18:34:28 2016 +0200

----------------------------------------------------------------------
  .../optional/junit/JUnitTestRunner.java | 1 +
  .../optional/junit/JUnitTestRunnerTest.java | 30 +++++++++++++++++++-
  2 files changed, 30 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/421ae34d/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
index a457375..0397d84 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
@@ -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 {

http://git-wip-us.apache.org/repos/asf/ant/blob/421ae34d/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
index a75c5cb..e5092fe 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunnerTest.java
@@ -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;
+ }
+ }
  }

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 7 | next ›
Discussion Overview
groupnotifications @
categoriesant
postedMay 22, '16 at 4:39p
activeMay 22, '16 at 4:39p
posts7
users1
websiteant.apache.org

1 user in discussion

Bodewig: 7 posts

People

Translate

site design / logo © 2017 Grokbase