org.junit.runner.Runner Java Examples

The following examples show how to use org.junit.runner.Runner. 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: RunnerSuiteFinder.java    From pitest with Apache License 2.0 6 votes vote down vote up
@Override
public List<Class<?>> apply(final Class<?> a) {
  try {
    final Runner runner = AdaptedJUnitTestUnit.createRunner(a);

    final List<Description> allChildren = new ArrayList<>();
    flattenChildren(allChildren, runner.getDescription());

    final List<Description> suites = FCollection.filter(allChildren,
        Prelude.or(isSuiteMethodRunner(runner), isSuite()));
    final Set<Class<?>> classes = suites.stream().flatMap(descriptionToTestClass()).collect(Collectors.toSet());

    classes.remove(a);
    return new ArrayList<>(classes);
  } catch (final RuntimeException ex) {
    // some runners (looking at you spock) can throw a runtime exception
    // when the getDescription method is called.
    return Collections.emptyList();
  }

}
 
Example #2
Source File: CustomParameterizedRunner.java    From registry with Apache License 2.0 6 votes vote down vote up
private List<Runner> createRunnersForParameters(
        Iterable<Object> allParameters, String namePattern,
        ParametersRunnerFactory runnerFactory) throws Exception {
    try {
        List<TestWithParameters> tests = createTestsForParameters(
                allParameters, namePattern);
        List<Runner> runners = new ArrayList<Runner>();
        for (TestWithParameters test : tests) {
            runners.add(runnerFactory
                    .createRunnerForTestWithParameters(test));
        }
        return runners;
    } catch (ClassCastException e) {
        throw parametersMethodReturnedWrongType();
    }
}
 
Example #3
Source File: AndroidAnnotatedBuilderTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void successfullyCreateAndroidRunner() throws Exception {
  final Runner mockedRunner = mock(Runner.class);
  AndroidAnnotatedBuilder ab =
      new AndroidAnnotatedBuilder(mockRunnerBuilder, mockAndroidRunnerParams) {
        @Override
        public Runner buildAndroidRunner(Class<? extends Runner> runnerClass, Class<?> testClass)
            throws Exception {
          assertEquals(runnerClass, AndroidJUnit4.class);
          assertEquals(testClass, RunWithAndroidJUnit4Class.class);
          return mockedRunner;
        }
      };
  // attempt to create a runner for a class annotated with @RunWith(AndroidJUnit4.class)
  Runner runner = ab.runnerForClass(RunWithAndroidJUnit4Class.class);
  assertEquals(0, runner.testCount());
}
 
Example #4
Source File: NamedParameterizedRunnerTest.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Confirms that each given name has a {@linkplain ReifiedParamRunner} associated with it, and returns the
 * name -> runner map
 * @param runner the parameterized runner
 * @param names the expected names
 * @return a map of names to reified runners
 */
private static Map<String,ReifiedParamRunner> testParameterizations(NamedParameterizedRunner runner, String... names)
{
    List<Runner> children = runner.getChildren();
    assertEquals("children.size()", names.length, children.size());

    Set<String> expectedNames = new HashSet<>(names.length, 1.0f);
    for (String name : names) {
        assertTrue("unexpected error, duplicate name: " + name, expectedNames.add(name));
    }

    Map<String,ReifiedParamRunner> foundRunners = new HashMap<>();
    for (Runner child : children)
    {
        ReifiedParamRunner reified = (ReifiedParamRunner)child;
        String paramToString = reified.paramToString();
        assertNull("duplicate name: " + paramToString, foundRunners.put(paramToString, reified));
    }

    for (String expected : expectedNames)
    {
        assertTrue("didn't find expected param: " + expected, foundRunners.containsKey(expected));
    }

    return foundRunners;
}
 
