Java Code Examples for org.junit.runner.JUnitCore#addListener()

The following examples show how to use org.junit.runner.JUnitCore#addListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test @Suppress in combination with size that filters out all methods */
@Test
public void testSuppress_withSize() {
  Request request =
      builder
          .addTestClass(SampleJUnit3Suppressed.class.getName())
          .addTestClass(SampleJUnit3Test.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  MyRunListener l = new MyRunListener();
  testRunner.addListener(l);
  Result r = testRunner.run(request);
  Assert.assertEquals(2, r.getRunCount());
  Assert.assertEquals(2, l.testCount);
}
 
Example 2
Source File: End2EndTestDriver.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
protected int doWork() throws Exception {
  //this is called from the command line, so we should set to use the distributed cluster
  IntegrationTestingUtility.setUseDistributedCluster(conf);
  Class<?>[] classes = findEnd2EndTestClasses();
  System.out.println("Found " + classes.length + " end2end tests to run:");
  for (Class<?> aClass : classes) {
      System.out.println("  " + aClass);
  }
  if(skipTests) return 0;
  
  JUnitCore junit = new JUnitCore();
  junit.addListener(new End2EndTestListenter(System.out));
  Result result = junit.run(classes);

  return result.wasSuccessful() ? 0 : 1;
}
 
Example 3
Source File: SquidbTestRunner.java    From squidb with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the test classes given in {@param classes}.
 *
 * @returns Zero if all tests pass, non-zero otherwise.
 */
public static int run(Class[] classes, RunListener listener, PrintStream out) {
    JUnitCore junitCore = new JUnitCore();
    junitCore.addListener(listener);
    boolean hasError = false;
    int numTests = 0;
    int numFailures = 0;
    long start = System.currentTimeMillis();
    for (@AutoreleasePool Class c : classes) {
        out.println("Running " + c.getName());
        Result result = junitCore.run(c);
        numTests += result.getRunCount();
        numFailures += result.getFailureCount();
        hasError = hasError || !result.wasSuccessful();
    }
    long end = System.currentTimeMillis();
    out.println(String.format("Ran %d tests, %d failures. Total time: %s seconds", numTests, numFailures,
            NumberFormat.getInstance().format((double) (end - start) / 1000)));
    return hasError ? 1 : 0;
}
 
Example 4
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Test case where method has both a size annotation and suppress annotation. Expect suppress to
 * overrule the size.
 */
@Test
public void testSizeWithSuppress() {
  Request request =
      builder
          .addTestClass(SampleSizeWithSuppress.class.getName())
          .addTestClass(SampleJUnit3Test.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  MyRunListener l = new MyRunListener();
  testRunner.addListener(l);
  Result r = testRunner.run(request);
  Assert.assertEquals(2, r.getRunCount());
  Assert.assertEquals(2, l.testCount);
}
 
Example 5
Source File: JUnitTestingUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Run the tests in the supplied {@code testClass}, using the specified
 * {@link Runner}, and assert the expectations of the test execution.
 *
 * <p>If the specified {@code runnerClass} is {@code null}, the tests
 * will be run with the runner that the test class is configured with
 * (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
 *
 * @param runnerClass the explicit runner class to use or {@code null}
 * if the implicit runner should be used
 * @param testClass the test class to run with JUnit
 * @param expectedStartedCount the expected number of tests that started
 * @param expectedFailedCount the expected number of tests that failed
 * @param expectedFinishedCount the expected number of tests that finished
 * @param expectedIgnoredCount the expected number of tests that were ignored
 * @param expectedAssumptionFailedCount the expected number of tests that
 * resulted in a failed assumption
 */
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
		int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
		int expectedAssumptionFailedCount) throws Exception {

	TrackingRunListener listener = new TrackingRunListener();

	if (runnerClass != null) {
		Constructor<?> constructor = runnerClass.getConstructor(Class.class);
		Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
		RunNotifier notifier = new RunNotifier();
		notifier.addListener(listener);
		runner.run(notifier);
	}
	else {
		JUnitCore junit = new JUnitCore();
		junit.addListener(listener);
		junit.run(testClass);
	}

	assertEquals("tests started for [" + testClass + "]:", expectedStartedCount, listener.getTestStartedCount());
	assertEquals("tests failed for [" + testClass + "]:", expectedFailedCount, listener.getTestFailureCount());
	assertEquals("tests finished for [" + testClass + "]:", expectedFinishedCount, listener.getTestFinishedCount());
	assertEquals("tests ignored for [" + testClass + "]:", expectedIgnoredCount, listener.getTestIgnoredCount());
	assertEquals("failed assumptions for [" + testClass + "]:", expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount());
}
 
Example 6
Source File: JUnitTestingUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Run all tests in the supplied test classes according to the policies of
 * the supplied {@link Computer}, using the {@link Runner} configured via
 * {@link RunWith @RunWith} or the default JUnit runner, and assert the
 * expectations of the test execution.
 *
 * <p>To have all tests executed in parallel, supply {@link ParallelComputer#methods()}
 * as the {@code Computer}. To have all tests executed serially, supply
 * {@link Computer#serial()} as the {@code Computer}.
 *
 * @param computer the JUnit {@code Computer} to use
 * @param expectedStartedCount the expected number of tests that started
 * @param expectedFailedCount the expected number of tests that failed
 * @param expectedFinishedCount the expected number of tests that finished
 * @param expectedIgnoredCount the expected number of tests that were ignored
 * @param expectedAssumptionFailedCount the expected number of tests that
 * resulted in a failed assumption
 * @param testClasses one or more test classes to run
 */
public static void runTestsAndAssertCounters(Computer computer, int expectedStartedCount, int expectedFailedCount,
		int expectedFinishedCount, int expectedIgnoredCount, int expectedAssumptionFailedCount,
		Class<?>... testClasses) throws Exception {

	JUnitCore junit = new JUnitCore();
	TrackingRunListener listener = new TrackingRunListener();
	junit.addListener(listener);
	junit.run(computer, testClasses);

	// @formatter:off
	assertAll(
		() -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started"),
		() -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed"),
		() -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished"),
		() -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored"),
		() -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions")
	);
	// @formatter:on
}
 
Example 7
Source File: TestRunner.java    From Concurnas with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	JUnitCore core = new JUnitCore();
	TestListener listener = new TestListener();
       core.addListener(listener);

       System.out.println("Starting to run tests...");
       
       //core.run(new ClassRequest(AllTests.class));
       core.run(new ClassRequest(BytecodeTestJustCopier.class));
       
	System.exit(listener.failed);//0 is ok, >1 is fail
}
 
Example 8
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test @Suppress when all methods have been filtered */
@Test
public void testSuppress_all() {
  Request request =
      builder
          .addTestClass(SampleAllSuppressed.class.getName())
          .addTestClass(SampleJUnit3Suppressed.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  MyRunListener l = new MyRunListener();
  testRunner.addListener(l);
  Result r = testRunner.run(request);
  Assert.assertEquals(2, r.getRunCount());
  Assert.assertEquals(2, l.testCount);
}
 
Example 9
Source File: GuidedFuzzing.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Runs the guided fuzzing loop for a resolved class.
 *
 * <p>The test class must be annotated with <tt>@RunWith(JQF.class)</tt>
 * and the test method must be annotated with <tt>@Fuzz</tt>.</p>
 *
 * <p>Once this method is invoked, the guided fuzzing loop runs continuously
 * until the guidance instance decides to stop by returning <tt>false</tt>
 * for {@link Guidance#hasInput()}. Until the fuzzing stops, this method
 * cannot be invoked again (i.e. at most one guided fuzzing can be running
 * at any time in a single JVM instance).</p>
 *
 * @param testClass     the test class containing the test method
 * @param testMethod    the test method to execute in the fuzzing loop
 * @param guidance      the fuzzing guidance
 * @param out           an output stream to log Junit messages
 * @throws IllegalStateException if a guided fuzzing run is currently executing
 * @return the Junit-style test result
 */
public synchronized static Result run(Class<?> testClass, String testMethod,
                                      Guidance guidance, PrintStream out) throws IllegalStateException {

    // Ensure that the class uses the right test runner
    RunWith annotation = testClass.getAnnotation(RunWith.class);
    if (annotation == null || !annotation.value().equals(JQF.class)) {
        throw new IllegalArgumentException(testClass.getName() + " is not annotated with @RunWith(JQF.class)");
    }


    // Set the static guided instance
    setGuidance(guidance);

    // Register callback
    SingleSnoop.setCallbackGenerator(guidance::generateCallBack);

    // Create a JUnit Request
    Request testRequest = Request.method(testClass, testMethod);

    // Instantiate a runner (may return an error)
    Runner testRunner = testRequest.getRunner();

    // Start tracing for the test method
    SingleSnoop.startSnooping(testClass.getName() + "#" + testMethod);

    // Run the test and make sure to de-register the guidance before returning
    try {
        JUnitCore junit = new JUnitCore();
        if (out != null) {
            junit.addListener(new TextListener(out));
        }
        return junit.run(testRunner);
    } finally {
        unsetGuidance();
    }



}
 
Example 10
Source File: JUnitRunner.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * args[0] test class
 * args[1] test method (optional)
 * 
 * @param args
 */
public static void main(String[] args) {

  if (args.length < 1 || args.length > 2) {
    System.err.println("Usage: java -cp .:JUnitRunner-0.0.1-SNAPSHOT.jar:<project cp> uk.ac.shef.JUnitRunner <full test class name> [test method name]");
    System.exit(-1);
  }

  Class<?> clazz = null;
  try {
    clazz = Class.forName(args[0], false, JUnitRunner.class.getClassLoader());
  } catch (ClassNotFoundException e) {
    e.printStackTrace();
    System.exit(-1);
  }

  Request request = null;
  if (args.length == 1) {
    request = Request.aClass(clazz);
  } else if (args.length == 2) {
    request = Request.method(clazz, args[1]);
  }

  JUnitListener listener = new JUnitListener();

  JUnitCore runner = new JUnitCore();
  runner.addListener(listener);
  runner.run(request); // run test method

  System.exit(0);
}
 
Example 11
Source File: JUnitTestingUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Run the tests in the supplied {@code testClass}, using the specified
 * {@link Runner}, and assert the expectations of the test execution.
 *
 * <p>If the specified {@code runnerClass} is {@code null}, the tests
 * will be run with the runner that the test class is configured with
 * (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
 *
 * @param runnerClass the explicit runner class to use or {@code null}
 * if the default JUnit runner should be used
 * @param testClass the test class to run with JUnit
 * @param expectedStartedCount the expected number of tests that started
 * @param expectedFailedCount the expected number of tests that failed
 * @param expectedFinishedCount the expected number of tests that finished
 * @param expectedIgnoredCount the expected number of tests that were ignored
 * @param expectedAssumptionFailedCount the expected number of tests that
 * resulted in a failed assumption
 */
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
		int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
		int expectedAssumptionFailedCount) throws Exception {

	TrackingRunListener listener = new TrackingRunListener();

	if (runnerClass != null) {
		Constructor<?> constructor = runnerClass.getConstructor(Class.class);
		Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
		RunNotifier notifier = new RunNotifier();
		notifier.addListener(listener);
		runner.run(notifier);
	}
	else {
		JUnitCore junit = new JUnitCore();
		junit.addListener(listener);
		junit.run(testClass);
	}

	// @formatter:off
	assertAll(
		() -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started for [" + testClass + "]"),
		() -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed for [" + testClass + "]"),
		() -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished for [" + testClass + "]"),
		() -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored for [" + testClass + "]"),
		() -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions for [" + testClass + "]")
	);
	// @formatter:on
}
 
Example 12
Source File: JUnitRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result call() throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
    JUnitCore runner = new JUnitCore();
    runner.addListener(listener);
    Class<?>[] testClasses = testClassesFromCustomClassLoader();
    try {
        return runner.run(testClasses);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
 
Example 13
Source File: JUnitSingleTestRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result call() throws Exception {
    JUnitCore runner = new JUnitCore();
    runner.addListener(listener);
    Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
    try {
        return runner.run(request);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}
 
Example 14
Source File: JUnitSingleTestResultRunner.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Result call() throws Exception {
	JUnitCore runner = new JUnitCore();
	runner.addListener(listener);
	Request request = Request.method(testClassFromCustomClassLoader(), testCaseName());
	return runner.run(request);
}
 
Example 15
Source File: HashemTestRunner.java    From mr-hashemi with Universal Permissive License v1.0 5 votes vote down vote up
public static void runInMain(Class<?> testClass, String[] args) throws InitializationError, NoTestsRemainException {
    JUnitCore core = new JUnitCore();
    core.addListener(new TextListener(System.out));
    HashemTestRunner suite = new HashemTestRunner(testClass);
    if (args.length > 0) {
        suite.filter(new NameFilter(args[0]));
    }
    Result r = core.run(suite);
    if (!r.wasSuccessful()) {
        System.exit(1);
    }
}
 
Example 16
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Runs the test request and gets list of test methods run */
private static ArrayList<String> runRequest(Request request) {
  JUnitCore testRunner = new JUnitCore();
  RecordingRunListener listener = new RecordingRunListener();
  testRunner.addListener(listener);
  testRunner.run(request);
  return listener.methods;
}
 
Example 17
Source File: JUnitTestingUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Run the tests in the supplied {@code testClass}, using the specified
 * {@link Runner}, and assert the expectations of the test execution.
 *
 * <p>If the specified {@code runnerClass} is {@code null}, the tests
 * will be run with the runner that the test class is configured with
 * (i.e., via {@link RunWith @RunWith}) or the default JUnit runner.
 *
 * @param runnerClass the explicit runner class to use or {@code null}
 * if the default JUnit runner should be used
 * @param testClass the test class to run with JUnit
 * @param expectedStartedCount the expected number of tests that started
 * @param expectedFailedCount the expected number of tests that failed
 * @param expectedFinishedCount the expected number of tests that finished
 * @param expectedIgnoredCount the expected number of tests that were ignored
 * @param expectedAssumptionFailedCount the expected number of tests that
 * resulted in a failed assumption
 */
public static void runTestsAndAssertCounters(Class<? extends Runner> runnerClass, Class<?> testClass,
		int expectedStartedCount, int expectedFailedCount, int expectedFinishedCount, int expectedIgnoredCount,
		int expectedAssumptionFailedCount) throws Exception {

	TrackingRunListener listener = new TrackingRunListener();

	if (runnerClass != null) {
		Constructor<?> constructor = runnerClass.getConstructor(Class.class);
		Runner runner = (Runner) BeanUtils.instantiateClass(constructor, testClass);
		RunNotifier notifier = new RunNotifier();
		notifier.addListener(listener);
		runner.run(notifier);
	}
	else {
		JUnitCore junit = new JUnitCore();
		junit.addListener(listener);
		junit.run(testClass);
	}

	// @formatter:off
	assertAll(
		() -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started for [" + testClass + "]"),
		() -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed for [" + testClass + "]"),
		() -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished for [" + testClass + "]"),
		() -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored for [" + testClass + "]"),
		() -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions for [" + testClass + "]")
	);
	// @formatter:on
}
 
Example 18
Source File: PayloadsTest.java    From ysoserial with MIT License 5 votes vote down vote up
public static void main(String[] args) {

        JUnitCore junit = new JUnitCore();
        PayloadListener listener = new PayloadListener();
        junit.addListener(listener);
        Result result = junit.run(PayloadsTest.class);
        System.exit(result.wasSuccessful() ? 0 : 1);
    }
 
Example 19
Source File: ExpectedExceptionTest.java    From hamcrest-junit with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void runTestAndVerifyResult() {
    EventCollector collector = new EventCollector();
    JUnitCore core = new JUnitCore();
    core.addListener(collector);
    core.run(classUnderTest);
    assertThat(collector, matcher);
}
 
Example 20
Source File: TestExecutor.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Initialize listeners and add them to the JUnitCore runner */
private void setUpListeners(JUnitCore testRunner) {
  for (RunListener listener : listeners) {
    Log.d(LOG_TAG, "Adding listener " + listener.getClass().getName());
    testRunner.addListener(listener);
    if (listener instanceof InstrumentationRunListener) {
      ((InstrumentationRunListener) listener).setInstrumentation(instr);
    }
  }
}