Bird
Raised Fist0
Selenium Javatesting~20 mins

Custom annotations in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Annotations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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");
    }
}
ATest executed
BTest executed printed 3 times
CCompilation error due to missing annotation processor
DRuntime error due to annotation misuse
Attempts:
2 left
💡 Hint
Custom annotations alone do not change method execution unless processed.
❓ assertion
intermediate
2: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
    }
}
Areturn method.getDeclaredAnnotations().contains(SlowTest.class);
Breturn method.isAnnotationPresent(SlowTest.class);
Creturn method.getAnnotation(SlowTest.class) != null;
Dreturn method.getAnnotationsByType(SlowTest.class).length == 0;
Attempts:
2 left
💡 Hint
Use the standard Java reflection method to check annotation presence.
🔧 Debug
advanced
2: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);
AThe annotation defaults to CLASS retention, so it is not available at runtime.
BThe annotation must be public to be detected at runtime.
CThe annotation requires @Target to specify applicable elements.
DThe annotation processor is missing, causing detection failure.
Attempts:
2 left
💡 Hint
Check the default retention policy of Java annotations.
❓ framework
advanced
2: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?
AAdd a while loop inside the test method to retry based on @Retry annotation value.
BUse @BeforeEach to loop the test method execution count times manually.
CCreate a JUnit 5 Extension that reads @Retry and reruns the test method up to count times on failure.
DUse @Test(timeout=3) to retry the test 3 times automatically.
Attempts:
2 left
💡 Hint
JUnit 5 extensions allow intercepting test execution lifecycle.
🧠 Conceptual
expert
2: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?
AThey enforce strict typing on test method parameters.
BThey automatically improve test execution speed without code changes.
CThey replace the need for writing assertions in test methods.
DThey allow adding metadata to tests that can be processed to modify test behavior dynamically.
Attempts:
2 left
💡 Hint
Think about how annotations can influence test execution.

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

  1. Step 1: Understand what annotations do

    Annotations add metadata to code elements like methods or classes, influencing how tests run or are processed.
  2. 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.
  3. Final Answer:

    To add metadata that can control test behavior or provide extra information -> Option D
  4. Quick Check:

    Custom annotations = metadata control [OK]
Hint: Custom annotations add info to tests, not replace code [OK]
Common Mistakes:
  • Thinking custom annotations replace WebDriver methods
  • Assuming they auto-generate reports without code
  • Believing tests can run without Java code
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

  1. Step 1: Recall Java annotation syntax

    Annotations use the keyword @interface and define methods without bodies for elements.
  2. 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.
  3. Final Answer:

    public @interface TestInfo { String author(); int priority() default 1; } -> Option A
  4. 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

  1. Step 1: Understand annotation retrieval via reflection

    The method getAnnotation(TestInfo.class) fetches the annotation instance on testMethod.
  2. Step 2: Check annotation values

    The annotation has author="Alice" and priority=3, so printing them outputs "Alice:3".
  3. Final Answer:

    Alice:3 -> Option C
  4. 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

  1. Step 1: Check annotation retention policy

    If the annotation lacks @Retention(RetentionPolicy.RUNTIME), it is not available at runtime, so getAnnotation returns null.
  2. Step 2: Understand NullPointerException cause

    Since info is null, calling info.priority() throws NullPointerException.
  3. Final Answer:

    The annotation is not retained at runtime, so info is null -> Option B
  4. 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

  1. Step 1: Choose correct retention for runtime processing

    To access annotations at runtime via reflection, use @Retention(RetentionPolicy.RUNTIME).
  2. Step 2: Define annotation elements with default values

    Define description() as required and severity() with default "LOW" for optional use.
  3. Final Answer:

    @Retention(RetentionPolicy.RUNTIME) public @interface StepInfo { String description(); String severity() default "LOW"; } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard