How to use getTestClass method of org.junit.runners.parameterized.TestWithParameters class

Best junit code snippet using org.junit.runners.parameterized.TestWithParameters.getTestClass

Run junit automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.zookeeper;
20
21import java.util.List;
22import org.junit.runners.model.FrameworkMethod;
23import org.junit.runners.model.InitializationError;
24import org.junit.runners.model.Statement;
25import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters;
26import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory;
27import org.junit.runners.parameterized.TestWithParameters;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31public class ZKParameterized {
32
33    private static final Logger LOG = LoggerFactory.getLogger(ZKParameterized.class);
34
35    public static class RunnerFactory extends BlockJUnit4ClassRunnerWithParametersFactory {
36
37        @Override
38        public org.junit.runner.Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError {
39            return new ZKParameterized.Runner(test);
40        }
41
42    }
43
44    public static class Runner extends BlockJUnit4ClassRunnerWithParameters {
45
46        public Runner(TestWithParameters test) throws InitializationError {
47            super(test);
48        }
49
50        @Override
51        protected List<FrameworkMethod> computeTestMethods() {
52            return JUnit4ZKTestRunner.computeTestMethodsForClass(getTestClass().getJavaClass(), super.computeTestMethods());
53        }
54
55        @Override
56        protected Statement methodInvoker(FrameworkMethod method, Object test) {
57            return new JUnit4ZKTestRunner.LoggedInvokeMethod(method, test);
58        }
59
60    }
61
62}
63
Full Screen
copy
1/*     */ package org.junit.runners;
2/*     */ 
3/*     */ import java.lang.annotation.ElementType;
4/*     */ import java.lang.annotation.Inherited;
5/*     */ import java.lang.annotation.Retention;
6/*     */ import java.lang.annotation.RetentionPolicy;
7/*     */ import java.lang.annotation.Target;
8/*     */ import java.text.MessageFormat;
9/*     */ import java.util.ArrayList;
10/*     */ import java.util.Arrays;
11/*     */ import java.util.Collections;
12/*     */ import java.util.List;
13/*     */ import org.junit.runner.Runner;
14/*     */ import org.junit.runners.model.FrameworkMethod;
15/*     */ import org.junit.runners.model.InitializationError;
16/*     */ import org.junit.runners.model.TestClass;
17/*     */ import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory;
18/*     */ import org.junit.runners.parameterized.ParametersRunnerFactory;
19/*     */ import org.junit.runners.parameterized.TestWithParameters;
20/*     */ 
21/*     */ 
22/*     */ 
23/*     */ 
24/*     */ 
25/*     */ 
26/*     */ 
27/*     */ 
28/*     */ 
29/*     */ 
30/*     */ 
31/*     */ 
32/*     */ 
33/*     */ 
34/*     */ 
35/*     */ 
36/*     */ 
37/*     */ 
38/*     */ 
39/*     */ 
40/*     */ 
41/*     */ 
42/*     */ 
43/*     */ 
44/*     */ 
45/*     */ 
46/*     */ 
47/*     */ 
48/*     */ 
49/*     */ 
50/*     */ 
51/*     */ 
52/*     */ 
53/*     */ 
54/*     */ 
55/*     */ 
56/*     */ 
57/*     */ 
58/*     */ 
59/*     */ 
60/*     */ 
61/*     */ 
62/*     */ 
63/*     */ 
64/*     */ 
65/*     */ 
66/*     */ 
67/*     */ 
68/*     */ 
69/*     */ 
70/*     */ 
71/*     */ 
72/*     */ 
73/*     */ 
74/*     */ 
75/*     */ 
76/*     */ 
77/*     */ 
78/*     */ 
79/*     */ 
80/*     */ 
81/*     */ 
82/*     */ 
83/*     */ 
84/*     */ 
85/*     */ 
86/*     */ 
87/*     */ 
88/*     */ 
89/*     */ 
90/*     */ 
91/*     */ 
92/*     */ 
93/*     */ 
94/*     */ 
95/*     */ 
96/*     */ 
97/*     */ 
98/*     */ 
99/*     */ 
100/*     */ 
101/*     */ 
102/*     */ 
103/*     */ 
104/*     */ 
105/*     */ 
106/*     */ 
107/*     */ 
108/*     */ 
109/*     */ 
110/*     */ 
111/*     */ 
112/*     */ 
113/*     */ 
114/*     */ 
115/*     */ 
116/*     */ 
117/*     */ 
118/*     */ 
119/*     */ 
120/*     */ 
121/*     */ 
122/*     */ 
123/*     */ 
124/*     */ 
125/*     */ 
126/*     */ 
127/*     */ 
128/*     */ 
129/*     */ 
130/*     */ 
131/*     */ 
132/*     */ 
133/*     */ 
134/*     */ 
135/*     */ 
136/*     */ 
137/*     */ 
138/*     */ 
139/*     */ 
140/*     */ 
141/*     */ 
142/*     */ 
143/*     */ 
144/*     */ 
145/*     */ 
146/*     */ 
147/*     */ 
148/*     */ 
149/*     */ 
150/*     */ 
151/*     */ 
152/*     */ 
153/*     */ 
154/*     */ 
155/*     */ 
156/*     */ 
157/*     */ 
158/*     */ 
159/*     */ 
160/*     */ 
161/*     */ 
162/*     */ 
163/*     */ 
164/*     */ 
165/*     */ 
166/*     */ 
167/*     */ 
168/*     */ 
169/*     */ 
170/*     */ 
171/*     */ 
172/*     */ 
173/*     */ 
174/*     */ 
175/*     */ 
176/*     */ 
177/*     */ 
178/*     */ 
179/*     */ 
180/*     */ 
181/*     */ 
182/*     */ 
183/*     */ 
184/*     */ 
185/*     */ 
186/*     */ 
187/*     */ 
188/*     */ 
189/*     */ 
190/*     */ 
191/*     */ 
192/*     */ 
193/*     */ 
194/*     */ 
195/*     */ 
196/*     */ 
197/*     */ 
198/*     */ 
199/*     */ 
200/*     */ 
201/*     */ 
202/*     */ 
203/*     */ 
204/*     */ 
205/*     */ 
206/*     */ 
207/*     */ 
208/*     */ 
209/*     */ 
210/*     */ 
211/*     */ 
212/*     */ 
213/*     */ 
214/*     */ 
215/*     */ 
216/*     */ 
217/*     */ 
218/*     */ 
219/*     */ 
220/*     */ 
221/*     */ 
222/*     */ 
223/*     */ 
224/*     */ 
225/*     */ 
226/*     */ 
227/*     */ 
228/*     */ 
229/*     */ 
230/*     */ public class Parameterized
231/*     */   extends Suite
232/*     */ {
233/* 233 */   private static final ParametersRunnerFactory DEFAULT_FACTORY = (ParametersRunnerFactory)new BlockJUnit4ClassRunnerWithParametersFactory();
234/*     */   
235/* 235 */   private static final List<Runner> NO_RUNNERS = Collections.emptyList();
236/*     */ 
237/*     */   
238/*     */   private final List<Runner> runners;
239/*     */ 
240/*     */ 
241/*     */   
242/*     */   public Parameterized(Class<?> klass) throws Throwable {
243/* 243 */     super(klass, NO_RUNNERS);
244/* 244 */     ParametersRunnerFactory runnerFactory = getParametersRunnerFactory(klass);
245/*     */     
246/* 246 */     Parameters parameters = (Parameters)getParametersMethod().getAnnotation(Parameters.class);
247/*     */     
248/* 248 */     this.runners = Collections.unmodifiableList(createRunnersForParameters(allParameters(), parameters.name(), runnerFactory));
249/*     */   }
250/*     */ 
251/*     */ 
252/*     */   
253/*     */   private ParametersRunnerFactory getParametersRunnerFactory(Class<?> klass) throws InstantiationException, IllegalAccessException {
254/* 254 */     UseParametersRunnerFactory annotation = klass.<UseParametersRunnerFactory>getAnnotation(UseParametersRunnerFactory.class);
255/*     */     
256/* 256 */     if (annotation == null) {
257/* 257 */       return DEFAULT_FACTORY;
258/*     */     }
259/* 259 */     Class<? extends ParametersRunnerFactory> factoryClass = annotation.value();
260/*     */     
261/* 261 */     return factoryClass.newInstance();
262/*     */   }
263/*     */ 
264/*     */ 
265/*     */   
266/*     */   protected List<Runner> getChildren() {
267/* 267 */     return this.runners;
268/*     */   }
269/*     */ 
270/*     */   
271/*     */   private TestWithParameters createTestWithNotNormalizedParameters(String pattern, int index, Object parametersOrSingleParameter) {
272/* 272 */     (new Object[1])[0] = parametersOrSingleParameter; Object[] parameters = (parametersOrSingleParameter instanceof Object[]) ? (Object[])parametersOrSingleParameter : new Object[1];
273/*     */     
274/* 274 */     return createTestWithParameters(getTestClass(), pattern, index, parameters);
275/*     */   }
276/*     */ 
277/*     */ 
278/*     */   
279/*     */   private Iterable<Object> allParameters() throws Throwable {
280/* 280 */     Object parameters = getParametersMethod().invokeExplosively(null, new Object[0]);
281/* 281 */     if (parameters instanceof Iterable)
282/* 282 */       return (Iterable<Object>)parameters; 
283/* 283 */     if (parameters instanceof Object[]) {
284/* 284 */       return Arrays.asList((Object[])parameters);
285/*     */     }
286/* 286 */     throw parametersMethodReturnedWrongType();
287/*     */   }
288/*     */ 
289/*     */   
290/*     */   private FrameworkMethod getParametersMethod() throws Exception {
291/* 291 */     List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(Parameters.class);
292/*     */     
293/* 293 */     for (FrameworkMethod each : methods) {
294/* 294 */       if (each.isStatic() && each.isPublic()) {
295/* 295 */         return each;
296/*     */       }
297/*     */     } 
298/*     */     
299/* 299 */     throw new Exception("No public static parameters method on class " + getTestClass().getName());
300/*     */   }
301/*     */ 
302/*     */ 
303/*     */ 
304/*     */ 
305/*     */ 
306/*     */   
307/*     */   private List<Runner> createRunnersForParameters(Iterable<Object> allParameters, String namePattern, ParametersRunnerFactory runnerFactory) throws InitializationError, Exception {
308/*     */     try {
309/* 309 */       List<TestWithParameters> tests = createTestsForParameters(allParameters, namePattern);
310/*     */       
311/* 311 */       List<Runner> runners = new ArrayList<Runner>();
312/* 312 */       for (TestWithParameters test : tests) {
313/* 313 */         runners.add(runnerFactory.createRunnerForTestWithParameters(test));
314/*     */       }
315/*     */       
316/* 316 */       return runners;
317/* 317 */     } catch (ClassCastException e) {
318/* 318 */       throw parametersMethodReturnedWrongType();
319/*     */     } 
320/*     */   }
321/*     */ 
322/*     */ 
323/*     */   
324/*     */   private List<TestWithParameters> createTestsForParameters(Iterable<Object> allParameters, String namePattern) throws Exception {
325/* 325 */     int i = 0;
326/* 326 */     List<TestWithParameters> children = new ArrayList<TestWithParameters>();
327/* 327 */     for (Object parametersOfSingleTest : allParameters) {
328/* 328 */       children.add(createTestWithNotNormalizedParameters(namePattern, i++, parametersOfSingleTest));
329/*     */     }
330/*     */     
331/* 331 */     return children;
332/*     */   }
333/*     */   
334/*     */   private Exception parametersMethodReturnedWrongType() throws Exception {
335/* 335 */     String className = getTestClass().getName();
336/* 336 */     String methodName = getParametersMethod().getName();
337/* 337 */     String message = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.", new Object[] { className, methodName });
338/*     */ 
339/*     */     
340/* 340 */     return new Exception(message);
341/*     */   }
342/*     */ 
343/*     */   
344/*     */   private static TestWithParameters createTestWithParameters(TestClass testClass, String pattern, int index, Object[] parameters) {
345/* 345 */     String finalPattern = pattern.replaceAll("\\{index\\}", Integer.toString(index));
346/*     */     
347/* 347 */     String name = MessageFormat.format(finalPattern, parameters);
348/* 348 */     return new TestWithParameters("[" + name + "]", testClass, Arrays.asList(parameters));
349/*     */   }
350/*     */   
351/*     */   @Retention(RetentionPolicy.RUNTIME)
352/*     */   @Inherited
353/*     */   @Target({ElementType.TYPE})
354/*     */   public static @interface UseParametersRunnerFactory {
355/*     */     Class<? extends ParametersRunnerFactory> value() default BlockJUnit4ClassRunnerWithParametersFactory.class;
356/*     */   }
357/*     */   
358/*     */   @Retention(RetentionPolicy.RUNTIME)
359/*     */   @Target({ElementType.FIELD})
360/*     */   public static @interface Parameter {
361/*     */     int value() default 0;
362/*     */   }
363/*     */   
364/*     */   @Retention(RetentionPolicy.RUNTIME)
365/*     */   @Target({ElementType.METHOD})
366/*     */   public static @interface Parameters {
367/*     */     String name() default "{index}";
368/*     */   }
369/*     */ }
370
371
372/* Location:              C:\Users\CAR\Desktop\sab\SAB_projekat_1920\SAB_projekat_1920\SAB_projekat_1920.jar!\org\junit\runners\Parameterized.class
373 * Java compiler version: 5 (49.0)
374 * JD-Core Version:       1.1.3
375 */
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 TestWithParameters

Trigger getTestClass code on LambdaTest Cloud Grid

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