KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > framework > TestFailure


1 package junit.framework;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.StringWriter JavaDoc;
5
6
7 /**
8  * A <code>TestFailure</code> collects a failed test together with
9  * the caught exception.
10  * @see TestResult
11  */

12 public class TestFailure extends Object JavaDoc {
13     protected Test fFailedTest;
14     protected Throwable JavaDoc fThrownException;
15     
16
17     /**
18      * Constructs a TestFailure with the given test and exception.
19      */

20     public TestFailure(Test failedTest, Throwable JavaDoc thrownException) {
21         fFailedTest= failedTest;
22         fThrownException= thrownException;
23     }
24     /**
25      * Gets the failed test.
26      */

27     public Test failedTest() {
28         return fFailedTest;
29     }
30     /**
31      * Gets the thrown exception.
32      */

33     public Throwable JavaDoc thrownException() {
34         return fThrownException;
35     }
36     /**
37      * Returns a short description of the failure.
38      */

39     @Override JavaDoc
40     public String JavaDoc toString() {
41         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
42         buffer.append(fFailedTest+": "+fThrownException.getMessage());
43         return buffer.toString();
44     }
45     public String JavaDoc trace() {
46         StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
47         PrintWriter JavaDoc writer= new PrintWriter JavaDoc(stringWriter);
48         thrownException().printStackTrace(writer);
49         StringBuffer JavaDoc buffer= stringWriter.getBuffer();
50         return buffer.toString();
51     }
52     public String JavaDoc exceptionMessage() {
53         return thrownException().getMessage();
54     }
55     public boolean isFailure() {
56         return thrownException() instanceof AssertionFailedError;
57     }
58 }
Popular Tags