org.junit.runner.JUnitCore Java Examples

The following examples show how to use org.junit.runner.JUnitCore. 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: GenericRunner.java    From vividus with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    Class<?> clazz = MethodHandles.lookup().lookupClass();
    Result result = new JUnitCore().run(clazz);
    int exitCode;
    if (result.getFailureCount() > 0)
    {
        Logger logger = LoggerFactory.getLogger(clazz);
        result.getFailures().forEach(f ->
        {
            logger.error("Failure: {}", f);
            logger.error(f.getTrace());
        });
        exitCode = ERROR_EXIT_CODE;
    }
    else
    {
        exitCode = calculateExitCode(BeanFactory.getBean(IRunStatusProvider.class).getRunStatus());
    }
    System.exit(exitCode);
}
 
Example #3
Source File: TestRunner.java    From codeu_project_2017 with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
  final Result result =
      JUnitCore.runClasses(
          codeu.chat.common.SecretTest.class,
          codeu.chat.relay.ServerTest.class,
          codeu.chat.server.BasicControllerTest.class,
          codeu.chat.server.RawControllerTest.class,
          codeu.chat.util.TimeTest.class,
          codeu.chat.util.UuidTest.class,
          codeu.chat.util.store.StoreTest.class
      );
   for (final Failure failure : result.getFailures()) {
      System.out.println(failure.toString());
   }
   System.out.println(result.wasSuccessful());
}
 
Example #4
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 #5
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Test provided multiple annotations to exclude. */
@Test
public void testTestSizeFilter_multipleNotAnnotation() {
  Request request =
      builder
          .addAnnotationExclusionFilter(SmallTest.class.getName())
          .addAnnotationExclusionFilter(MediumTest.class.getName())
          .addTestClass(SampleMultipleAnnotation.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  // expect 1 test that failed
  Assert.assertEquals(1, result.getRunCount());
  Assert.assertEquals(
      "testRunThis", result.getFailures().get(0).getDescription().getMethodName());
}
 
Example #6
Source File: TestExecutor.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Execute the tests */
public Bundle execute(Request request) {
  Bundle resultBundle = new Bundle();
  Result junitResults = new Result();
  try {
    JUnitCore testRunner = new JUnitCore();
    setUpListeners(testRunner);
    junitResults = testRunner.run(request);
  } catch (Throwable t) {
    final String msg = "Fatal exception when running tests";
    Log.e(LOG_TAG, msg, t);
    junitResults.getFailures().add(new Failure(Description.createSuiteDescription(msg), t));
  } finally {
    ByteArrayOutputStream summaryStream = new ByteArrayOutputStream();
    // create the stream used to output summary data to the user
    PrintStream summaryWriter = new PrintStream(summaryStream);
    reportRunEnded(listeners, summaryWriter, resultBundle, junitResults);
    summaryWriter.close();
    resultBundle.putString(
        Instrumentation.REPORT_KEY_STREAMRESULT, String.format("\n%s", summaryStream.toString()));
  }
  return resultBundle;
}
 
Example #7
Source File: TestExceptionInBeforeClassHooks.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionWithinTestFailsTheTest() {
  Result runClasses = JUnitCore.runClasses(Nested2.class);
  assertFailureCount(3, runClasses);
  Assert.assertEquals(3, runClasses.getRunCount());
  
  ArrayList<String> foobars = new ArrayList<>();
  for (Failure f : runClasses.getFailures()) {
    Matcher m = Pattern.compile("foobar[0-9]+").matcher(f.getTrace());
    while (m.find()) {
      foobars.add(m.group());
    }
  }

  Collections.sort(foobars);
  Assert.assertEquals("[foobar1, foobar2, foobar3]", 
      Arrays.toString(foobars.toArray()));
}
 
Example #8
Source File: ContextHierarchyDirtiesContextTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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));
}
 
