• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junit.framework;
2 
3 import org.junit.internal.Throwables;
4 
5 
6 /**
7  * A {@code TestFailure} collects a failed test together with
8  * the caught exception.
9  *
10  * @see TestResult
11  */
12 public class TestFailure {
13     protected Test fFailedTest;
14     protected Throwable fThrownException;
15 
16     /**
17      * Constructs a TestFailure with the given test and exception.
18      */
TestFailure(Test failedTest, Throwable thrownException)19     public TestFailure(Test failedTest, Throwable thrownException) {
20         fFailedTest = failedTest;
21         fThrownException = thrownException;
22     }
23 
24     /**
25      * Gets the failed test.
26      */
failedTest()27     public Test failedTest() {
28         return fFailedTest;
29     }
30 
31     /**
32      * Gets the thrown exception.
33      */
thrownException()34     public Throwable thrownException() {
35         return fThrownException;
36     }
37 
38     /**
39      * Returns a short description of the failure.
40      */
41     @Override
toString()42     public String toString() {
43         return fFailedTest + ": " + fThrownException.getMessage();
44     }
45 
46     /**
47      * Returns a String containing the stack trace of the error
48      * thrown by TestFailure.
49      */
trace()50     public String trace() {
51         return Throwables.getStacktrace(thrownException());
52     }
53 
54     /**
55      * Returns a String containing the message from the thrown exception.
56      */
exceptionMessage()57     public String exceptionMessage() {
58         return thrownException().getMessage();
59     }
60 
61     /**
62      * Returns {@code true} if the error is considered a failure
63      * (i.e. if it is an instance of {@code AssertionFailedError}),
64      * {@code false} otherwise.
65      */
isFailure()66     public boolean isFailure() {
67         return thrownException() instanceof AssertionFailedError;
68     }
69 }