How to use getDescription method of org.junit.runners.ParentRunner class

Best junit code snippet using org.junit.runners.ParentRunner.getDescription

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package fd.ng.test.junit;
2
3import fd.ng.core.conf.ConfFileLoader;
4import fd.ng.core.yaml.YamlFactory;
5import fd.ng.core.yaml.YamlMap;
6import fd.ng.core.yaml.YamlReader;
7import fd.ng.test.junit.conf.TestinfoConf;
8import org.junit.experimental.ParallelComputer;
9import org.junit.runner.Computer;
10import org.junit.runner.Runner;
11import org.junit.runners.ParentRunner;
12import org.junit.runners.model.InitializationError;
13import org.junit.runners.model.RunnerBuilder;
14import org.junit.runners.model.RunnerScheduler;
15
16import java.util.concurrent.ExecutorService;
17import java.util.concurrent.Executors;
18import java.util.concurrent.TimeUnit;
19
20public class ParallelRunner extends Computer {
21	private final boolean classes;
22	private final boolean methods;
23
24	public ParallelRunner(boolean classes, boolean methods) {
25		this.classes = classes;
26		this.methods = methods;
27	}
28
29	public static Computer classes() {
30		return new ParallelComputer(true, false);
31	}
32
33	public static Computer methods() {
34		return new ParallelComputer(false, true);
35	}
36
37	private static Runner parallelize(Runner runner) {
38		if (runner instanceof ParentRunner) {
39			((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() {
40				private final ExecutorService fService = Executors.newCachedThreadPool();
41
42				public void schedule(Runnable childStatement) {
43					fService.submit(childStatement);
44				}
45
46				public void finished() {
47					try {
48						fService.shutdown();
49						fService.awaitTermination(TestinfoConf.ParallelRunTimeout, TimeUnit.MILLISECONDS);
50					} catch (InterruptedException e) {
51						System.out.printf("Runner Termination Error : %s, class=%s%n", runner.getDescription(), runner.getClass().getName());
52						e.printStackTrace(System.out);
53					}
54				}
55			});
56		} else {
57			System.out.printf("Unsupported Runner : %s, class=%s%n", runner.getDescription(), runner.getClass().getName());
58		}
59		return runner;
60	}
61
62	@Override
63	public Runner getSuite(RunnerBuilder builder, java.lang.Class<?>[] classes)
64			throws InitializationError {
65		Runner suite = super.getSuite(builder, classes);
66		return this.classes ? parallelize(suite) : suite;
67	}
68
69	@Override
70	protected Runner getRunner(RunnerBuilder builder, Class<?> testClass)
71			throws Throwable {
72		Runner runner = super.getRunner(builder, testClass);
73		return methods ? parallelize(runner) : runner;
74	}
75}
76
Full Screen
copy
1package org.junit.tests.running.classes.parent;
2
3
4import org.junit.Test;
5import org.junit.runner.Description;
6import org.junit.runner.notification.RunNotifier;
7import org.junit.runners.BlockJUnit4ClassRunner;
8import org.junit.runners.ParentRunner;
9import org.junit.runners.model.FrameworkMethod;
10import org.junit.runners.model.InitializationError;
11
12import java.lang.reflect.Field;
13import java.net.URL;
14import java.net.URLClassLoader;
15import java.util.List;
16
17import static org.junit.Assert.assertEquals;
18
19public class ParentRunnerClassLoaderTest {
20    @Test
21    public void testClassRuleAccessToClassInAnotherClassLoader() throws Exception {
22        Class<?> testClassWithOwnClassLoader = wrapToClassLoader(TestWithClassRule.class);
23
24        runTestWithParentRunner(testClassWithOwnClassLoader);
25
26        Field fieldWithReference = testClassWithOwnClassLoader.getDeclaredField("applyTestClass");
27        Class<?> usedClass = (Class<?>) fieldWithReference.get(null);
28
29        assertEquals("JUnitRunner can be located in own classLoader, so, " +
30                        "Class.forName org.junit.runner.Description.getTestClass can not see " +
31                        "in current classloader by execute Class.forName",
32                testClassWithOwnClassLoader, usedClass
33        );
34    }
35
36    @Test
37    public void testDescriptionContainCorrectTestClass() throws Exception {
38        Class<?> testClassWithOwnClassLoader = wrapToClassLoader(TestWithClassRule.class);
39        ParentRunner<?> runner = new BlockJUnit4ClassRunner(testClassWithOwnClassLoader);
40
41        Description description = runner.getDescription();
42        assertEquals("ParentRunner accept already instantiate Class<?> with tests, if we lost it instance, and will " +
43                "use Class.forName we can not find test class again, because tests can be " +
44                "located in different ClassLoader",
45                description.getTestClass(), testClassWithOwnClassLoader
46        );
47    }
48
49    @Test
50    public void testBackwardCompatibilityWithOverrideGetName() throws Exception {
51        final Class<TestWithClassRule> originalTestClass = TestWithClassRule.class;
52        final Class<?> waitClass = ParentRunnerClassLoaderTest.class;
53
54        ParentRunner<FrameworkMethod> subParentRunner = new BlockJUnit4ClassRunner(originalTestClass) {
55            @Override
56            protected String getName() {
57                return waitClass.getName();
58            }
59        };
60
61        Description description = subParentRunner.getDescription();
62        Class<?> result = description.getTestClass();
63
64        assertEquals("Subclass of ParentRunner can override getName method and specify another test class for run, " +
65                "we should  maintain backwards compatibility with JUnit 4.12",
66                waitClass, result
67        );
68    }
69
70    private void runTestWithParentRunner(Class<?> testClass) throws InitializationError {
71        ParentRunner<?> runner = new BlockJUnit4ClassRunner(testClass);
72        runner.run(new RunNotifier());
73    }
74
75    private Class<?> wrapToClassLoader(Class<?> sourceClass) throws ClassNotFoundException {
76        URL classpath = sourceClass.getProtectionDomain().getCodeSource().getLocation();
77        VisibleClassLoader loader = new VisibleClassLoader(new URL[]{classpath}, this.getClass().getClassLoader());
78        Class<?> testClassWithOwnClassLoader = loader.findClass(sourceClass.getName());
79
80        assert testClassWithOwnClassLoader != sourceClass;
81
82        return testClassWithOwnClassLoader;
83    }
84
85
86    private static class VisibleClassLoader extends URLClassLoader {
87        public VisibleClassLoader(URL[] urls, ClassLoader parent) {
88            super(urls, parent);
89        }
90
91        @Override // just making public
92        public Class<?> findClass(String name) throws ClassNotFoundException {
93            return super.findClass(name);
94        }
95    }
96}
97
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger getDescription code on LambdaTest Cloud Grid

Execute automation tests with getDescription on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)