0
0
Selenium Javatesting~10 mins

Custom annotations in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a custom annotation named @TestInfo.

Selenium Java
public @interface [1] {
    String author() default "";
    String date() default "";
}
Drag options to blanks, or click blank then click option'
ATestAnnotation
BInfoTest
CCustomTest
DTestInfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class or interface keyword instead of @interface.
Naming the annotation incorrectly.
2fill in blank
medium

Complete the code to apply the @TestInfo annotation to a test method.

Selenium Java
@[1](author = "Alice", date = "2024-06-01")
public void sampleTest() {
    // test code
}
Drag options to blanks, or click blank then click option'
ATestInfo
BTest
CCustomTest
DInfoTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of the custom annotation.
Missing the @ symbol before the annotation name.
3fill in blank
hard

Fix the error in the annotation retention policy to make @TestInfo available at runtime.

Selenium Java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention([1])
public @interface TestInfo {
    String author() default "";
    String date() default "";
}
Drag options to blanks, or click blank then click option'
ARetentionPolicy.CLASS
BRetentionPolicy.SOURCE
CRetentionPolicy.RUNTIME
DRetentionPolicy.DOCUMENTED
Attempts:
3 left
💡 Hint
Common Mistakes
Using RetentionPolicy.SOURCE or CLASS which are not available at runtime.
Forgetting to import RetentionPolicy.
4fill in blank
hard

Fill both blanks to define a custom annotation with runtime retention and method target.

Selenium Java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;

@Retention([1])
@Target([2])
public @interface TestInfo {
    String author() default "";
    String date() default "";
}
Drag options to blanks, or click blank then click option'
ARetentionPolicy.RUNTIME
BElementType.METHOD
CRetentionPolicy.CLASS
DElementType.FIELD
Attempts:
3 left
💡 Hint
Common Mistakes
Using ElementType.FIELD instead of METHOD.
Using RetentionPolicy.CLASS which is not runtime.
5fill in blank
hard

Fill all three blanks to retrieve the author value from the @TestInfo annotation at runtime.

Selenium Java
import java.lang.reflect.Method;

public class TestRunner {
    public static void main(String[] args) throws Exception {
        Method method = TestClass.class.getMethod("testMethod");
        if (method.isAnnotationPresent([1].class)) {
            [1] annotation = method.getAnnotation([1].class);
            System.out.println("Author: " + annotation.[2]());
        }
    }
}
Drag options to blanks, or click blank then click option'
ATestInfo
Bauthor
Cdate
DTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @TestInfo.
Calling date() instead of author() to get the author value.