Example #5
Source File: JUnitCustomRunnerTestUnitFinder.java    From pitest with Apache License 2.0 6 votes vote down vote up
@Override
public List<TestUnit> findTestUnits(final Class<?> clazz) {

  final Runner runner = AdaptedJUnitTestUnit.createRunner(clazz);

  if (isExcluded(runner) || isNotARunnableTest(runner, clazz.getName()) || !isIncluded(clazz)) {
    return Collections.emptyList();
  }

  if (Filterable.class.isAssignableFrom(runner.getClass())
      && !shouldTreatAsOneUnit(clazz, runner)) {
    final List<TestUnit> filteredUnits = splitIntoFilteredUnits(runner.getDescription());
    return filterUnitsByMethod(filteredUnits);
  } else {
    return Collections.<TestUnit> singletonList(new AdaptedJUnitTestUnit(
        clazz, Optional.<Filter> empty()));
  }
}
 
Example #6
Source File: AndroidSuiteBuilder.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public Runner runnerForClass(Class<?> testClass) throws Throwable {
  if (androidRunnerParams.isIgnoreSuiteMethods()) {
    return null;
  }
  try {
    if (hasSuiteMethod(testClass)) {
      Test t = SuiteMethod.testFromSuiteMethod(testClass);
      if (!(t instanceof TestSuite)) {
        // this should not be possible
        throw new IllegalArgumentException(
            testClass.getName() + "#suite() did not return a TestSuite");
      }
      return new JUnit38ClassRunner(new AndroidTestSuite((TestSuite) t, androidRunnerParams));
    }
  } catch (Throwable e) {
    // log error message including stack trace before throwing to help with debugging.
    Log.e(LOG_TAG, "Error constructing runner", e);
    throw e;
  }
  return null;
}
 
Example #7
Source File: MasterSuiteRunnersBuilder.java    From JTAF-XCore with Apache License 2.0 6 votes vote down vote up
public List<Runner> runners()
		throws InitializationError, ParsingException, SAXException, IOException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
	
	
	AutomationEngine engine = AutomationEngine.getInstance();
	ArrayList<Runner> tests = new ArrayList<Runner>();
	
	engine.buildModel(new File("testlibrary"), new File("testscripts"));
	TestAgenda testAgenda = engine.getTestAgenda();

	for (TestScript testScript : testAgenda.getTestScripts()) {
		TestCaseRunner theRunner = new TestCaseRunner(ParallelScriptRunner.class, new ParallelScriptRunner(testScript));
		tests.add(theRunner);
		ConcurrentScheduler.registerTestName(testScript.getName());
	}
	
	return tests;
}
 
Example #8
Source File: AbstractPinpointPluginTestSuite.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public AbstractPinpointPluginTestSuite(Class<?> testClass) throws InitializationError, ArtifactResolutionException, DependencyResolutionException {
    super(testClass, Collections.<Runner> emptyList());

    PinpointAgent agent = testClass.getAnnotation(PinpointAgent.class);
    this.agentJar = resolveAgentPath(agent);

    PinpointConfig config = testClass.getAnnotation(PinpointConfig.class);
    this.configFile = config == null ? null : resolveConfigFileLocation(config.value());

    PinpointProfile profile = testClass.getAnnotation(PinpointProfile.class);
    this.profile = resolveProfile(profile);

    JvmArgument jvmArgument = testClass.getAnnotation(JvmArgument.class);
    this.jvmArguments = getJvmArguments(jvmArgument);

    JvmVersion jvmVersion = testClass.getAnnotation(JvmVersion.class);
    this.jvmVersions = jvmVersion == null ? new int[] { NO_JVM_VERSION } : jvmVersion.value();

    ImportPlugin importPlugin = testClass.getAnnotation(ImportPlugin.class);
    this.importPluginIds = getImportPlugin(importPlugin);

    this.requiredLibraries = getClassPathList(REQUIRED_CLASS_PATHS);
    this.mavenDependencyLibraries = getClassPathList(MAVEN_DEPENDENCY_CLASS_PATHS);
    this.testClassLocation = resolveTestClassLocation(testClass);
    this.debug = isDebugMode();
}
 
