All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.runner.JUnitCore Maven / Gradle / Ivy

There is a newer version: 2.0.9
Show newest version
/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.junit.runner;

import junit.runner.Version;
import org.junit.internal.TextListener;
import org.junit.internal.runners.JUnit38ClassRunner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.powermock.modules.agent.PowerMockAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * JUnitCore is a facade for running tests. It supports running JUnit 4 tests, 
 * JUnit 3.8.x tests, and mixtures. To run tests from the command line, run 
 * java org.junit.runner.JUnitCore TestClass1 TestClass2 ....
 * For one-shot test runs, use the static method {@link #runClasses(Class[])}. 
 * If you want to add special listeners,
 * create an instance of {@link org.junit.runner.JUnitCore} first and use it to run the tests.
 * 
 * @see org.junit.runner.Result
 * @see org.junit.runner.notification.RunListener
 * @see org.junit.runner.Request
 */
public class JUnitCore {
    static { PowerMockAgent.initializeIfPossible(); }
	
	private RunNotifier fNotifier;

	/**
	 * Create a new JUnitCore to run tests.
	 */
	public JUnitCore() {
		fNotifier= new RunNotifier();
	}

	/**
	 * Run the tests contained in the classes named in the args.
	 * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1.
	 * Write feedback while tests are running and write
	 * stack traces for all failed tests after the tests all complete.
	 * @param args names of classes in which to find tests to run
	 */
	public static void main(String... args) {
		Result result= new JUnitCore().runMain(args);
		killAllThreads(result);
	}

	private static void killAllThreads(Result result) {
		System.exit(result.wasSuccessful() ? 0 : 1);
	}
	
	/**
	 * Run the tests contained in classes. Write feedback while the tests
	 * are running and write stack traces for all failed tests after all tests complete. This is
	 * similar to {@link #main(String[])}, but intended to be used programmatically.
	 * @param classes Classes in which to find tests
	 * @return a {@link Result} describing the details of the test run and the failed tests.
	 */
	public static Result runClasses(Class... classes) {
		return new JUnitCore().run(classes);
	}
	
	/**
	 * Do not use. Testing purposes only.
	 */
	public Result runMain(String... args) {
		System.out.println("JUnit version " + Version.id());
		List> classes= new ArrayList>();
		List missingClasses= new ArrayList();
		for (String each : args)
			try {
				classes.add(Class.forName(each));
			} catch (ClassNotFoundException e) {
				System.out.println("Could not find class: " + each);
				Description description= Description.createSuiteDescription(each);
				Failure failure= new Failure(description, e);
				missingClasses.add(failure);
			}
		RunListener listener= new TextListener(System.out);
		addListener(listener);
		Result result= run(classes.toArray(new Class[0]));
		for (Failure each : missingClasses)
			result.getFailures().add(each);
		return result;
	}

	/**
	 * @return the version number of this release
	 */
	public String getVersion() {
		return Version.id();
	}
	
	/**
	 * Run all the tests in classes.
	 * @param classes the classes containing tests
	 * @return a {@link Result} describing the details of the test run and the failed tests.
	 */
	public Result run(Class... classes) {
		return run(Request.classes(classes));
	}

	/**
	 * Run all the tests contained in request.
	 * @param request the request describing tests
	 * @return a {@link Result} describing the details of the test run and the failed tests.
	 */
	public Result run(Request request) {
		return run(request.getRunner());
	}

	/**
	 * Run all the tests contained in JUnit 3.8.x test. Here for backward compatibility.
	 * @param test the old-style test
	 * @return a {@link Result} describing the details of the test run and the failed tests.
	 */
	public Result run(junit.framework.Test test) { 
		return run(new JUnit38ClassRunner(test));
	}
	
	/**
	 * Do not use. Testing purposes only.
	 */
	public Result run(Runner runner) {
		Result result= new Result();
		RunListener listener= result.createListener();
		addFirstListener(listener);
		try {
			fNotifier.fireTestRunStarted(runner.getDescription());
			runner.run(fNotifier);
			fNotifier.fireTestRunFinished(result);
		} finally {
			removeListener(listener);
		}
		return result;
	}
	
	private void addFirstListener(RunListener listener) {
		fNotifier.addFirstListener(listener);
	}
	

	/**
	 * Add a listener to be notified as the tests run.
	 * @param listener the listener to add
	 * @see org.junit.runner.notification.RunListener
	 */
	public void addListener(RunListener listener) {
		fNotifier.addListener(listener);
	}

	/**
	 * Remove a listener.
	 * @param listener the listener to remove
	 */
	public void removeListener(RunListener listener) {
		fNotifier.removeListener(listener);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy