Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class or interface keyword instead of @interface.
Naming the annotation incorrectly.
✗ Incorrect
The annotation name should be @TestInfo as defined in the code.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of the custom annotation.
Missing the @ symbol before the annotation name.
✗ Incorrect
The custom annotation @TestInfo is applied with author and date attributes.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using RetentionPolicy.SOURCE or CLASS which are not available at runtime.
Forgetting to import RetentionPolicy.
✗ Incorrect
RetentionPolicy.RUNTIME makes the annotation available during runtime for reflection.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ElementType.FIELD instead of METHOD.
Using RetentionPolicy.CLASS which is not runtime.
✗ Incorrect
RetentionPolicy.RUNTIME keeps the annotation at runtime, and ElementType.METHOD restricts usage to methods.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Test instead of @TestInfo.
Calling date() instead of author() to get the author value.
✗ Incorrect
Use TestInfo.class to check and get the annotation, then call author() method to get the author value.