Example #9
Source File: AdaptedJUnitTestUnit.java    From pitest with Apache License 2.0 6 votes vote down vote up
private void filterIfRequired(final ResultCollector rc, final Runner runner) {
  if (this.filter.isPresent()) {
    if (!(runner instanceof Filterable)) {
      LOG.warning("Not able to filter " + runner.getDescription()
          + ". Mutation may have prevented JUnit from constructing test");
      return;
    }
    final Filterable f = (Filterable) runner;
    try {
      f.filter(this.filter.get());
    } catch (final NoTestsRemainException e1) {
      rc.notifySkipped(this.getDescription());
      return;
    }
  }
}
 
Example #10
Source File: AbstractMultiTestRunner.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runEnabledTests(RunNotifier nested) {
    if (enabledTests.isEmpty()) {
        return;
    }

    Runner runner;
    try {
        runner = createExecutionRunner();
    } catch (Throwable t) {
        runner = new CannotExecuteRunner(getDisplayName(), target, t);
    }

    try {
        if (!disabledTests.isEmpty()) {
            ((Filterable) runner).filter(new Filter() {
                @Override
                public boolean shouldRun(Description description) {
                    return !disabledTests.contains(description);
                }

                @Override
                public String describe() {
                    return "disabled tests";
                }
            });
        }
    } catch (NoTestsRemainException e) {
        return;
    }

    runner.run(nested);
}
 
Example #11
Source File: JUnitCustomRunnerTestUnitFinder.java    From pitest with Apache License 2.0 5 votes vote down vote up
private boolean shouldTreatAsOneUnit(final Class<?> clazz, final Runner runner) {
  final Set<Method> methods = Reflection.allMethods(clazz);
  return runnerCannotBeSplit(runner)
      || hasAnnotation(methods, BeforeClass.class)
      || hasAnnotation(methods, AfterClass.class)
      || hasClassRuleAnnotations(clazz, methods);
}
 
Example #12
Source File: GuidedFuzzing.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Runs the guided fuzzing loop for a resolved class.
 *
 * <p>The test class must be annotated with <tt>@RunWith(JQF.class)</tt>
 * and the test method must be annotated with <tt>@Fuzz</tt>.</p>
 *
 * <p>Once this method is invoked, the guided fuzzing loop runs continuously
 * until the guidance instance decides to stop by returning <tt>false</tt>
 * for {@link Guidance#hasInput()}. Until the fuzzing stops, this method
 * cannot be invoked again (i.e. at most one guided fuzzing can be running
 * at any time in a single JVM instance).</p>
 *
 * @param testClass     the test class containing the test method
 * @param testMethod    the test method to execute in the fuzzing loop
 * @param guidance      the fuzzing guidance
 * @param out           an output stream to log Junit messages
 * @throws IllegalStateException if a guided fuzzing run is currently executing
 * @return the Junit-style test result
 */
public synchronized static Result run(Class<?> testClass, String testMethod,
                                      Guidance guidance, PrintStream out) throws IllegalStateException {

    // Ensure that the class uses the right test runner
    RunWith annotation = testClass.getAnnotation(RunWith.class);
    if (annotation == null || !annotation.value().equals(JQF.class)) {
        throw new IllegalArgumentException(testClass.getName() + " is not annotated with @RunWith(JQF.class)");
    }


    // Set the static guided instance
    setGuidance(guidance);

    // Register callback
    SingleSnoop.setCallbackGenerator(guidance::generateCallBack);

    // Create a JUnit Request
    Request testRequest = Request.method(testClass, testMethod);

    // Instantiate a runner (may return an error)
    Runner testRunner = testRequest.getRunner();

    // Start tracing for the test method
    SingleSnoop.startSnooping(testClass.getName() + "#" + testMethod);

    // Run the test and make sure to de-register the guidance before returning
    try {
        JUnitCore junit = new JUnitCore();
        if (out != null) {
            junit.addListener(new TextListener(out));
        }
        return junit.run(testRunner);
    } finally {
        unsetGuidance();
    }



}
 
Example #13
Source File: SpringJUnit4ParameterizedClassRunner.java    From tds with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Only called reflectively. Do not use programmatically.
 */
public SpringJUnit4ParameterizedClassRunner(Class<?> klass) throws Throwable {
  super(klass, Collections.<Runner>emptyList());
  List<Object[]> parametersList = getParametersList(getTestClass());
  for (int i = 0; i < parametersList.size(); i++)
    runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), parametersList, i));
}
 
