How to use getAnnotation method of org.junit.runner.Description class

Best junit code snippet using org.junit.runner.Description.getAnnotation

Run junit automation tests on LambdaTest cloud grid

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

copy
1/*
2 * Copyright 2011 Red Hat, Inc.
3 *
4 * This file is made available under the terms of the Common Public License
5 * v1.0 which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
7 */
8import java.io.PrintStream;
9import java.lang.annotation.Annotation;
10import java.lang.reflect.Method;
11import net.sourceforge.jnlp.annotations.KnownToFail;
12import net.sourceforge.jnlp.annotations.Remote;
13import net.sourceforge.jnlp.browsertesting.Browsers;
14
15import org.junit.internal.JUnitSystem;
16import org.junit.runner.Description;
17import org.junit.runner.Result;
18import org.junit.runner.notification.Failure;
19import org.junit.runner.notification.RunListener;
20
21public class LessVerboseTextListener extends RunListener {
22
23    private PrintStream writer;
24    private boolean testFailed = false;
25    private int  totalK2F=0;
26    private int  failedK2F=0;
27    private int  passedK2F=0;
28    private int  ignoredK2F=0;
29
30    public LessVerboseTextListener(JUnitSystem system) {
31        writer= system.out();
32    }
33
34    @Override
35    public void testStarted(Description description) throws Exception {
36        testFailed = false;
37    }
38
39    @Override
40    public void testIgnored(Description description) throws Exception {
41        writer.println("Ignored: " + description.getClassName() + "." + description.getMethodName());
42        printK2F(writer, null, description);
43        printRemote(writer, description);
44    }
45
46
47    @Override
48    public void testFailure(Failure failure) {
49        testFailed = true;
50        writer.println("FAILED: " + failure.getTestHeader() + " " + failure.getMessage());
51        printK2F(writer,true,failure.getDescription());
52        printRemote(writer, failure.getDescription());
53    }
54
55    @Override
56    public void testFinished(org.junit.runner.Description description) throws Exception {
57        if (!testFailed) {
58            writer.println("Passed: " + description.getClassName() + "." + description.getMethodName());
59            printK2F(writer,false,description);
60            printRemote(writer, description);
61        }
62    }
63
64    @Override
65    public void testRunFinished(Result result) throws Exception {
66        int passed = result.getRunCount() - result.getFailureCount() - result.getIgnoreCount();
67        int failed = result.getFailureCount();
68        int ignored = result.getIgnoreCount();
69        writer.println("Total tests run: "+result.getRunCount()+"; From  those : " + totalK2F + " known to fail");
70        writer.println("Test known to fail: passed: " + passedK2F + "; failed: " + failedK2F + "; ignored: " + ignoredK2F);
71        writer.println("Test results: passed: " + passed + "; failed: " + failed + "; ignored: " + ignored);
72
73    }
74
75    private void printK2F(PrintStream writer, Boolean failed, Description description) {
76        try {
77            KnownToFail k2f = getK2F(description);
78            boolean thisTestIsK2F = false;
79            if (k2f != null){
80                //determine if k2f in the current browser
81                Browsers[] br = k2f.failsIn();
82                if(0 == br.length){ //@KnownToFail with default optional parameter failsIn={}
83                    thisTestIsK2F = true;
84                }else{
85                    for(Browsers b : br){
86                        if(description.toString().contains(b.toString())){
87                            thisTestIsK2F = true;
88                        }
89                    }
90                }
91            }
92
93            if( thisTestIsK2F ){
94                totalK2F++;
95                if (failed != null) {
96                    if (failed) {
97                        failedK2F++;
98                    } else {
99                        passedK2F++;
100                    }
101                } else {
102                    ignoredK2F++;
103                }
104                if (failed != null && !failed) {
105                    writer.println(" - WARNING This test is known to fail, but have passed!");
106                } else {
107                    writer.println(" - This test is known to fail");
108                }
109            }
110
111
112        } catch (Exception ex) {
113            ex.printStackTrace();
114        }
115    }
116
117
118    @SuppressWarnings("unchecked")
119    public static <T extends Annotation> T getAnnotation(Class<?> q, String methodName, Class<T> a) {
120        try {
121            if (q != null) {
122                T rem = q.getAnnotation(a);
123                if (rem != null) {
124                    return rem;
125                }
126                String qs = methodName;
127                if (qs.contains(" - ")) {
128                    qs = qs.replaceAll(" - .*", "");
129                }
130                Method qm = q.getMethod(qs);
131                if (qm != null) {
132                    rem = qm.getAnnotation(a);
133                    return rem;
134
135                }
136            }
137        } catch (Exception ex) {
138            ex.printStackTrace();
139        }
140        return null;
141    }
142
143    public static KnownToFail getK2F(Description description) {
144        return getAnnotation(description.getTestClass(), description.getMethodName(), KnownToFail.class);
145    }
146
147    public static Remote getRemote(Description description) {
148        return getAnnotation(description.getTestClass(), description.getMethodName(), Remote.class);
149
150    }
151
152    private void printRemote(PrintStream writer, Description description) {
153        try {
154            Remote rem = getRemote(description);
155            if (rem != null) {
156                writer.println(" - This test is running remote content, note that failures may be caused by broken target application or connection");
157            }
158        } catch (Exception ex) {
159            ex.printStackTrace();
160        }
161    }
162}
163
Full Screen
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 */
18package org.apache.phoenix.util;
19
20import org.junit.Ignore;
21import org.junit.runner.Description;
22import org.junit.runner.notification.Failure;
23import org.junit.runner.notification.RunListener;
24import org.junit.runner.notification.RunNotifier;
25import org.junit.runners.BlockJUnit4ClassRunner;
26import org.junit.runners.model.FrameworkMethod;
27import org.junit.runners.model.InitializationError;
28import org.junit.runners.model.Statement;
29
30public class RunUntilFailure extends BlockJUnit4ClassRunner {  
31    private boolean hasFailure;
32
33    public RunUntilFailure(Class<?> klass) throws InitializationError {  
34        super(klass);  
35    }  
36
37    @Override  
38    protected Description describeChild(FrameworkMethod method) {  
39        if (method.getAnnotation(Repeat.class) != null &&  
40                method.getAnnotation(Ignore.class) == null) {  
41            return describeRepeatTest(method);  
42        }  
43        return super.describeChild(method);  
44    }  
45
46    private Description describeRepeatTest(FrameworkMethod method) {  
47        int times = method.getAnnotation(Repeat.class).value();  
48
49        Description description = Description.createSuiteDescription(  
50                testName(method) + " [" + times + " times]",  
51                method.getAnnotations());  
52
53        for (int i = 1; i <= times; i++) {  
54            description.addChild(Description.createTestDescription(  
55                    getTestClass().getJavaClass(),  
56                    testName(method) + "-" + i));  
57        }  
58        return description;  
59    }  
60
61    @Override  
62    protected void runChild(final FrameworkMethod method, RunNotifier notifier) {  
63        Description description = describeChild(method);  
64
65        if (method.getAnnotation(Repeat.class) != null &&  
66                method.getAnnotation(Ignore.class) == null) {  
67            runRepeatedly(methodBlock(method), description, notifier);  
68        }  
69        super.runChild(method, notifier);  
70    }  
71
72    private void runRepeatedly(Statement statement, Description description,  
73            RunNotifier notifier) {
74        notifier.addListener(new RunListener() {
75            @Override
76            public void testFailure(Failure failure) {
77                hasFailure = true;
78            }
79        });
80        for (Description desc : description.getChildren()) {  
81            if (hasFailure) {
82                notifier.fireTestIgnored(desc);
83            } else if(!desc.isSuite()) {
84                runLeaf(statement, desc, notifier);
85            }
86        }  
87    }  
88
89}  
90
91
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 getAnnotation code on LambdaTest Cloud Grid

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