KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > junit > runner > notification > Failure


1 package org.junit.runner.notification;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.StringWriter JavaDoc;
5
6 import org.junit.runner.Description;
7
8 /**
9  * A <code>Failure</code> holds a description of the failed test and the
10  * exception that was thrown while running it. In most cases the {@link org.junit.runner.Description}
11  * will be of a single test. However, if problems are encountered while constructing the
12  * test (for example, if a {@link org.junit.BeforeClass} method is not static), it may describe
13  * something other than a single test.
14  */

15 public class Failure {
16     private final Description fDescription;
17     private Throwable JavaDoc fThrownException;
18
19     /**
20      * Constructs a <code>Failure</code> with the given description and exception.
21      * @param description a {@link org.junit.runner.Description} of the test that failed
22      * @param thrownException the exception that was thrown while running the test
23      */

24     public Failure(Description description, Throwable JavaDoc thrownException) {
25         fThrownException = thrownException;
26         fDescription= description;
27     }
28
29     /**
30      * @return a user-understandable label for the test
31      */

32     public String JavaDoc getTestHeader() {
33         return fDescription.getDisplayName();
34     }
35
36     /**
37      * @return the raw description of the context of the failure.
38      */

39     public Description getDescription() {
40         return fDescription;
41     }
42
43     /**
44      * @return the exception thrown
45      */

46
47     public Throwable JavaDoc getException() {
48         return fThrownException;
49     }
50
51     @Override JavaDoc
52     public String JavaDoc toString() {
53         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
54         buffer.append(getTestHeader() + ": "+fThrownException.getMessage());
55         return buffer.toString();
56     }
57
58     /**
59      * Convenience method
60      * @return the printed form of the exception
61      */

62     public String JavaDoc getTrace() {
63         StringWriter JavaDoc stringWriter= new StringWriter JavaDoc();
64         PrintWriter JavaDoc writer= new PrintWriter JavaDoc(stringWriter);
65         getException().printStackTrace(writer);
66         StringBuffer JavaDoc buffer= stringWriter.getBuffer();
67         return buffer.toString();
68     }
69
70     /**
71      * Convenience method
72      * @return the message of the thrown exception
73      */

74     public String JavaDoc getMessage() {
75         return getException().getMessage();
76     }
77 }
78
Popular Tags