Example #9
Source File: ProjectPersistTest.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
@Test
public void should_store_project_under_test_to_two_separated_directories_when_the_same_test_fails_twice_in_a_row() throws IOException {
    final Result firstRun = JUnitCore.runClasses(ProjectPersistFail.class);
    final Result secondRun = JUnitCore.runClasses(ProjectPersistFail.class);

    assertThat(firstRun.wasSuccessful()).isFalse();
    assertThat(secondRun.wasSuccessful()).isFalse();
    // it's rather shallow check, but if it's not equal it means second execution of the test failed for different reasons
    assertThat(secondRun.getFailures()).hasSameSizeAs(firstRun.getFailures());

    assertThat(findPersistedProjects("repo.bundle", "ProjectPersistFail_should_fail")).hasSize(2);
}
 
Example #10
Source File: TestRunner.java    From JSON-Java-unit-test with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
   Result result = JUnitCore.runClasses(JunitTestSuite.class);
   for (Failure failure : result.getFailures()) {
      System.out.println(failure.toString());
   }
   System.out.println(result.wasSuccessful());
}
 
Example #11
Source File: ParameterStatementTest.java    From neodymium-library with MIT License 5 votes vote down vote up
@Test
public void testGeneratorCanNotSetFinalField()
{
    // test that a final field can not be set
    Result result = JUnitCore.runClasses(GeneratorCanNotSetFinalField.class);
    checkFail(result, 1, 0, 1, "Could not set parameter due to it is not public or it is final");
}
 
Example #12
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that inclusion filter is filtering out all other tests in the same class and leaves the
 * rest of the inclusion filters
 */
@Test
public void testMultipleMethodInclusions() {
  Request request =
      builder
          .addTestClass(SampleTwoTestsClass.class.getName())
          .addTestMethod(SampleThreeTestsClass.class.getName(), "test1of3")
          .addTestMethod(SampleThreeTestsClass.class.getName(), "test3of3")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(4, result.getRunCount());
}
 
