Search in sources :

Example 61 with Runner

use of org.junit.runner.Runner in project intellij-community by JetBrains.

the class JUnit4TestRunnerUtil method getParameterizedRequest.

private static Request getParameterizedRequest(final String parameterString, final String methodName, Class clazz, RunWith clazzAnnotation) {
    if (clazzAnnotation == null)
        return null;
    final Class runnerClass = clazzAnnotation.value();
    if (Parameterized.class.isAssignableFrom(runnerClass)) {
        try {
            if (methodName != null) {
                final Method method = clazz.getMethod(methodName, new Class[0]);
                if (method != null && !method.isAnnotationPresent(Test.class) && TestCase.class.isAssignableFrom(clazz)) {
                    return Request.runner(JUnit45ClassesRequestBuilder.createIgnoreAnnotationAndJUnit4ClassRunner(clazz));
                }
            }
            //ignore for junit4.4 and <
            Class.forName("org.junit.runners.BlockJUnit4ClassRunner");
            final Constructor runnerConstructor = runnerClass.getConstructor(new Class[] { Class.class });
            return Request.runner((Runner) runnerConstructor.newInstance(new Object[] { clazz })).filterWith(new Filter() {

                public boolean shouldRun(Description description) {
                    final String descriptionMethodName = description.getMethodName();
                    //filter by params
                    if (parameterString != null && descriptionMethodName != null && !descriptionMethodName.endsWith(parameterString)) {
                        return false;
                    }
                    //filter only selected method
                    if (methodName != null && descriptionMethodName != null && //valid for any parameter for current method
                    !descriptionMethodName.startsWith(methodName + "[") && !descriptionMethodName.equals(methodName)) {
                        //if fork mode used, parameter is included in the name itself
                        return false;
                    }
                    return true;
                }

                public String describe() {
                    if (parameterString == null) {
                        return methodName + " with any parameter";
                    }
                    if (methodName == null) {
                        return "Parameter " + parameterString + " for any method";
                    }
                    return methodName + " with parameter " + parameterString;
                }
            });
        } catch (Throwable throwable) {
        //return simple method runner
        }
    }
    return null;
}
Also used : Runner(org.junit.runner.Runner) Description(org.junit.runner.Description) Test(org.junit.Test) TestCase(junit.framework.TestCase) Filter(org.junit.runner.manipulation.Filter) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Example 62 with Runner

use of org.junit.runner.Runner in project gerrit by GerritCodeReview.

the class ConfigSuite method runnersFor.

private static List<Runner> runnersFor(Class<?> clazz) {
    Method defaultConfig = getDefaultConfig(clazz);
    List<Method> configs = getConfigs(clazz);
    Map<String, org.eclipse.jgit.lib.Config> configMap = callConfigMapMethod(getConfigMap(clazz), configs);
    Field parameterField = getOnlyField(clazz, Parameter.class);
    checkArgument(parameterField != null, "No @ConfigSuite.Parameter found");
    Field nameField = getOnlyField(clazz, Name.class);
    List<Runner> result = Lists.newArrayListWithCapacity(configs.size() + 1);
    try {
        result.add(new ConfigRunner(clazz, parameterField, nameField, null, callConfigMethod(defaultConfig)));
        for (Method m : configs) {
            result.add(new ConfigRunner(clazz, parameterField, nameField, m.getName(), callConfigMethod(m)));
        }
        for (Map.Entry<String, org.eclipse.jgit.lib.Config> e : configMap.entrySet()) {
            result.add(new ConfigRunner(clazz, parameterField, nameField, e.getKey(), e.getValue()));
        }
        return result;
    } catch (InitializationError e) {
        System.err.println("Errors initializing runners:");
        for (Throwable t : e.getCauses()) {
            t.printStackTrace();
        }
        throw new RuntimeException(e);
    }
}
Also used : BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) Runner(org.junit.runner.Runner) InitializationError(org.junit.runners.model.InitializationError) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Field(java.lang.reflect.Field) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 63 with Runner

use of org.junit.runner.Runner in project pinpoint by naver.

the class ForkedPinpointPluginTest method runTests.

