• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package junit.framework;
2 
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
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 {
13 	protected Test fFailedTest;
14 	protected Throwable fThrownException;
15 
16 
17 	/**
18 	 * Constructs a TestFailure with the given test and exception.
19 	 */
TestFailure(Test failedTest, Throwable thrownException)20 	public TestFailure(Test failedTest, Throwable thrownException) {
21 		fFailedTest= failedTest;
22 		fThrownException= thrownException;
23 	}
24 	/**
25 	 * Gets the failed test.
26 	 */
failedTest()27 	public Test failedTest() {
28 	    return fFailedTest;
29 	}
30 	/**
31 	 * Gets the thrown exception.
32 	 */
thrownException()33 	public Throwable thrownException() {
34 	    return fThrownException;
35 	}
36 	/**
37 	 * Returns a short description of the failure.
38 	 */
toString()39 	public String toString() {
40 	    StringBuffer buffer= new StringBuffer();
41 	    buffer.append(fFailedTest+": "+fThrownException.getMessage());
42 	    return buffer.toString();
43 	}
trace()44 	public String trace() {
45 		StringWriter stringWriter= new StringWriter();
46 		PrintWriter writer= new PrintWriter(stringWriter);
47 		thrownException().printStackTrace(writer);
48 		StringBuffer buffer= stringWriter.getBuffer();
49 		return buffer.toString();
50 	}
exceptionMessage()51 	public String exceptionMessage() {
52 		return thrownException().getMessage();
53 	}
isFailure()54 	public boolean isFailure() {
55 		return thrownException() instanceof AssertionFailedError;
56 	}
57 }
58