Search in sources :

Example 1 with MethodFilter

use of fr.inria.stamp.test.filter.MethodFilter 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)

Aggregations

MethodFilter (fr.inria.stamp.test.filter.MethodFilter)1 TestListener (fr.inria.stamp.test.listener.TestListener)1 Request (org.junit.runner.Request)1 Runner (org.junit.runner.Runner)1 RunNotifier (org.junit.runner.notification.RunNotifier)1