• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.runner.notification;
2 
3 import java.io.Serializable;
4 
5 import org.junit.internal.Throwables;
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  * @since 4.0
16  */
17 public class Failure implements Serializable {
18     private static final long serialVersionUID = 1L;
19 
20     /*
21      * We have to use the f prefix until the next major release to ensure
22      * serialization compatibility.
23      * See https://github.com/junit-team/junit4/issues/976
24      */
25     private final Description fDescription;
26     private final Throwable fThrownException;
27 
28     /**
29      * Constructs a <code>Failure</code> with the given description and exception.
30      *
31      * @param description a {@link org.junit.runner.Description} of the test that failed
32      * @param thrownException the exception that was thrown while running the test
33      */
Failure(Description description, Throwable thrownException)34     public Failure(Description description, Throwable thrownException) {
35         this.fThrownException = thrownException;
36         this.fDescription = description;
37     }
38 
39     /**
40      * @return a user-understandable label for the test
41      */
getTestHeader()42     public String getTestHeader() {
43         return fDescription.getDisplayName();
44     }
45 
46     /**
47      * @return the raw description of the context of the failure.
48      */
getDescription()49     public Description getDescription() {
50         return fDescription;
51     }
52 
53     /**
54      * @return the exception thrown
55      */
56 
getException()57     public Throwable getException() {
58         return fThrownException;
59     }
60 
61     @Override
toString()62     public String toString() {
63         return getTestHeader() + ": " + fThrownException.getMessage();
64     }
65 
66     /**
67      * Gets the printed form of the exception and its stack trace.
68      */
getTrace()69     public String getTrace() {
70         return Throwables.getStacktrace(getException());
71     }
72 
73     /**
74      * Gets a the printed form of the exception, with a trimmed version of the stack trace.
75      * This method will attempt to filter out frames of the stack trace that are below
76      * the test method call.
77      */
getTrimmedTrace()78     public String getTrimmedTrace() {
79         return Throwables.getTrimmedStackTrace(getException());
80     }
81 
82     /**
83      * Convenience method
84      *
85      * @return the message of the thrown exception
86      */
getMessage()87     public String getMessage() {
88         return getException().getMessage();
89     }
90 }
91