Challenge - 5 Problems
Custom Annotations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a custom annotation usage in Selenium Java
Consider the following custom annotation and its usage in a Selenium test class. What will be the output when the test method runs?
Selenium Java
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import org.junit.jupiter.api.Test; @Retention(RetentionPolicy.RUNTIME) @interface Retry { int value() default 1; } public class SampleTest { @Retry(3) @Test public void flakyTest() { System.out.println("Test executed"); } }
Attempts:
2 left
💡 Hint
Custom annotations alone do not change method execution unless processed.
✗ Incorrect
The custom annotation @Retry is defined and used, but no code processes it to retry the test. So the test method runs once and prints "Test executed" once.
❓ assertion
intermediate2:00remaining
Assertion to verify custom annotation presence
Given a Selenium test method annotated with a custom annotation @SlowTest, which assertion correctly verifies that the method has this annotation at runtime?
Selenium Java
import java.lang.reflect.Method; public class TestUtils { public static boolean hasSlowTestAnnotation(Method method) { // Fill in assertion here } }
Attempts:
2 left
💡 Hint
Use the standard Java reflection method to check annotation presence.
✗ Incorrect
The method isAnnotationPresent(Class) returns true if the annotation is present. Option B also works but is less direct. Option B is invalid because getDeclaredAnnotations() returns Annotation[], not Class objects. Option B checks for zero length which means annotation is absent.
🔧 Debug
advanced2:00remaining
Debugging missing retention policy in custom annotation
A custom annotation @CriticalTest is defined without specifying @Retention. When trying to detect this annotation at runtime in Selenium tests, it is always missing. What is the cause?
Selenium Java
public @interface CriticalTest {
String value() default "";
}
// Later in test code
boolean present = testMethod.isAnnotationPresent(CriticalTest.class);Attempts:
2 left
💡 Hint
Check the default retention policy of Java annotations.
✗ Incorrect
If @Retention is not specified, the default is CLASS retention, meaning the annotation is in the class file but not available at runtime via reflection. To detect at runtime, RetentionPolicy.RUNTIME must be specified.
❓ framework
advanced2:00remaining
Implementing retry logic using custom annotation in Selenium Java
Which approach correctly implements retry logic for a Selenium test method annotated with @Retry(count=3) using JUnit 5 extensions?
Attempts:
2 left
💡 Hint
JUnit 5 extensions allow intercepting test execution lifecycle.
✗ Incorrect
JUnit 5 Extensions can intercept test execution and implement retry logic by reading custom annotations. Options B and C are manual and not recommended. Option C is invalid as timeout does not retry tests.
🧠 Conceptual
expert2:00remaining
Why use custom annotations in Selenium test automation?
Which of the following best explains the main advantage of using custom annotations in Selenium test automation frameworks?
Attempts:
2 left
💡 Hint
Think about how annotations can influence test execution.
✗ Incorrect
Custom annotations add metadata that frameworks or extensions can read to change how tests run, such as retries, categorization, or conditional execution. They do not improve speed automatically, replace assertions, or enforce typing.