/** * @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; }
@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()); }
@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")); }
/** * @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; }
@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(); }
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; }
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; }
@Test public void annotatedClassWithoutValueReturnsDefault() { DelegateRunningToDiscoverer discoverer = new DelegateRunningToDiscoverer(); Class discovered = discoverer.getDelegateRunningToOn(FixtureWithoutValue.class); assertThat(discovered, equalTo((Class) JUnit4.class)); }
@Test public void unannotatedClassReturnsDefault() { DelegateRunningToDiscoverer discoverer = new DelegateRunningToDiscoverer(); Class discovered = discoverer.getDelegateRunningToOn(UnannotatedFixture.class); assertThat(discovered, equalTo((Class) JUnit4.class)); }
private void executeJunitTest(final Class<?> testClass) { try { new JUnit4(testClass).run(runNotifierWith(runListeners)); } catch (final InitializationError initializationError) { throw new RuntimeException(initializationError); } }
@Override protected Class<? extends Runner> getRunnerClass() { return JUnit4.class; }
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); } } }
@Test public void shouldIgnoreJUnitClasses() { assertIgnored(JUnit4.class); }
private static void runTest(RunNotifier notifier, Class<?> klass) throws InitializationError { JUnit4 runner = new JUnit4(klass); runner.run(notifier); }