How to use schedule method of org.junit.runners.model.Interface RunnerScheduler class

Best junit code snippet using org.junit.runners.model.Interface RunnerScheduler.schedule

Run junit automation tests on LambdaTest cloud grid

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

copy
1package de.tbosch.test.utils;
2
3import java.lang.annotation.ElementType;
4import java.lang.annotation.Retention;
5import java.lang.annotation.RetentionPolicy;
6import java.lang.annotation.Target;
7import java.lang.reflect.Modifier;
8import java.util.List;
9import java.util.concurrent.ExecutorService;
10import java.util.concurrent.Executors;
11import java.util.concurrent.TimeUnit;
12
13import org.junit.runners.Parameterized;
14import org.junit.runners.model.FrameworkMethod;
15import org.junit.runners.model.RunnerScheduler;
16import org.junit.runners.model.TestClass;
17
18/**
19 * Erweitert den TestRunner {@link Parameterized} um die M├â┬Âglichkeit der parallelen Testfallausf├â┬╝hrung.
20 * 
21 * @author Thomas Bosch
22 */
23public class ParallelParameterized extends Parameterized {
24
25	/**
26	 * Annotation für die Methode in der Testklasse, die angibt wieviele Threads parallel laufen sollen.
27	 */
28	@Retention(RetentionPolicy.RUNTIME)
29	@Target(ElementType.METHOD)
30	public static @interface ThreadCount {
31		// marker
32	}
33
34	private static class ThreadPoolScheduler implements RunnerScheduler {
35
36		private final ExecutorService executor;
37
38		/**
39		 * Konstruktor.
40		 * 
41		 * @param threadNum
42		 */
43		public ThreadPoolScheduler(int threadNum) {
44			executor = Executors.newFixedThreadPool(threadNum);
45		}
46
47		/**
48		 * @see org.junit.runners.model.RunnerScheduler#finished()
49		 */
50		@Override
51		public void finished() {
52			executor.shutdown();
53			try {
54				executor.awaitTermination(8, TimeUnit.HOURS);
55			} catch (InterruptedException exc) {
56				throw new RuntimeException(exc);
57			}
58		}
59
60		/**
61		 * @see org.junit.runners.model.RunnerScheduler#schedule(java.lang.Runnable)
62		 */
63		@Override
64		public void schedule(Runnable childStatement) {
65			executor.submit(childStatement);
66		}
67	}
68
69	/**
70	 * Konstruktor.
71	 * 
72	 * @param klass
73	 * @throws Throwable
74	 */
75	public ParallelParameterized(Class<?> klass) throws Throwable {
76		super(klass);
77		int threadNum = (Integer) getThreadCountMethod(getTestClass()).invokeExplosively(null);
78		setScheduler(new ThreadPoolScheduler(threadNum));
79	}
80
81	private FrameworkMethod getThreadCountMethod(TestClass testClass) throws Exception {
82		List<FrameworkMethod> methods = testClass.getAnnotatedMethods(ThreadCount.class);
83		for (FrameworkMethod each : methods) {
84			int modifiers = each.getMethod().getModifiers();
85			if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) {
86				return each;
87			}
88		}
89		throw new Exception("No public static ThreadNumber method on class " + testClass.getName());
90	}
91
92}
Full Screen
copy
1package org.junit.runners.model;
2
3public interface RunnerScheduler {
4  void schedule(Runnable paramRunnable);
5  
6  void finished();
7}
8
9
10/* Location:              D:\APPS\yazan\JPBY.jar!\org\junit\runners\model\RunnerScheduler.class
11 * Java compiler version: 5 (49.0)
12 * JD-Core Version:       1.1.3
13 */
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

Most used method in Interface-RunnerScheduler

Trigger schedule code on LambdaTest Cloud Grid

Execute automation tests with schedule 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)