001package junit.framework;
002
003import org.junit.internal.Throwables;
004
005
006/**
007 * A {@code TestFailure} collects a failed test together with
008 * the caught exception.
009 *
010 * @see TestResult
011 */
012public class TestFailure {
013    protected Test fFailedTest;
014    protected Throwable fThrownException;
015
016    /**
017     * Constructs a TestFailure with the given test and exception.
018     */
019    public TestFailure(Test failedTest, Throwable thrownException) {
020        fFailedTest = failedTest;
021        fThrownException = thrownException;
022    }
023
024    /**
025     * Gets the failed test.
026     */
027    public Test failedTest() {
028        return fFailedTest;
029    }
030
031    /**
032     * Gets the thrown exception.
033     */
034    public Throwable thrownException() {
035        return fThrownException;
036    }
037
038    /**
039     * Returns a short description of the failure.
040     */
041    @Override
042    public String toString() {
043        return fFailedTest + ": " + fThrownException.getMessage();
044    }
045    
046    /**
047     * Returns a String containing the stack trace of the error
048     * thrown by TestFailure.
049     */
050    public String trace() {
051        return Throwables.getStacktrace(thrownException());
052    }
053
054    /**
055     * Returns a String containing the message from the thrown exception.
056     */
057    public String exceptionMessage() {
058        return thrownException().getMessage();
059    }
060
061    /**
062     * Returns {@code true} if the error is considered a failure
063     * (i.e. if it is an instance of {@code AssertionFailedError}),
064     * {@code false} otherwise.
065     */
066    public boolean isFailure() {
067        return thrownException() instanceof AssertionFailedError;
068    }
069}