• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.junit.runner;
2 
3 import junit.runner.Version;
4 import org.junit.internal.JUnitSystem;
5 import org.junit.internal.RealSystem;
6 import org.junit.internal.TextListener;
7 import org.junit.internal.runners.JUnit38ClassRunner;
8 import org.junit.runner.notification.RunListener;
9 import org.junit.runner.notification.RunNotifier;
10 
11 /**
12  * <code>JUnitCore</code> is a facade for running tests. It supports running JUnit 4 tests,
13  * JUnit 3.8.x tests, and mixtures. To run tests from the command line, run
14  * <code>java org.junit.runner.JUnitCore TestClass1 TestClass2 ...</code>.
15  * For one-shot test runs, use the static method {@link #runClasses(Class[])}.
16  * If you want to add special listeners,
17  * create an instance of {@link org.junit.runner.JUnitCore} first and use it to run the tests.
18  *
19  * @see org.junit.runner.Result
20  * @see org.junit.runner.notification.RunListener
21  * @see org.junit.runner.Request
22  * @since 4.0
23  */
24 public class JUnitCore {
25     private final RunNotifier notifier = new RunNotifier();
26 
27     /**
28      * Run the tests contained in the classes named in the <code>args</code>.
29      * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
30      * Write feedback while tests are running and write
31      * stack traces for all failed tests after the tests all complete.
32      *
33      * @param args names of classes in which to find tests to run
34      */
main(String... args)35     public static void main(String... args) {
36         Result result = new JUnitCore().runMain(new RealSystem(), args);
37         System.exit(result.wasSuccessful() ? 0 : 1);
38     }
39 
40     /**
41      * Run the tests contained in <code>classes</code>. Write feedback while the tests
42      * are running and write stack traces for all failed tests after all tests complete. This is
43      * similar to {@link #main(String[])}, but intended to be used programmatically.
44      *
45      * @param classes Classes in which to find tests
46      * @return a {@link Result} describing the details of the test run and the failed tests.
47      */
runClasses(Class<?>.... classes)48     public static Result runClasses(Class<?>... classes) {
49         return runClasses(defaultComputer(), classes);
50     }
51 
52     /**
53      * Run the tests contained in <code>classes</code>. Write feedback while the tests
54      * are running and write stack traces for all failed tests after all tests complete. This is
55      * similar to {@link #main(String[])}, but intended to be used programmatically.
56      *
57      * @param computer Helps construct Runners from classes
58      * @param classes  Classes in which to find tests
59      * @return a {@link Result} describing the details of the test run and the failed tests.
60      */
runClasses(Computer computer, Class<?>... classes)61     public static Result runClasses(Computer computer, Class<?>... classes) {
62         return new JUnitCore().run(computer, classes);
63     }
64 
65     /**
66      * @param system
67      * @param args from main()
68      */
runMain(JUnitSystem system, String... args)69     Result runMain(JUnitSystem system, String... args) {
70         system.out().println("JUnit version " + Version.id());
71 
72         JUnitCommandLineParseResult jUnitCommandLineParseResult = JUnitCommandLineParseResult.parse(args);
73 
74         RunListener listener = new TextListener(system);
75         addListener(listener);
76 
77         return run(jUnitCommandLineParseResult.createRequest(defaultComputer()));
78     }
79 
80     /**
81      * @return the version number of this release
82      */
getVersion()83     public String getVersion() {
84         return Version.id();
85     }
86 
87     /**
88      * Run all the tests in <code>classes</code>.
89      *
90      * @param classes the classes containing tests
91      * @return a {@link Result} describing the details of the test run and the failed tests.
92      */
run(Class<?>.... classes)93     public Result run(Class<?>... classes) {
94         return run(defaultComputer(), classes);
95     }
96 
97     /**
98      * Run all the tests in <code>classes</code>.
99      *
100      * @param computer Helps construct Runners from classes
101      * @param classes the classes containing tests
102      * @return a {@link Result} describing the details of the test run and the failed tests.
103      */
run(Computer computer, Class<?>... classes)104     public Result run(Computer computer, Class<?>... classes) {
105         return run(Request.classes(computer, classes));
106     }
107 
108     /**
109      * Run all the tests contained in <code>request</code>.
110      *
111      * @param request the request describing tests
112      * @return a {@link Result} describing the details of the test run and the failed tests.
113      */
run(Request request)114     public Result run(Request request) {
115         return run(request.getRunner());
116     }
117 
118     /**
119      * Run all the tests contained in JUnit 3.8.x <code>test</code>. Here for backward compatibility.
120      *
121      * @param test the old-style test
122      * @return a {@link Result} describing the details of the test run and the failed tests.
123      */
run(junit.framework.Test test)124     public Result run(junit.framework.Test test) {
125         return run(new JUnit38ClassRunner(test));
126     }
127 
128     /**
129      * Do not use. Testing purposes only.
130      */
run(Runner runner)131     public Result run(Runner runner) {
132         Result result = new Result();
133         RunListener listener = result.createListener();
134         notifier.addFirstListener(listener);
135         try {
136             notifier.fireTestRunStarted(runner.getDescription());
137             runner.run(notifier);
138             notifier.fireTestRunFinished(result);
139         } finally {
140             removeListener(listener);
141         }
142         return result;
143     }
144 
145     /**
146      * Add a listener to be notified as the tests run.
147      *
148      * @param listener the listener to add
149      * @see org.junit.runner.notification.RunListener
150      */
addListener(RunListener listener)151     public void addListener(RunListener listener) {
152         notifier.addListener(listener);
153     }
154 
155     /**
156      * Remove a listener.
157      *
158      * @param listener the listener to remove
159      */
removeListener(RunListener listener)160     public void removeListener(RunListener listener) {
161         notifier.removeListener(listener);
162     }
163 
defaultComputer()164     static Computer defaultComputer() {
165         return new Computer();
166     }
167 }
168