Example #14
Source File: HybrisJUnitIntegrationTestLoader.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
private ITestReference createFilteredTest(Class<?> clazz, String testName, String[] failureNames) {
	DescriptionMatcher matcher= DescriptionMatcher.create(clazz, testName);
	SubForestFilter filter= new SubForestFilter(matcher);
	Request request= sortByFailures(Request.classWithoutSuiteMethod(clazz).filterWith(filter), failureNames);
	Runner runner= request.getRunner();
	Description description= getRootDescription(runner, matcher);
	return new HybrisJUnitTestReference(runner, description);
}
 
Example #15
Source File: UniqueTestGenerator.java    From metamutator with GNU General Public License v3.0 5 votes vote down vote up
private CtCodeSnippetStatement createTestSnippet(CtClass element, Class<? extends Runner> runner) {
	CtCodeSnippetStatement e = getFactory().Core()
			.createCodeSnippetStatement();
	String val = "	new "+runner.getCanonicalName()+"("
			+ element.getQualifiedName()
			+ ".class).run(new org.junit.runner.notification.RunNotifier() {\n"
			+ "		@Override\n"
			+ "		public void fireTestFailure(org.junit.runner.notification.Failure failure) {\n"
			+ "			if (failure.getException() instanceof RuntimeException) throw (RuntimeException)failure.getException(); \n"
			+ "			if (failure.getException() instanceof Error) throw (Error)failure.getException(); \n"
			+ "         throw new RuntimeException(failure.getException());\n"
			+ "		}\n" + "	})";
	e.setValue(val);
	return e;
}
 
Example #16
Source File: PinpointPluginTestSuite.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private boolean shouldRun(Filter filter, Runner each) {
    if (filter.shouldRun(describeChild(each))) {
        return true;
    }

    if (each instanceof PinpointPluginTestRunner) {
        return ((PinpointPluginTestRunner) each).isAvaiable(filter);
    }

    return false;
}
 
Example #17
Source File: TestLoader.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Get the {@link Collection) of {@link Runner runners}.
 */
List<Runner> getRunnersFor(Collection<String> classNames, boolean isScanningPath) {
  for (String className : classNames) {
    doCreateRunner(className, isScanningPath);
  }
  return new ArrayList<>(runnersMap.values());
}
 
Example #18
Source File: JUnitCustomRunnerTestUnitFinder.java    From pitest with Apache License 2.0 5 votes vote down vote up
private boolean runnerCannotBeSplit(final Runner runner) {
  final String runnerName = runner.getClass().getName();
  return runnerName.equals("junitparams.JUnitParamsRunner")
      || runnerName.startsWith("org.spockframework.runtime.Sputnik")
      || runnerName.startsWith("com.insightfullogic.lambdabehave")
      || runnerName.startsWith("com.googlecode.yatspec")
      || runnerName.startsWith("com.google.gwtmockito.GwtMockitoTestRunner");
}
 
Example #19
Source File: RunnerBuilder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected AnnotatedBuilder annotatedBuilder() {
	return new AnnotatedBuilder(this) {
		@Override
		public Runner buildRunner(Class<? extends Runner> runnerClass,
				Class<?> testClass) throws Exception {
			if (XtextRunner.class.equals(runnerClass)) {
				return new AllScenariosRunner(testClass, processor, scenarios);
			}
			return super.buildRunner(runnerClass, testClass);
		}
	};
}
 
