View Javadoc
1   package org.junit.runner;
2   
3   import org.junit.runner.notification.Failure;
4   import org.junit.runner.notification.RunListener;
5   
6   import java.io.Serializable;
7   import java.util.List;
8   import java.util.concurrent.CopyOnWriteArrayList;
9   import java.util.concurrent.atomic.AtomicInteger;
10  import java.util.concurrent.atomic.AtomicLong;
11  
12  /**
13   * A <code>Result</code> collects and summarizes information from running multiple tests.
14   * All tests are counted -- additional information is collected from tests that fail.
15   *
16   * @since 4.0
17   */
18  public class Result implements Serializable {
19      private static final long serialVersionUID = 2L;
20      private final AtomicInteger count = new AtomicInteger();
21      private final AtomicInteger ignoreCount = new AtomicInteger();
22      private final CopyOnWriteArrayList<Failure> failures = new CopyOnWriteArrayList<Failure>();
23      private final AtomicLong runTime = new AtomicLong();
24      private final AtomicLong startTime = new AtomicLong();
25  
26      /**
27       * @return the number of tests run
28       */
29      public int getRunCount() {
30          return count.get();
31      }
32  
33      /**
34       * @return the number of tests that failed during the run
35       */
36      public int getFailureCount() {
37          return failures.size();
38      }
39  
40      /**
41       * @return the number of milliseconds it took to run the entire suite to run
42       */
43      public long getRunTime() {
44          return runTime.get();
45      }
46  
47      /**
48       * @return the {@link Failure}s describing tests that failed and the problems they encountered
49       */
50      public List<Failure> getFailures() {
51          return failures;
52      }
53  
54      /**
55       * @return the number of tests ignored during the run
56       */
57      public int getIgnoreCount() {
58          return ignoreCount.get();
59      }
60  
61      /**
62       * @return <code>true</code> if all tests succeeded
63       */
64      public boolean wasSuccessful() {
65          return getFailureCount() == 0;
66      }
67  
68      @RunListener.ThreadSafe
69      private class Listener extends RunListener {
70          @Override
71          public void testRunStarted(Description description) throws Exception {
72              startTime.set(System.currentTimeMillis());
73          }
74  
75          @Override
76          public void testRunFinished(Result result) throws Exception {
77              long endTime = System.currentTimeMillis();
78              runTime.addAndGet(endTime - startTime.get());
79          }
80  
81          @Override
82          public void testFinished(Description description) throws Exception {
83              count.getAndIncrement();
84          }
85  
86          @Override
87          public void testFailure(Failure failure) throws Exception {
88              failures.add(failure);
89          }
90  
91          @Override
92          public void testIgnored(Description description) throws Exception {
93              ignoreCount.getAndIncrement();
94          }
95  
96          @Override
97          public void testAssumptionFailure(Failure failure) {
98              // do nothing: same as passing (for 4.5; may change in 4.6)
99          }
100     }
101 
102     /**
103      * Internal use only.
104      */
105     public RunListener createListener() {
106         return new Listener();
107     }
108 }