import org.junit.runner.JUnitCore; import org.junit.runner.Request; import org.junit.runner.Result; public class SingleJUnitTestRunner { public static void main(String... args) throws ClassNotFoundException { String[] classAndMethod = args[0].split("#"); Request request = Request.method(Class.forName(classAndMethod[0]), classAndMethod[1]); Result result = new JUnitCore().run(request); System.exit(result.wasSuccessful() ? 0 : 1); } }
/** * Run the tests contained in <code>classes</code>. 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 computer Helps construct Runners from classes * @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(Computer computer, Class<?>... classes) { return new JUnitCore().run(computer, classes); }
/** * The result of running JUnit on Request {@code request} */ public static PrintableResult testResult(Request request) { return new PrintableResult(new JUnitCore().run(request)); }
/** * Run all the tests contained in <code>request</code>. * * @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, new JUnitCore()); }
/** * Run the tests contained in <code>classes</code>. 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 computer Helps construct Runners from classes * @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(Computer computer, Class<?>... classes) { return new JUnitCore().run(computer, classes); }
/** * Run all the tests contained in <code>request</code>. * * @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, new JUnitCore()); }
/** * The result of running JUnit on Request {@code request} */ public static PrintableResult testResult(Request request) { return new PrintableResult(new JUnitCore().run(request)); }
/** * Runs main and exits */ private static void runMainAndExit(JUnitSystem system, String... args) { Result result = new JUnitCore().runMain(system, args); System.exit(result.wasSuccessful() ? 0 : 1); }
/** * Run the tests contained in <code>classes</code>. 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(defaultComputer(), classes); }
/** * Run the tests contained in the classes named in the <code>args</code>. * 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(new RealSystem(), args); System.exit(result.wasSuccessful() ? 0 : 1); }
public static Failure runTestWithExpectedFailure(final Class<?> test) { JUnitCore junitCore = new JUnitCore(); Result result = junitCore.run(Request.aClass(test).getRunner()); List<Failure> failures = result.getFailures(); assertThat(failures).hasSize(1); return failures.get(0); }
public static Result runTestWithValidation(final Class<?> test) { JUnitCore junitCore = new JUnitCore(); Result result = junitCore.run(Request.aClass(test).getRunner()); List<Failure> failures = result.getFailures(); if (!failures.isEmpty()) { Failure firstFailure = failures.get(0); throw new AssertionError(firstFailure.getException()); } assertThat(result.wasSuccessful()).isTrue(); return result; }
public static Result runTest(final Class<?> test) { JUnitCore junitCore = new JUnitCore(); return junitCore.run(Request.aClass(test).getRunner()); }
public static List<Failure> runTestWithExpectedFailures(final Class<?> test) { JUnitCore junitCore = new JUnitCore(); Result result = junitCore.run(Request.aClass(test).getRunner()); List<Failure> failures = result.getFailures(); assertThat(failures).isNotEmpty(); return failures; } }
@Test public void testReportLateFailures() { Result result; try { result = new JUnitCore().run(new BlockJUnit4ClassRunner(LateFailureReport.class)); } catch (InitializationError initializationError) { throw new AssertionError(initializationError); } assertEquals(1, result.getFailureCount()); assertEquals(IllegalStateException.class, result.getFailures().get(0).getException().getClass()); } }
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive, boolean isBarContextActive, boolean isBazContextActive) { JUnitCore jUnitCore = new JUnitCore(); Result result = jUnitCore.run(testClass); assertTrue("all tests passed", result.wasSuccessful()); assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue()); ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context; assertEquals("baz", ContextHierarchyDirtiesContextTests.baz); assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive)); ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent(); assertThat(barContext, notNullValue()); assertEquals("bar", ContextHierarchyDirtiesContextTests.bar); assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive)); ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent(); assertThat(fooContext, notNullValue()); assertEquals("foo", ContextHierarchyDirtiesContextTests.foo); assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive)); }
@Test public void testWorkingCategoryAndParameterized() { Request request = Request.aClass(WorkingCategoryClass.class); ExposedParameterized runner = (ExposedParameterized) request.getRunner(); request = request.filterWith(new CategoryFilter((ExposedGetAnnotations) runner.getChildren().get(0))); Result result = new JUnitCore().run(request); assertEquals(2, result.getRunCount()); }
@Test public void testBrokenCategoryAndParameterized() { Request request = Request.aClass(BrokenCategoryClass.class); ExposedParameterized runner = (ExposedParameterized) request.getRunner(); request = request.filterWith(new CategoryFilter( (ExposedBlockJUnit4ClassRunnerWithParameters) runner.getChildren().get(0))); Result result = new JUnitCore().run(request); assertEquals( "Yeah!! This might actually mean we've upgraded to JUnit 4.13. Hurry up already and delete this hack.", 1, result.getRunCount()); }
JUnitCore junit = new JUnitCore(); junit.addListener(listener); junit.run(testClass);
Class<?>... testClasses) throws Exception { JUnitCore junit = new JUnitCore(); TrackingRunListener listener = new TrackingRunListener(); junit.addListener(listener);