Example #20
Source File: JUnitTestClassExecuter.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runTestClass(String testClassName) throws ClassNotFoundException {
    final Class<?> testClass = Class.forName(testClassName, true, applicationClassLoader);
    Request request = Request.aClass(testClass);
    if (options.hasCategoryConfiguration()) {
        Transformer<Class<?>, String> transformer = new Transformer<Class<?>, String>() {
            public Class<?> transform(final String original) {
                try {
                    return applicationClassLoader.loadClass(original);
                } catch (ClassNotFoundException e) {
                    throw new InvalidUserDataException(String.format("Can't load category class [%s].", original), e);
                }
            }
        };
        request = request.filterWith(new CategoryFilter(
                CollectionUtils.collect(options.getIncludeCategories(), transformer),
                CollectionUtils.collect(options.getExcludeCategories(), transformer)
        ));
    }

    if (!options.getIncludedTests().isEmpty()) {
        request = request.filterWith(new MethodNameFilter(options.getIncludedTests()));
    }

    Runner runner = request.getRunner();
    //In case of no matching methods junit will return a ErrorReportingRunner for org.junit.runner.manipulation.Filter.class.
    //Will be fixed with adding class filters
    if (!org.junit.runner.manipulation.Filter.class.getName().equals(runner.getDescription().getDisplayName())) {
        RunNotifier notifier = new RunNotifier();
        notifier.addListener(listener);
        runner.run(notifier);
    }
}
 
Example #21
Source File: IgnoredTestDescriptorProvider.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
List<Description> getAllDescriptions(Description description, String className) {
    final AllExceptIgnoredTestRunnerBuilder allExceptIgnoredTestRunnerBuilder = new AllExceptIgnoredTestRunnerBuilder();
    try {
        final Class<?> testClass = description.getClass().getClassLoader().loadClass(className);
        Runner runner = allExceptIgnoredTestRunnerBuilder.runnerForClass(testClass);
        if (runner == null) {
            //fall back to default runner
            runner = Request.aClass(testClass).getRunner();
        }
        final Description runnerDescription = runner.getDescription();
        return runnerDescription.getChildren();
    } catch (Throwable throwable) {
        throw new TestSuiteExecutionException(String.format("Unable to process Ignored class %s.", className), throwable);
    }
}
 
Example #22
Source File: AllExceptIgnoredTestRunnerBuilder.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Runner runnerForClass(Class<?> testClass) throws Throwable {
    try {
        return new BlockJUnit4ClassRunner(testClass);
    } catch (Throwable t) {
        //failed to instantiate BlockJUnitRunner. try deprecated JUnitRunner (for JUnit < 4.5)
        try {
            Class<Runner> runnerClass = (Class<Runner>) Thread.currentThread().getContextClassLoader().loadClass("org.junit.internal.runners.JUnit4ClassRunner");
            final Constructor<Runner> constructor = runnerClass.getConstructor(Class.class);
            return constructor.newInstance(testClass);
        } catch (Throwable e) {
            LoggerFactory.getLogger(getClass()).warn("Unable to load JUnit4 runner to calculate Ignored test cases", e);
        }
    }
    return null;
}
 
Example #23
Source File: AllExceptIgnoredTestRunnerBuilder.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Runner runnerForClass(Class<?> testClass) throws Throwable {
    try {
        return new BlockJUnit4ClassRunner(testClass);
    } catch (Throwable t) {
        //failed to instantiate BlockJUnitRunner. try deprecated JUnitRunner (for JUnit < 4.5)
        try {
            Class<Runner> runnerClass = (Class<Runner>) Thread.currentThread().getContextClassLoader().loadClass("org.junit.internal.runners.JUnit4ClassRunner");
            final Constructor<Runner> constructor = runnerClass.getConstructor(Class.class);
            return constructor.newInstance(testClass);
        } catch (Throwable e) {
            LoggerFactory.getLogger(getClass()).warn("Unable to load JUnit4 runner to calculate Ignored test cases", e);
        }
    }
    return null;
}
 
