org.junit.runners.JUnit4 Java Examples

The following examples show how to use org.junit.runners.JUnit4. 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: IsolatedTestCaseRunner.java    From tlaplus with MIT License 6 votes vote down vote up
public IsolatedTestCaseRunner(final Class<?> testFileClass)
		throws InitializationError, ClassNotFoundException, InstantiationException, IllegalAccessException,
		IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

	// Since IsolatedTestCaseRunner runs several isolated tests in a single VM, it
	// is good practice to clean resources before each new test.
	System.gc();
	
	ClassLoader classLoader = IsolatedTestCaseRunner.class.getClassLoader();
	if (classLoader instanceof URLClassLoader) {
		// When run within e.g. Eclipse, the classloader might not be of instance
		// URLClassLoader. In this case, just use the provided class loader which won't
		// isolate tests. A set of tests can thus only be run in a single VM from ant
		// or maven which pass a URLClassLoader.
		classLoader = new IsolatedTestCaseClassLoader((URLClassLoader) classLoader);
	}
	delegate = new JUnit4(classLoader.loadClass(testFileClass.getName()));
}
 
Example #2
Source File: JavaProcessTest.java    From ipc-eventbus with Apache License 2.0 6 votes vote down vote up
@Test
public void test_launch_java_process() throws InterruptedException {
  JavaProcess proc = JavaProcess.newBuilder()
      .mainClass(Echo.class.getName())
      .addClasspath(Echo.class)
      .addClasspath(ThreadUtil.class)
      .addClasspath(JUnit4.class)
      .arguments("one", "two")
      .addJvmProp("my.prop", "world")
      .addJvmArg("-Xmx512m")
      .env("VAR", "Hello")
      .pipeStdout()
      .pipeStderr()
      .recordStdout()
      .recordStderr()
      .build();

  System.out.println(proc.getCommand());
  assertEquals(0, proc.waitFor());
  assertEquals("Hello\n" +
      "world\n" +
      "one\n" +
      "two\n", proc.getRecordedStdoutText());
  assertEquals("", proc.getRecordedStderrText());
}
 
Example #3
Source File: JavaProcessTest.java    From ipc-eventbus with Apache License 2.0 6 votes vote down vote up
@Test
public void test_launch_failing_java_process() throws InterruptedException {
  JavaProcess proc = JavaProcess.newBuilder()
      .mainClass(EchoFail.class.getName())
      .addClasspath(EchoFail.class)
      .addClasspath(ThreadUtil.class)
      .addClasspath(JUnit4.class)
      .arguments("one", "two")
      .addJvmProp("my.prop", "world")
      .addJvmArg("-Xmx512m")
      .env("VAR", "Hello")
      .recordStdout()
      .recordStderr()
      .build();

  System.out.println(proc.getCommand());
  assertEquals(1, proc.waitFor());
  assertEquals("Hello\n" +
      "world\n" +
      "one\n" +
      "two\n", proc.getRecordedStdoutText());
  assertTrue(proc.getRecordedStderrText().contains("Exception in thread \"main\" java.lang.AssertionError: message"));
}
 
Example #4
Source File: SquidbTestRunner.java    From squidb with Apache License 2.0 6 votes vote down vote up
/**
 * @return true if {@param cls} is {@link JUnit4} annotated.
 */
protected boolean isJUnit4TestClass(Class cls) {
    // Need to find test classes, otherwise crashes with b/11790448.
    if (!cls.getName().endsWith("Test")) {
        return false;
    }
    // Check the annotations.
    Annotation annotation = cls.getAnnotation(RunWith.class);
    if (annotation != null) {
        RunWith runWith = (RunWith) annotation;
        Object value = runWith.value();
        if (value.equals(JUnit4.class) || value.equals(Suite.class)) {
            return true;
        }
    }
    return false;
}
 
