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

The following examples show how to use org.junit.runner.JUnitCore#run() . 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: TimeoutTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
/** Ensure that the combination of timing out and passing tests are all reported correctly */
@Test
public void testTimeoutInJUnit3Style() {
  Request request =
      builder
          .addTestClass(JUnit3StyleTimeoutClass.class.getName())
          .setPerTestTimeout(JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT)
          .build();
  JUnitCore junitCore = new JUnitCore();
  Result result = junitCore.run(request);
  assertThat(result.getFailures()).hasSize(3);
  assertThat(getFailureMessages(result.getFailures()))
      .containsExactly(
          String.format(
              "Test timed out after %s milliseconds", JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT),
          String.format(
              "Test timed out after %s milliseconds", JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT),
          String.format(
              "Test timed out after %s milliseconds",
              JUnit3StyleTimeoutClass.GLOBAL_ARG_TIMEOUT));
}
 
Example 2
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, with super class. */
@Test
public void testSuppress_withSizeAndSuper() {
  Request request =
      builder
          .addTestClass(SampleJUnit3SuppressedWithSuper.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 3
Source File: PropertyIndexingTests.java    From hypergraphdb with Apache License 2.0 6 votes vote down vote up
public static void main(String []argv)
{
	JUnitCore junit = new JUnitCore();
	Result result = null;
	do
	{
		result = junit.run(Request.method(PropertyIndexingTests.class, "derivedPropertyIndexing"));
	} while (result.getFailureCount() == 0 && false);
	System.out.println("Failures " + result.getFailureCount());
	if (result.getFailureCount() > 0)
	{
		for (Failure failure : result.getFailures())
		{
			failure.getException().printStackTrace();
		}
	}
	System.exit(0);
}
 
Example 4
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test that a custom RunnerBuilder is used. */
@Test
public void testCustomRunnerBuilder() {
  Request request =
      builder
          .addCustomRunnerBuilderClass(CustomRunnerBuilder.class)
          .addTestClass(BrokenRunnableTest.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  assertEquals("unexpected run count", 1, result.getRunCount());
  assertEquals("unexpected failure count", 1, result.getFailureCount());
  Failure failure = result.getFailures().get(0);
  assertEquals("unexpected failure", "Broken", failure.getMessage());
}
 
Example 5
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Test that annotation filtering by class works when methods are from superclass.
 *
 * <p>TODO: add a similar test to upstream junit.
 */
@Test
public void testAddAnnotationInclusionFilter_super() {
  Request request =
      builder
          .addAnnotationInclusionFilter(SmallTest.class.getName())
          .addTestClass(InheritedAnnnotation.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
Example 6
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test case where entire JUnit3 test class has been filtered out */
@Test
public void testSize_classFiltered() {
  Request request =
      builder
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestClass(SampleNoSize.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  MyRunListener l = new MyRunListener();
  JUnitCore testRunner = new JUnitCore();
  testRunner.addListener(l);
  testRunner.run(request);
  Assert.assertEquals(1, l.testCount);
}
 
Example 7
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 8
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify method filter does not filter out initialization errors */
@Test
public void testJUnit4FilterWithInitError() {
  Request request =
      builder
          .addTestMethod(JUnit4TestInitFailure.class.getName(), "testWillFailOnClassInit")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(1, result.getRunCount());
}
 
Example 9
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 10
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 11
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 12
Source File: DynamicClassCompilerTest.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void accessProtectedMethodFromSameClassloaderAndPackage() 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 loader = BytecodeClassLoaderBuilder.loaderFor(sources);
	Class<?> testClass = loader.loadClass(qualifiedTestName);
	Class<?> theClass = loader.loadClass(qualifiedName);
	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 13
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 14
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that including and excluding different classes leaves that class's methods. */
@Test
public void testClassAndNotClass_different() {
  Request request =
      builder
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .removeTestClass(SampleRunnerFilterClassSize.class.getName())
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
Example 15
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testBothMethodInclusionAndExclusion() {
  Request request =
      builder
          .addTestClass(SampleTwoTestsClass.class.getName())
          .removeTestMethod(SampleTwoTestsClass.class.getName(), "test1of2")
          .removeTestMethod(SampleThreeTestsClass.class.getName(), "test1of3")
          .addTestMethod(SampleThreeTestsClass.class.getName(), "test3of3")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
Example 16
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test filtering by two methods in separate classes */
@Test
public void testTwoMethodsDiffClassFilter() {
  Request request =
      builder
          .addTestMethod(SampleJUnit3Test.class.getName(), "testSmall")
          .addTestMethod(SampleRunnerFilterSizeTest.class.getName(), "testOther")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(2, result.getRunCount());
}
 
Example 17
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that filtering out all tests is not treated as an error */
@Test
public void testNoTests() {
  Request request =
      builder
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestSizeFilter(TestSize.MEDIUM)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(0, result.getRunCount());
}
 
Example 18
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Test size annotations with JUnit3 test methods */
@Test
public void testSize_junit3Method() {
  Request request =
      builder
          .addTestClass(SampleJUnit3Test.class.getName())
          .addTestClass(SampleNoSize.class.getName())
          .addTestSizeFilter(TestSize.SMALL)
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result r = testRunner.run(request);
  Assert.assertEquals(2, r.getRunCount());
}
 
Example 19
Source File: TestRequestBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/** Verify that filtering out all but one test in a class gives one test */
@Test
public void testClassAndMethod() {
  Request request =
      builder
          .addTestClass(SampleRunnerFilterSizeTest.class.getName())
          .addTestMethod(SampleRunnerFilterSizeTest.class.getName(), "testSmall")
          .build();
  JUnitCore testRunner = new JUnitCore();
  Result result = testRunner.run(request);
  Assert.assertEquals(1, result.getRunCount());
}
 
Example 20
Source File: DynamicClassCompilerTest.java    From nopol with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void accessPublicMethodFromDifferentClassloader() throws ClassNotFoundException {
	String qualifiedName = "test.dynamic.compiler.HelloWorld";
	String qualifiedTestName = "test.dynamic.compiler.HelloWorldTest";
	String code = 
			"package test.dynamic.compiler;" +
			"public class HelloWorld {" +
			"	@Override" +
			"	public String toString() {" +
			"		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 toStringTest() {" +
			"		assertEquals(\"Hello World!\", new HelloWorld().toString());" +
			"	}" + 
			"}";
	ClassLoader parentLoader = BytecodeClassLoaderBuilder.loaderFor(qualifiedName, code);
	Map<String, String> sources = adHocMap(asList(qualifiedName, qualifiedTestName), asList(code, testCode));
	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, "toStringTest");
	Result result = junit.run(request);
	assertTrue(result.wasSuccessful());
}