Example #24
Source File: JunitSuiteRunnerTest.java    From lambda-behave with MIT License 5 votes vote down vote up
@Test
public void shouldNamedChildrenAfterTheirSuites() throws InitializationError {
    JunitSuiteRunner testee = new JunitSuiteRunner(ValidTestWithTwoDescriptions.class);
    List<Runner> actual = testee.getChildren();
    assertEquals(createSuiteDescription("a thing"), actual.get(0).getDescription());
    assertEquals(createSuiteDescription("another thing"), actual.get(1).getDescription());
}
 
Example #25
Source File: ConcurrentParameterized.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
@Override
protected List<Runner> getChildren() {
    for (Runner runner : super.getChildren()) {
        BlockJUnit4ClassRunner classRunner = (BlockJUnit4ClassRunner) runner;
        classRunner.setScheduler(scheduler);
    }
    return super.getChildren();
}
 
Example #26
Source File: AbstractMultiTestRunner.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void runEnabledTests(RunNotifier nested) {
    if (enabledTests.isEmpty()) {
        return;
    }

    Runner runner;
    try {
        runner = createExecutionRunner();
    } catch (Throwable t) {
        runner = new CannotExecuteRunner(getDisplayName(), target, t);
    }

    try {
        if (!disabledTests.isEmpty()) {
            ((Filterable) runner).filter(new Filter() {
                @Override
                public boolean shouldRun(Description description) {
                    return !disabledTests.contains(description);
                }

                @Override
                public String describe() {
                    return "disabled tests";
                }
            });
        }
    } catch (NoTestsRemainException e) {
        return;
    }

    runner.run(nested);
}
 
Example #27
Source File: AdaptedJUnitTestUnit.java    From pitest with Apache License 2.0 5 votes vote down vote up
private void checkForErrorRunner(final Runner runner) {
  if (runner instanceof ErrorReportingRunner) {
    LOG.warning("JUnit error for class " + this.clazz + " : "
        + runner.getDescription());
  }

}
 
Example #28
Source File: AndroidLogOnlyBuilderTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void builderHandlesJUnit3Tests() throws Throwable {
  Runner selectedRunner = androidLogOnlyBuilder.runnerForClass(JUnit3FailingTestCase.class);
  assertThat(selectedRunner, notNullValue());
  assertThat(selectedRunner.getClass(), typeCompatibleWith(JUnit38ClassRunner.class));
  runWithRunner(selectedRunner, 1, 0);
}
 
Example #29
Source File: AdaptedJUnitTestUnit.java    From pitest with Apache License 2.0 5 votes vote down vote up
public static Runner createRunner(final Class<?> clazz) {
  final RunnerBuilder builder = createRunnerBuilder();
  try {
    return builder.runnerForClass(clazz);
  } catch (final Throwable ex) {
    LOG.log(Level.SEVERE, "Error while creating runner for " + clazz, ex);
    throw translateCheckedException(ex);
  }

}
 
Example #30
Source File: AllExceptIgnoredTestRunnerBuilder.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Runner runnerForClass(Class<?> testClass) throws Throwable {
    try {
        return new BlockJUnit4ClassRunner(testClass);
    } catch (Throwable t) {
        //failed to instantiate BlockJUnitRunner. try deprecated JUnitRunner (for JUnit < 4.5)
        try {
            Class<Runner> runnerClass = (Class<Runner>) Thread.currentThread().getContextClassLoader().loadClass("org.junit.internal.runners.JUnit4ClassRunner");
            final Constructor<Runner> constructor = runnerClass.getConstructor(Class.class);
            return constructor.newInstance(testClass);
        } catch (Throwable e) {
            LoggerFactory.getLogger(getClass()).warn("Unable to load JUnit4 runner to calculate Ignored test cases", e);
        }
    }
    return null;
}