How to use getQualifier method of net.serenitybdd.junit.runners.QualifierFinder class

Best Serenity JUnit code snippet using net.serenitybdd.junit.runners.QualifierFinder.getQualifier

Run Serenity JUnit automation tests on LambdaTest cloud grid

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

copy
1package net.serenitybdd.junit.runners;
2
3import net.thucydides.core.reflection.MethodFinder;
4import net.thucydides.junit.annotations.Qualifier;
5import net.thucydides.junit.internals.MethodInvoker;
6
7import java.lang.reflect.Method;
8import java.lang.reflect.Modifier;
9import java.util.List;
10
11/**
12 * When running data-driven tests, each set of test data needs a way to distinguish it from the others.
13 * This class provides means to distinguish instantiated test cases. By default, the toString() method is used.
14 * If a public method that returns a String is marked with the Qualifier annotation, this method will be used instead.
15 */
16public class QualifierFinder {
17
18    private final Object testCase;
19
20    public QualifierFinder(final Object testCase) {
21        this.testCase = testCase;
22    }
23
24    public static QualifierFinder forTestCase(final Object testCase) {
25        return new QualifierFinder(testCase);
26    }
27
28    public String getQualifier() {
29        if (hasQualifierAnnotation()) {
30            String qualifierValue = (String) MethodInvoker.on(testCase).run(getQualifiedMethod());
31            return (qualifierValue != null) ? qualifierValue : "<UNSPECIFIED>";
32        } else {
33            return "";
34        }
35    }
36
37    private Method getQualifiedMethod() {
38        List<Method> methods = MethodFinder.inClass(testCase.getClass()).getAllMethods();
39        for (Method each : methods) {
40            if (each.getAnnotation(Qualifier.class) != null) {
41                checkModifiersFor(each);
42                return each;
43            }
44        }
45        return null;
46    }
47
48    private void checkModifiersFor(final Method each) {
49        int modifiers = each.getModifiers();
50        if (Modifier.isStatic(modifiers)) {
51            throw new IllegalArgumentException("Qualifier method must not be static");
52        }
53        if (!Modifier.isPublic(modifiers)) {
54            throw new IllegalArgumentException("Qualifier method must be public");
55        }
56        if (each.getReturnType() != String.class) {
57            throw new IllegalArgumentException("Qualifier method must return a String");
58        }
59    }
60
61    private boolean hasQualifierAnnotation() {
62        return (getQualifiedMethod() != null);
63    }
64}
65
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 QualifierFinder

Trigger getQualifier code on LambdaTest Cloud Grid

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