Example #13
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test filtering by two methods in single class */
@Test
public void testMultipleMethodsFilter() {
  Request request =
      builder
          .addTestMethod(SampleJUnit3Test.class.getName(), "testSmall")
          .addTestMethod(SampleJUnit3Test.class.getName(), "testSmall2")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
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: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnit3Suite_IgnoreSuiteMethodsFlagSet_IgnoresSuiteMethods() {
  Request request =
      builder.addTestClass(JUnit3Suite.class.getName()).ignoreSuiteMethods(true).build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(1, result.getRunCount());
}
 
Example #16
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that a JUnit 3 suite method that returns a TestCase is executed when skipExecution =
 * false.
 */
@Test
public void testNoSkipExecution_JUnit3SuiteMethod_ReturnsTestCase() {
  Request request =
      builder.addTestClass(JUnit3SuiteMethod_ReturnsTestCase.class.getName()).build();
  JUnitCore testRunner = new JUnitCore();
  // Differs from standard JUnit behavior; a suite() method can return any implementation of
  // junit.framework.Test not just TestSuite.
  ensureAllTestsFailed(
      testRunner.run(request),
      1,
      JUnit3SuiteMethod_ReturnsTestCase.class.getName() + "#suite() did not return a TestSuite");
}
 
Example #17
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that a JUnit 3 TestCase is executed when skipExecution = false. */
@Test
public void testNoSkipExecution_JUnit3TestCase() {
  Request request = builder.addTestClass(JUnit3FailingTestCase.class.getName()).build();
  JUnitCore testRunner = new JUnitCore();
  ensureAllTestsFailed(testRunner.run(request), 1, "broken");
}
 
Example #18
Source File: ProjectPersistUsingPropertyTest.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
@Test
public void should_store_project_under_test_directory_when_test_is_passing_but_property_is_set() throws IOException {
    System.setProperty("test.bed.project.persist", "true");

    final Result result = JUnitCore.runClasses(ProjectPersistAnotherPass.class);

    assertThat(result.wasSuccessful()).isTrue();
    assertThat(findPersistedProjects("repo.bundle", "ProjectPersistAnotherPass_should_pass")).hasSize(1);
}
 
Example #19
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 #20
Source File: PtlWebDriverManagerTest.java    From hifive-pitalium with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 全クラスでWebDriverを再利用するテスト。<br />
 * {@link PtlWebDriverStrategy#sessionLevel()}が設定されていない、またはデフォルト値の場合全体で再利用される。
 * </p>
 * 以下が同じWebDriverのインスタンスIDになる。
 * <ul>
 * <li>NO_ANNOTATED_1 - 1,2 / NO_ANNOTATED_2 - 1,2 / ANNOTATED_ONLY - 1,2 / ANNOTATED_USE_CONFIG - 1,2 /
 * ANNOTATED_GLOBAL - 1,2</li>
 * <li>ANNOTATED_TEST_CLASS - 1,2</li>
 * <li>ANNOTATED_TEST_CASE - 1</li>
 * <li>ANNOTATED_TEST_CASE - 2</li>
 * </ul>
 */
@Test
public void sessionLevelGlobal() throws Exception {
	PtlWebDriverManager.getInstance().resetCache(WebDriverSessionLevel.GLOBAL);
	JUnitCore.runClasses(TEST_CLASSES);

	for (Map<String, Integer> instanceIds : driverInstanceIds.values()) {
		assertThat(instanceIds.size(), is(14));
		assertThat(new HashSet<Integer>(instanceIds.values()).size(), is(4));

		// NO_ANNOTATED_1 - 1,2 / NO_ANNOTATED_2 - 1,2 / ANNOTATED_ONLY - 1,2 /
		// ANNOTATED_USE_CONFIG - 1,2 / ANNOTATED_GLOBAL - 1,2
		String[] classIds = { NO_ANNOTATION_1, NO_ANNOTATION_2, ANNOTATED_ONLY, ANNOTATED_USE_CONFIG,
				ANNOTATED_GLOBAL };

		int expect1 = instanceIds.get(NO_ANNOTATION_1 + TEST_1);
		for (String classId : classIds) {
			assertThat(instanceIds.get(classId + TEST_1), is(expect1));
			assertThat(instanceIds.get(classId + TEST_2), is(expect1));
		}

		// ANNOTATED_TEST_CLASS - 1,2
		int expect2 = instanceIds.get(ANNOTATED_TEST_CLASS + TEST_1);
		assertThat(expect2, is(not(expect1)));
		assertThat(instanceIds.get(ANNOTATED_TEST_CLASS + TEST_2), is(expect2));

		// ANNOTATED_TEST_CASE - 1,2
		int testCase1 = instanceIds.get(ANNOTATED_TEST_CASE + TEST_1);
		int testCase2 = instanceIds.get(ANNOTATED_TEST_CASE + TEST_2);
		assertThat(testCase1, is(not(expect1)));
		assertThat(testCase1, is(not(expect2)));
		assertThat(testCase1, is(not(testCase2)));
	}
}
 
Example #21
Source File: DataUtilsTest.java    From neodymium-library with MIT License 5 votes vote down vote up
@Test
public void testDataUtils() throws Exception
{
    // test the data utils
    Result result = JUnitCore.runClasses(DataUtilsTests.class);
    checkPass(result, 9, 0, 0);
}
 
Example #22
Source File: PtlWebDriverCloserTest.java    From hifive-pitalium with Apache License 2.0 5 votes vote down vote up
@Test
public void globalTest() throws Exception {
	JUnitCore.runClasses(GlobalTestCase.class);

	for (WebDriver driver : drivers.values()) {
		driver.getWindowHandle();
	}
}
 
Example #23
Source File: NeodymiumContextTest.java    From neodymium-library with MIT License 5 votes vote down vote up
@Test
public void testContextGetCleared() throws Exception
{
    // test that NeodymiumRunner clears the context before each run
    Result result = JUnitCore.runClasses(ContextGetsCleared.class);
    checkPass(result, 2, 0, 0);
}
 
Example #24
Source File: DynamicClassCompilerTest.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void accessProtectedMethodFromDifferentClassloaderButSamePackageName() throws ClassNotFoundException {
	String qualifiedName = "test.dynamic.compiler.HelloWorld";
	String qualifiedTestName = "test.dynamic.compiler.HelloWorldTest";
	String code = 
			"package test.dynamic.compiler;" +
			"public class HelloWorld {" +
			"	protected String message() {" +
			"		return \"Hello World!\";" +
			"	}" + 
			"}";
	String testCode = 
			"package test.dynamic.compiler;" +
			"import org.junit.Test;" +
			"import static org.junit.Assert.assertEquals;" +
			"public class HelloWorldTest {" +
			"	@Test" +
			"	public void protectedMethodTest() {" +
			"		assertEquals(\"Hello World!\", new HelloWorld().message());" +
			"	}" + 
			"}";
	Map<String, String> sources = adHocMap(asList(qualifiedName, qualifiedTestName), asList(code, testCode));
	ClassLoader parentLoader = BytecodeClassLoaderBuilder.loaderFor(qualifiedName, code);
	ClassLoader loader = BytecodeClassLoaderBuilder.loaderFor(sources, parentLoader);
	Class<?> testClass = loader.loadClass(qualifiedTestName);
	Class<?> theClass = loader.loadClass(qualifiedName);
	assertFalse(parentLoader == loader);
	assertTrue(loader == theClass.getClassLoader());
	assertTrue(loader == testClass.getClassLoader());
	JUnitCore junit = new JUnitCore();
	Request request = Request.method(testClass, "protectedMethodTest");
	Result result = junit.run(request);
	assertTrue(result.wasSuccessful());
}
 
Example #25
Source File: PtlBlockJUnit4ClassRunnerWithParametersTest.java    From hifive-pitalium with Apache License 2.0 5 votes vote down vote up
/**
 * {@link ParameterizedBeforeClass}を設定したメソッドのバリデーションテスト
 */
@Test
public void testValidateParameterizedBeforeClass() throws Exception {
	Result result = JUnitCore.runClasses(ValidateParameterizedBeforeClass.class);
	assertThat(result.getFailureCount(), is(3));

	for (Failure failure : result.getFailures()) {
		assertThat(
				failure.getMessage(),
				is(anyOf(equalTo("Method beforeClass() should be static"),
						equalTo("Method beforeClass() should be public"),
						//							equalTo("Method beforeClass() should have 1 parameters"),
						equalTo("Method beforeClass() should be void"))));
	}
}
 
Example #26
Source File: MutantSearchSpaceExplorator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
private static void checkThatAllTestsPass(Class<?> TEST_CLASS) {
	resetAllSelectors();
	JUnitCore core = new JUnitCore();
	Result result = core.run(TEST_CLASS);
	if (result.getFailureCount()>0) {
		// oops the tests are not valid before
		throw new RuntimeException("oops it does not pass anymore", result.getFailures().get(0).getException());
	}
}
 
Example #27
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that @RunWith(JUnit4.class) annotated test is run when skipExecution = false. */
@Test
public void testNoSkipExecution_RunWithJUnit4() {
  Request request = builder.addTestClass(RunWithJUnit4Failing.class.getName()).build();
  JUnitCore testRunner = new JUnitCore();
  ensureAllTestsFailed(testRunner.run(request), 1, "broken");
}
 
Example #28
Source File: NeodymiumWebDriverTest.java    From neodymium-library with MIT License 5 votes vote down vote up
@Test
public void testValidateClearReuseWebDriverCache()
{
    // XVFB or a display needed
    Result result = JUnitCore.runClasses(ValidateClearReuseWebDriverCache.class);
    checkPass(result, 2, 0, 0);
}
 
Example #29
Source File: JUnit4CategoryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** This test is mentioned in {@code Categories} and any changes must be reflected. */
@Test
public void runSlowTestSuite() {
  // Targeting Test:
  Result testResult = JUnitCore.runClasses(SlowTestSuite.class);

  assertThat("unexpected run count", testResult.getRunCount(), is(equalTo(2)));
  assertThat("unexpected failure count", testResult.getFailureCount(), is(equalTo(0)));
  assertThat("unexpected failure count", testResult.getIgnoreCount(), is(equalTo(0)));
}
 
Example #30
Source File: DataUtilsTest.java    From neodymium-library with MIT License 5 votes vote down vote up
@Test
public void testDataUtilsXml() throws Exception
{
    // test the data utils
    Result result = JUnitCore.runClasses(DataUtilsTestsXml.class);
    checkPass(result, 9, 0, 0);
}