private static Result runTests(Class<?> testClass, String testId) throws InitializationError {
    JUnitCore junit = new JUnitCore();
    junit.addListener(new PrintListener());
    Runner runner = new ForkedPinpointPluginTestRunner(testClass, testId);
    Result result = junit.run(runner);
    return result;
}
Also used : Runner(org.junit.runner.Runner) PrintListener(com.navercorp.pinpoint.test.plugin.shared.PrintListener) JUnitCore(org.junit.runner.JUnitCore) Result(org.junit.runner.Result)

Example 64 with Runner

use of org.junit.runner.Runner in project pinpoint by naver.

the class PinpointPluginTestSuite method filter.

@Override
public void filter(Filter filter) throws NoTestsRemainException {
    synchronized (childrenLock) {
        List<Runner> children = new ArrayList<>(getFilteredChildren());
        for (Iterator<Runner> iter = children.iterator(); iter.hasNext(); ) {
            Runner each = iter.next();
            if (shouldRun(filter, each)) {
                try {
                    filter.apply(each);
                } catch (NoTestsRemainException e) {
                    iter.remove();
                }
            } else {
                iter.remove();
            }
        }
        filteredChildren = Collections.unmodifiableCollection(children);
        if (filteredChildren.isEmpty()) {
            throw new NoTestsRemainException();
        }
    }
}
Also used : Runner(org.junit.runner.Runner) ArrayList(java.util.ArrayList) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException)

Example 65 with Runner

use of org.junit.runner.Runner in project scout.rt by eclipse.

the class InteractiveTestSuite method run.

@Override
@SuppressWarnings({ "squid:S1181", "squid:S1166" })
public void run(final RunNotifier notifier) {
    System.out.println("Started interactive test console. (Auto-closing in 30 seconds when no input is entered, assuming it is a ci-test-run)");
    String lastLine = "";
    while (true) {
        try {
            System.out.println("********");
            System.out.println("[Enter fully qualified test class name, enter to repeat last test, '.' to exit]");
            if (!checkIfHumanInterface()) {
                return;
            }
            String line = new BufferedReader(new InputStreamReader(System.in)).readLine();
            if (StringUtility.isNullOrEmpty(line)) {
                if (!lastLine.isEmpty()) {
                    line = lastLine;
                } else {
                    continue;
                }
            }
            if (".".equalsIgnoreCase(line)) {
                return;
            }
            lastLine = line;
            Method runMethod;
            try {
                // NOSONAR
                runMethod = m_annotatedClass.getMethod("run", Runner.class, RunNotifier.class);
            } catch (Throwable t) {
                runMethod = InteractiveTestSuite.class.getMethod("run", Runner.class, RunNotifier.class);
            }
            Class<?> testClass = Class.forName(line, true, m_annotatedClass.getClassLoader());
            Runner runner = m_builder.safeRunnerForClass(testClass);
            if (Modifier.isStatic(runMethod.getModifiers())) {
                runMethod.invoke(null, runner, notifier);
            } else {
                runMethod.invoke(m_annotatedClass.newInstance(), runner, notifier);
            }
        } catch (Throwable ex) {
            System.out.println("Cannot load test " + lastLine);
            ex.printStackTrace(System.out);
        }
    }
}
Also used : Runner(org.junit.runner.Runner) RunNotifier(org.junit.runner.notification.RunNotifier) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Method(java.lang.reflect.Method)

Aggregations

Runner (org.junit.runner.Runner)106 Test (org.junit.Test)39 RunNotifier (org.junit.runner.notification.RunNotifier)22 ArrayList (java.util.ArrayList)18 Description (org.junit.runner.Description)18 JUnitCore (org.junit.runner.JUnitCore)18 ParentRunner (org.junit.runners.ParentRunner)16 Request (org.junit.runner.Request)15 Result (org.junit.runner.Result)13 Filter (org.junit.runner.manipulation.Filter)12 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)12 InitializationError (org.junit.runners.model.InitializationError)10 Method (java.lang.reflect.Method)9 JUnit4ParameterizedTest (androidx.test.testing.fixtures.JUnit4ParameterizedTest)7 SuiteConfiguration (org.eclipse.reddeer.junit.internal.configuration.SuiteConfiguration)7 Failure (org.junit.runner.notification.Failure)7 ErrorReportingRunner (org.junit.internal.runners.ErrorReportingRunner)6 Filterable (org.junit.runner.manipulation.Filterable)6 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)6 Field (java.lang.reflect.Field)5