Search in sources :

Example 21 with RunNotifier

use of org.junit.runner.notification.RunNotifier in project dspot by STAMP-project.

the class DefaultTestRunner method run.

@Override
public TestListener run(Class<?> testClass, Collection<String> testMethodNames, RunListener... additionalListeners) {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    TestListener listener = new TestListener();
    final Future<?> submit = executor.submit(() -> {
        Request request = Request.aClass(testClass);
        if (!testMethodNames.isEmpty()) {
            request = request.filterWith(new MethodFilter(testMethodNames));
        }
        Runner runner = request.getRunner();
        RunNotifier runNotifier = new RunNotifier();
        Arrays.stream(additionalListeners).forEach(runNotifier::addListener);
        runNotifier.addFirstListener(listener);
        // Since we want to use our custom ClassLoader to run the tests of the project being executed by DSpot,
        // and since we create a new thread for starting the JUnit Runner, we need to set the context ClassLoader
        // to be our custom ClassLoader. This is so that any code in the tests or triggered by the test that uses
        // the context ClassLoader will work.
        // As an example if the tests call some code that uses Java's ServiceLoader then it would fail to find and
        // load any provider located in our custom ClassLoader.
        Thread.currentThread().setContextClassLoader(this.classLoader);
        runner.run(runNotifier);
    });
    try {
        long timeBeforeTimeOut = testMethodNames.isEmpty() ? AmplificationHelper.getTimeOutInMs() * (testClass.getMethods().length + 1) : AmplificationHelper.getTimeOutInMs() * (testMethodNames.size() + 1);
        submit.get(timeBeforeTimeOut, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        submit.cancel(true);
        executor.shutdownNow();
    }
    return listener;
}
Also used : Runner(org.junit.runner.Runner) RunNotifier(org.junit.runner.notification.RunNotifier) MethodFilter(fr.inria.stamp.test.filter.MethodFilter) Request(org.junit.runner.Request) TestListener(fr.inria.stamp.test.listener.TestListener)

Example 22 with RunNotifier

use of org.junit.runner.notification.RunNotifier in project evosuite by EvoSuite.

the class PostProcessor method process.

/**
 * @param logs   logs of captured interaction
 * @param packages  package names associated with logs. Mapping logs.get(i) belongs to packages.get(i)
 * @throws IOException
 */
public static void process(final List<CaptureLog> logs, final List<String> packages, final List<Class<?>[]> observedClasses) throws IOException {
    if (logs == null) {
        throw new NullPointerException("list of CaptureLogs must not be null");
    }
    if (packages == null) {
        throw new NullPointerException("list of package names associated with logs must not be null");
    }
    if (observedClasses == null) {
        throw new NullPointerException("list of classes to be observed must not be null");
    }
    final int size = logs.size();
    if (packages.size() != size || observedClasses.size() != size) {
        throw new IllegalArgumentException("given lists must have same size");
    }
    // create post-processing sources in os specific temp folder
    final File tempDir = new File(System.getProperty("java.io.tmpdir"), "postprocessing_" + System.currentTimeMillis());
    tempDir.mkdir();
    CaptureLog log;
    String packageName;
    Class<?>[] classes;
    String targetFolder;
    File targetFile;
    final StringBuilder testClassNameBuilder = new StringBuilder();
    String testClassName;
    for (int i = 0; i < size; i++) {
        // =============== prepare carved test for post-processing ================================================
        packageName = packages.get(i);
        targetFolder = packageName.replace(".", File.separator);
        classes = observedClasses.get(i);
        if (classes.length == 0) {
            throw new IllegalArgumentException("there must be at least one class to be observed");
        }
        // for(int j = 0; j < classes.length; j++)
        // {
        // testClassNameBuilder.append(classes[j].getSimpleName());
        // if(testClassNameBuilder.length() >= 10)
        // {
        // break;
        // }
        // }
        testClassNameBuilder.append("CarvedTest");
        log = logs.get(i);
        long s = System.currentTimeMillis();
        logger.debug(">>>> (postprocess) start test create ");
        targetFile = new File(tempDir, targetFolder);
        targetFile.mkdirs();
        testClassName = getFreeClassName(targetFile, testClassNameBuilder.toString());
        targetFile = new File(targetFile, testClassName + ".java");
        // write out test files containing post-processing statements
        writeTest(log, packageName, testClassName, classes, targetFile, true);
        logger.debug(">>>> (postprocess) end test creation -> " + (System.currentTimeMillis() - s) / 1000);
        // =============== compile generated post-processing test ================================================
        final JavaCompiler compiler = new EclipseCompiler();
        // final JavaCompiler 			  compiler    = ToolProvider.getSystemJavaCompiler();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File[] { targetFile }));
        // --- add modified bins (see Transformer and ClassPreparer.createPreparedBin()) to class path
        String classPath = Configuration.INSTANCE.getProperty(Configuration.MODIFIED_BIN_LOC);
        classPath += File.pathSeparator + System.getProperty("java.class.path");
        final Boolean wasCompilationSuccess = compiler.getTask(null, fileManager, null, Arrays.asList(new String[] { "-cp", classPath }), null, compilationUnits).call();
        if (!wasCompilationSuccess) {
            logger.error("Compilation was not not successful for " + targetFile);
            fileManager.close();
            continue;
        }
        fileManager.close();
        // =============== execute + observe post-processing test run ================================================
        final PostProcessorClassLoader cl = new PostProcessorClassLoader(tempDir);
        final Class<?> testClass = cl.findClass(packageName + '.' + testClassName);
        BlockJUnit4ClassRunner testRunner;
        try {
            testRunner = new BlockJUnit4ClassRunner(testClass);
            testRunner.run(new RunNotifier());
        } catch (InitializationError e) {
            logger.error("" + e, e);
        }
        // ============== generate final test file ================================================================
        final String targetDir = Configuration.INSTANCE.getProperty(Configuration.GEN_TESTS_LOC);
        targetFile = new File(new File(targetDir), targetFolder);
        targetFile.mkdirs();
        testClassName = getFreeClassName(targetFile, testClassNameBuilder.toString());
        targetFile = new File(targetFile, testClassName + ".java");
        writeTest(log, packageName, testClassName, classes, targetFile, false);
        // recycle StringBuilder for testClassName
        testClassNameBuilder.setLength(0);
    }
    // clean up post-processing stuff
    tempDir.delete();
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) BlockJUnit4ClassRunner(org.junit.runners.BlockJUnit4ClassRunner) InitializationError(org.junit.runners.model.InitializationError) CaptureLog(org.evosuite.testcarver.capture.CaptureLog) EclipseCompiler(org.eclipse.jdt.internal.compiler.tool.EclipseCompiler) File(java.io.File)

Example 23 with RunNotifier

use of org.junit.runner.notification.RunNotifier in project nutz by nutzam.

the class AdvancedTestAll method test.

public static TestResult test(List<Request> reqs, String name, Map<Request, Method> reqMap) {
    // TODO 根据order文件还原测试顺序
    try {
        FileWriter fw = new FileWriter("./test_order_" + name + ".txt");
        for (Request request : reqs) {
            fw.write(reqMap.get(request).toString());
            fw.write("\n");
        }
        fw.flush();
        fw.close();
    } catch (IOException e) {
    }
    final TestResult result = new TestResult();
    RunNotifier notifier = new RunNotifier();
    notifier.addListener(new RunListener() {

        public void testFailure(Failure failure) throws Exception {
            result.addError(asTest(failure.getDescription()), failure.getException());
        }

        public void testFinished(Description description) throws Exception {
            result.endTest(asTest(description));
        }

        public void testStarted(Description description) throws Exception {
            result.startTest(asTest(description));
        }

        public junit.framework.Test asTest(Description description) {
            return new junit.framework.Test() {

                public void run(TestResult result) {
                    throw Lang.noImplement();
                }

                public int countTestCases() {
                    return 1;
                }
            };
        }
    });
    for (Request request : reqs) {
        request.getRunner().run(notifier);
    }
    return result;
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) Description(org.junit.runner.Description) FileWriter(java.io.FileWriter) Request(org.junit.runner.Request) TestResult(junit.framework.TestResult) IOException(java.io.IOException) IOException(java.io.IOException) RunListener(org.junit.runner.notification.RunListener) Test(org.junit.Test) TestFailure(junit.framework.TestFailure) Failure(org.junit.runner.notification.Failure)

Example 24 with RunNotifier

use of org.junit.runner.notification.RunNotifier in project mockito by mockito.

the class DefaultInternalRunnerTest method newNotifier.

private RunNotifier newNotifier(RunListener listener) {
    RunNotifier notifier = new RunNotifier();
    notifier.addListener(listener);
    return notifier;
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier)

Example 25 with RunNotifier

use of org.junit.runner.notification.RunNotifier in project cucumber-jvm by cucumber.

the class CucumberTest method cucumber_can_run_features_in_parallel.

@Test
void cucumber_can_run_features_in_parallel() throws Exception {
    RunNotifier notifier = new RunNotifier();
    RunListener listener = Mockito.mock(RunListener.class);
    notifier.addListener(listener);
    ParallelComputer computer = new ParallelComputer(true, true);
    Request.classes(computer, ValidEmpty.class).getRunner().run(notifier);
    {
        InOrder order = Mockito.inOrder(listener);
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("Followed by some examples #1(Feature A)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("Followed by some examples #1(Feature A)")));
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("Followed by some examples #2(Feature A)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("Followed by some examples #2(Feature A)")));
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("Followed by some examples #3(Feature A)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("Followed by some examples #3(Feature A)")));
    }
    {
        InOrder order = Mockito.inOrder(listener);
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("A(Feature B)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("A(Feature B)")));
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("B(Feature B)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("B(Feature B)")));
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("C #1(Feature B)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("C #1(Feature B)")));
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("C #2(Feature B)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("C #2(Feature B)")));
        order.verify(listener).testStarted(argThat(new DescriptionMatcher("C #3(Feature B)")));
        order.verify(listener).testFinished(argThat(new DescriptionMatcher("C #3(Feature B)")));
    }
}
Also used : RunNotifier(org.junit.runner.notification.RunNotifier) InOrder(org.mockito.InOrder) ParallelComputer(org.junit.experimental.ParallelComputer) RunListener(org.junit.runner.notification.RunListener) Test(org.junit.jupiter.api.Test)

Aggregations

RunNotifier (org.junit.runner.notification.RunNotifier)53 Failure (org.junit.runner.notification.Failure)16 Description (org.junit.runner.Description)15 Test (org.junit.Test)14 RunListener (org.junit.runner.notification.RunListener)13 Test (org.junit.jupiter.api.Test)9 Runner (org.junit.runner.Runner)9 InOrder (org.mockito.InOrder)7 Feature (io.cucumber.core.gherkin.Feature)5 ArrayList (java.util.ArrayList)5 Before (org.junit.Before)5 Request (org.junit.runner.Request)5 Result (org.junit.runner.Result)5 ParentRunner (org.junit.runners.ParentRunner)4 HashMap (java.util.HashMap)3 Scenario (org.drools.workbench.models.testscenarios.shared.Scenario)3 BlockJUnit4ClassRunner (org.junit.runners.BlockJUnit4ClassRunner)3 KieSession (org.kie.api.runtime.KieSession)3 Match (gherkin.formatter.model.Match)2 Scenario (gherkin.formatter.model.Scenario)2