Example #5
Source File: TestManager.java    From RestServices with Apache License 2.0 5 votes vote down vote up
public List<String> findJUnitTests(TestSuite testSuite)
{
	List<String> junitTests = new ArrayList<String>(); 
	try {
		Class<?>[] clazzez = getUnitTestClasses(testSuite);

		if (clazzez != null && clazzez.length > 0) { 
			for (Class<?> clazz : clazzez) {
				
				//From https://github.com/KentBeck/junit/blob/master/src/main/java/org/junit/runners/BlockJUnit4ClassRunner.java method computeTestMethods
				try {
					List<FrameworkMethod> methods =	new JUnit4(clazz).getTestClass().getAnnotatedMethods(Test.class);
					
					if (methods != null && !methods.isEmpty()) 
						for (FrameworkMethod method: methods)
							junitTests.add(clazz.getName() + "/" + method.getName());
				}
				catch(InitializationError e2) {
					StringBuilder errors = new StringBuilder();
					
					for(Throwable cause : e2.getCauses())
						errors.append("\n").append(cause.getMessage());
					
					LOG.error("Failed to recognize class '" + clazz + "' as unitTestClass: " + errors.toString());
				}
			}
		}
	}
	catch(Exception e) {
		LOG.error(CLOUD_SECURITY_ERROR + e.getMessage(), e);
	}
	return junitTests;
}
 
Example #6
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailingTestLogsCorrectly() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(PartiallyFailingThenPassingTest.class));

  assertTrue(result.wasSuccessful());
  assertThat(result.getFailureCount(), is(0));
  assertThat(result.getRunCount(), is(6));
}
 
Example #7
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEffectivelyEmptyTestIsSafe() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(EffectivelyEmptyTest.class));

  assertTrue(result.wasSuccessful());
  assertThat(result.getIgnoreCount(), is(1));
  assertThat(result.getRunCount(), is(0));
  assertThat(result.getFailureCount(), is(0));
}
 
Example #8
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingRuleAnnotationTriggersFailure() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(MissingRuleAnnotation.class));

  assertFalse(result.wasSuccessful());
  assertThat(result.getRunCount(), is(1));
  assertThat(result.getFailureCount(), is(1));
  Throwable throwable = result.getFailures().get(0).getException();
  assertThat(throwable, hasMessage(is("TestRetryer must be annotated with both @ClassRule and @Rule")));
}
 
Example #9
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMissingClassRuleAnnotationTriggersFailure() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(MissingClassRuleAnnotation.class));

  assertFalse(result.wasSuccessful());
  assertThat(result.getRunCount(), is(0));
  assertThat(result.getFailureCount(), is(1));
  Throwable throwable = result.getFailures().get(0).getException();
  assertThat(throwable, hasMessage(is("TestRetryer must be annotated with both @ClassRule and @Rule")));
}
 
Example #10
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testRetryAndPassOnEventualSuccess() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(EventualSuccess.class));

  assertTrue(result.wasSuccessful());
  assertThat(result.getFailureCount(), is(0));
  assertThat(result.getRunCount(), is(4));
}
 
Example #11
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoRetryOnImmediateSuccess() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(ImmediateSuccess.class));
  assertTrue(result.wasSuccessful());
  assertThat(result.getFailureCount(), is(0));
  assertThat(result.getRunCount(), is(1));
}
 
Example #12
Source File: TestRetryerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test @SuppressWarnings("unchecked")
public void testExceptionsSuppressProperly() throws InitializationError {
  Result result = new JUnitCore().run(new JUnit4(RepeatedFailure.class));

  assertThat(result.getFailureCount(), is(1));

  Throwable exception = result.getFailures().get(0).getException();
  assertThat(exception, hasMessage(equalTo("Failed: 4")));
  assertThat(exception.getSuppressed(), array(hasMessage(equalTo("Failed: 3"))));
  assertThat(exception.getSuppressed()[0].getSuppressed(), array(hasMessage(equalTo("Failed: 2"))));
  assertThat(exception.getSuppressed()[0].getSuppressed()[0].getSuppressed(), array(hasMessage(equalTo("Failed: 1"))));
}
 
Example #13
Source File: IgnoreCoreClassesTest.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldIgnoreJUnitClasses() {
  assertIgnored(JUnit4.class);
}
 
Example #14
Source File: RepeatedSpringRuleTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #15
Source File: FailingBeforeAndAfterMethodsSpringRuleTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #16
Source File: TimedSpringRuleTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #17
Source File: RepeatedSpringRuleTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #18
Source File: FailingBeforeAndAfterMethodsSpringRuleTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #19
Source File: TimedSpringRuleTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #20
Source File: RepeatedSpringRuleTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #21
Source File: FailingBeforeAndAfterMethodsSpringRuleTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}
 
Example #22
Source File: TimedSpringRuleTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected Class<? extends Runner> getRunnerClass() {
	return JUnit4.class;
}