A custom annotation is a user-defined marker or metadata that you create to add extra information to your test code, which can be processed at runtime or compile time to influence test behavior.
Click to reveal answer
beginner
How do you define a custom annotation in Java?
You define a custom annotation using the @interface keyword, specify retention policy (like RUNTIME), and optionally target elements (like METHOD or FIELD).
Click to reveal answer
intermediate
Why use custom annotations in Selenium tests?
Custom annotations help organize tests, add metadata like test priority or author, and enable reusable logic such as custom waits or setup steps, making tests cleaner and easier to maintain.
Click to reveal answer
intermediate
What is the RetentionPolicy.RUNTIME in custom annotations?
RetentionPolicy.RUNTIME means the annotation is available during runtime via reflection, allowing test frameworks to read and act on the annotation while tests run.
Click to reveal answer
beginner
Give an example of a simple custom annotation for marking tests as 'Critical'.
Example:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface CriticalTest {
String value() default "";
}
This annotation can be placed on test methods to mark them as critical.
Click to reveal answer
Which keyword is used to create a custom annotation in Java?
A@interface
B@annotation
Cinterface
D@custom
✗ Incorrect
The @interface keyword is used to define a custom annotation in Java.
What does RetentionPolicy.RUNTIME mean for a custom annotation?
AAnnotation is available only in source code
BAnnotation is available at runtime via reflection
CAnnotation is discarded after compilation
DAnnotation is only for documentation
✗ Incorrect
RetentionPolicy.RUNTIME means the annotation is kept in the compiled class and can be accessed during runtime.
Why might you use custom annotations in Selenium tests?
ATo add metadata and control test execution
BTo replace WebDriver entirely
CTo write CSS selectors
DTo compile Java code faster
✗ Incorrect
Custom annotations add metadata like priority or author and help control test execution flow.
Which Java package is commonly used to access annotations at runtime?
Ajava.util.stream
Bjava.io
Cjava.lang.reflect
Djava.net
✗ Incorrect
The java.lang.reflect package provides classes to inspect annotations at runtime.
What element type would you specify to apply a custom annotation only to methods?
AElementType.FIELD
BElementType.TYPE
CElementType.PARAMETER
DElementType.METHOD
✗ Incorrect
ElementType.METHOD restricts the annotation usage to methods only.
Explain how to create and use a custom annotation in Selenium Java tests.
Think about how annotations add metadata and how Selenium tests can read them.
You got /5 concepts.
Describe the benefits of using custom annotations in test automation.
Consider how annotations help manage many tests in a project.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of creating custom annotations in Selenium Java tests?
easy
A. To automatically generate test reports without any code
B. To replace all Selenium WebDriver methods with custom ones
C. To write test cases without using any Java code
D. To add metadata that can control test behavior or provide extra information
Solution
Step 1: Understand what annotations do
Annotations add metadata to code elements like methods or classes, influencing how tests run or are processed.
Step 2: Identify the role of custom annotations
Custom annotations let testers add their own metadata to control or describe tests beyond built-in annotations.
Final Answer:
To add metadata that can control test behavior or provide extra information -> Option D
Quick Check:
Custom annotations = metadata control [OK]
Hint: Custom annotations add info to tests, not replace code [OK]
2. Which of the following is the correct syntax to define a custom annotation named TestInfo in Java?
easy
A. public @interface TestInfo { String author(); int priority() default 1; }
B. public class TestInfo { String author(); int priority() default 1; }
C. @interface TestInfo { public String author; public int priority = 1; }
D. annotation TestInfo { String author(); int priority(); }
Solution
Step 1: Recall Java annotation syntax
Annotations use the keyword @interface and define methods without bodies for elements.
Step 2: Check public @interface TestInfo { String author(); int priority() default 1; } syntax
public @interface TestInfo { String author(); int priority() default 1; } correctly uses public @interface TestInfo and defines methods with optional default values.
Final Answer:
public @interface TestInfo { String author(); int priority() default 1; } -> Option A
Quick Check:
Annotation syntax = @interface + methods [OK]
Hint: Annotations use @interface keyword, not class or annotation [OK]
Common Mistakes:
Using class instead of @interface
Declaring fields instead of methods inside annotation
Missing default keyword for optional elements
3. Given the custom annotation and usage below, what will be the output when printTestInfo() is called?
import java.lang.reflect.Method;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface TestInfo {
String author();
int priority() default 1;
}
public class SampleTest {
@TestInfo(author = "Alice", priority = 3)
public void testMethod() {}
public static void printTestInfo() throws Exception {
Method m = SampleTest.class.getMethod("testMethod");
TestInfo info = m.getAnnotation(TestInfo.class);
System.out.println(info.author() + ":" + info.priority());
}
}
medium
A. Compilation error
B. Alice:1
C. Alice:3
D. null:0
Solution
Step 1: Understand annotation retrieval via reflection
The method getAnnotation(TestInfo.class) fetches the annotation instance on testMethod.
Step 2: Check annotation values
The annotation has author="Alice" and priority=3, so printing them outputs "Alice:3".
Final Answer:
Alice:3 -> Option C
Quick Check:
Reflection reads annotation values correctly [OK]
Hint: Annotation values print as set, defaults overridden [OK]
Common Mistakes:
Assuming default priority is used instead of 3
Expecting null because of missing retention policy
Thinking reflection causes compile error
4. Consider this custom annotation definition:
@interface TestInfo {
String author();
int priority() default 1;
}
And this usage:
@TestInfo(author = "Bob")
public void test() {}
Why might the following code throw a NullPointerException when accessing priority()?
TestInfo info = testMethod.getAnnotation(TestInfo.class);
int p = info.priority();
medium
A. The priority method is missing in the annotation definition
B. The annotation is not retained at runtime, so info is null
C. The author value is missing, causing priority to be null
D. Annotations cannot have default values
Solution
Step 1: Check annotation retention policy
If the annotation lacks @Retention(RetentionPolicy.RUNTIME), it is not available at runtime, so getAnnotation returns null.
Step 2: Understand NullPointerException cause
Since info is null, calling info.priority() throws NullPointerException.
Final Answer:
The annotation is not retained at runtime, so info is null -> Option B
Quick Check:
Missing runtime retention = null annotation [OK]
Hint: Always add @Retention(RetentionPolicy.RUNTIME) for runtime use [OK]
Common Mistakes:
Assuming default values cause null
Thinking missing author causes error
Believing annotations can't have defaults
5. You want to create a custom annotation @StepInfo to mark test steps with a description and an optional severity level (default "LOW"). You also want to process these annotations at runtime to print all step descriptions with their severity. Which combination of annotation elements and retention policy is correct to achieve this?
hard
A. @Retention(RetentionPolicy.RUNTIME) public @interface StepInfo { String description(); String severity() default "LOW"; }
B. @Retention(RetentionPolicy.CLASS) public @interface StepInfo { String description(); String severity() default "LOW"; }
C. @Retention(RetentionPolicy.SOURCE) public @interface StepInfo { String description(); String severity(); }
D. public @interface StepInfo { String description(); String severity() default "LOW"; }
Solution
Step 1: Choose correct retention for runtime processing
To access annotations at runtime via reflection, use @Retention(RetentionPolicy.RUNTIME).
Step 2: Define annotation elements with default values
Define description() as required and severity() with default "LOW" for optional use.
Final Answer:
@Retention(RetentionPolicy.RUNTIME) public @interface StepInfo { String description(); String severity() default "LOW"; } -> Option A
Quick Check:
Runtime retention + default value = correct annotation [OK]
Hint: Use RUNTIME retention to read annotations during tests [OK]
Common Mistakes:
Using CLASS or SOURCE retention prevents runtime access
Omitting retention causes default CLASS retention
Not providing default for optional elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.