12. Java Generics in Page Objects & Test Utilities

Generics in Page Transitions

public class BasePage {
    public <T extends BasePage> T navigateTo(String url, Class<T> pageClass) {
        System.out.println("Navigating to: " + url);
        try {
            return pageClass.getDeclaredConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException("Failed to initialize page: " + pageClass.getName(), e);
        }
    }
}

Task

  • Implement GenericResponse<T>:
  • 1. Generic class GenericResponse<T> with fields: int statusCode and T body.
  • 2. Constructor accepting statusCode and body.
  • 3. Getters getStatusCode() and getBody().
πŸ’‘Common doubts
Did you like the problem?
Lesson 12 of 44
Java
Loading code editor…
Next