Java 类org.junit.runners.JUnit4 实例源码

项目:core-doppl    文件:DopplJunitTestHelper.java   
/**
 * @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;
}
项目:ipc-eventbus    文件:JavaProcessTest.java   
@Test
public void test_launch_java_process() throws InterruptedException {
  JavaProcess proc = JavaProcess.newBuilder()
      .mainClass(Echo.class.getName())
      .addClasspath(Echo.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());
}
项目:ipc-eventbus    文件:JavaProcessTest.java   
@Test
public void test_launch_failing_java_process() throws InterruptedException {
  JavaProcess proc = JavaProcess.newBuilder()
      .mainClass(EchoFail.class.getName())
      .addClasspath(EchoFail.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"));
}
项目:j2objc    文件:JUnitTestRunner.java   
/**
 * @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;
    if (runWith.value().equals(JUnit4.class)) {
      return true;
    }
  }
  return false;
}
项目:squidb    文件:SquidbTestRunner.java   
/**
 * @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;
}
项目:error-prone    文件:JUnit4SetUpNotRunTest.java   
@Test
public void noBeforeOnClasspath() throws Exception {
  File libJar = tempFolder.newFile("lib.jar");
  try (FileOutputStream fis = new FileOutputStream(libJar);
      JarOutputStream jos = new JarOutputStream(fis)) {
    addClassToJar(jos, RunWith.class);
    addClassToJar(jos, JUnit4.class);
    addClassToJar(jos, BlockJUnit4ClassRunner.class);
    addClassToJar(jos, ParentRunner.class);
    addClassToJar(jos, SuperTest.class);
    addClassToJar(jos, SuperTest.class.getEnclosingClass());
  }
  compilationHelper
      .addSourceLines(
          "Test.java",
          "import org.junit.runner.RunWith;",
          "import org.junit.runners.JUnit4;",
          "import " + SuperTest.class.getCanonicalName() + ";",
          "@RunWith(JUnit4.class)",
          "class Test extends SuperTest {",
          "  @Override public void setUp() {}",
          "}")
      .setArgs(Arrays.asList("-cp", libJar.toString()))
      .doTest();
}
项目:RedisConnector    文件:TestManager.java   
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;
}
项目:MqttClient    文件:TestManager.java   
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;
}
项目:database-connector    文件:TestManager.java   
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;
}
项目:quarantining-test-runner    文件:DelegateRunningToDiscoverer.java   
public Class<? extends Runner> getDelegateRunningToOn(Class<?> testClass)
{
    Class<? extends Runner> runnerClass = JUnit4.class;
    DelegateRunningTo annotation = testClass.getAnnotation(DelegateRunningTo.class);

    if(annotation != null)
    {
        runnerClass = annotation.value();
    }

    return runnerClass;
}
项目:quarantining-test-runner    文件:DelegateRunningToDiscovererTest.java   
@Test
public void annotatedClassWithoutValueReturnsDefault()
{
    DelegateRunningToDiscoverer discoverer = new DelegateRunningToDiscoverer();
    Class discovered = discoverer.getDelegateRunningToOn(FixtureWithoutValue.class);
    assertThat(discovered, equalTo((Class) JUnit4.class));
}
项目:quarantining-test-runner    文件:DelegateRunningToDiscovererTest.java   
@Test
public void unannotatedClassReturnsDefault()
{
    DelegateRunningToDiscoverer discoverer = new DelegateRunningToDiscoverer();
    Class discovered = discoverer.getDelegateRunningToOn(UnannotatedFixture.class);
    assertThat(discovered, equalTo((Class) JUnit4.class));
}
项目:RestServices    文件:TestManager.java   
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;
}
项目:fyodor    文件:TestRunner.java   
private void executeJunitTest(final Class<?> testClass) {
    try {
        new JUnit4(testClass).run(runNotifierWith(runListeners));
    } catch (final InitializationError initializationError) {
        throw new RuntimeException(initializationError);
    }
}
项目:UnitTesting    文件:TestManager.java   
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;
}
项目:spring4-understanding    文件:RepeatedSpringRuleTests.java   
@Override
protected Class<? extends Runner> getRunnerClass() {
    return JUnit4.class;
}
项目:spring4-understanding    文件:TimedSpringRuleTests.java   
@Override
protected Class<? extends Runner> getRunnerClass() {
    return JUnit4.class;
}
项目:spring4-understanding    文件:FailingBeforeAndAfterMethodsSpringRuleTests.java   
@Override
protected Class<? extends Runner> getRunnerClass() {
    return JUnit4.class;
}
项目:jicunit    文件:JicUnitRunner.java   
public JicUnitRunner(Class<?> testClass) throws Throwable {
  Class<? extends Runner> runnerClass;
  RunInContainerWith runInContainerWith = findAnnotation(testClass, RunInContainerWith.class);
  // figure out if this is happening locally or in the JEE container
  String containerUrl = getContainerUrl();
  if (containerUrl == null) {
    // this code is executed in the JEE container
    if (runInContainerWith != null) {
      runnerClass = runInContainerWith.value();
    } else {
      runnerClass = JUnit4.class;
    }
    try {
      Constructor<? extends Runner> constructor = runnerClass
          .getDeclaredConstructor(Class.class);
      mRunner = constructor.newInstance(testClass);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
        | InvocationTargetException | NoSuchMethodException | SecurityException e) {
      Throwable cause = (e.getCause() != null ? e.getCause() : e);
      String msg = "Unable to create instance of " + runnerClass + " using test class " + testClass.getName() + " Reason: " + cause;
      if (cause instanceof InitializationError) {
        InitializationError initializationError = (InitializationError) cause;
        msg = msg + " " + initializationError.getCauses();
      }
      sLog.log(Level.SEVERE, msg);
      throw new RuntimeException(msg, cause);
    }
  } else {
    // this code is executed locally so create a ProxyRunner which will
    // forward the execution to the container
    if (runInContainerWith != null) {
      runnerClass = runInContainerWith.value();
      if (Parameterized.class.isAssignableFrom(runnerClass)) {
        mRunner = new ParameterizedProxyRunner(testClass, containerUrl);
      } else if (Suite.class.isAssignableFrom(runnerClass)) {
        throw new IllegalArgumentException(
            RunInContainerWith.class.getSimpleName()
                + " annotation does not support Suite runner or any subclass of Suite except Parameterized");
      } else {
        Runner runInContainerRunner = runnerClass.getDeclaredConstructor(Class.class)
            .newInstance(testClass);
        Description desc = runInContainerRunner.getDescription();
        mRunner = new BasicProxyRunner(testClass, containerUrl, desc);
      }
    } else {
      mRunner = new BasicProxyRunner(testClass, containerUrl);
    }
  }

}
项目:pitest    文件:IgnoreCoreClassesTest.java   
@Test
public void shouldIgnoreJUnitClasses() {
  assertIgnored(JUnit4.class);
}
项目:whiskers    文件:Main.java   
private static void runTest(RunNotifier notifier, Class<?> klass) throws InitializationError {
    JUnit4 runner = new JUnit4(klass);
    runner.run(notifier);
}