I’m trying to test a LoginView that redirects authenticated users to a URL specified in a query parameter. The production code works, but I can’t figure out how to test it with Karibu. It’s a medium complexity server-client auth flow that I want to verify.
The code being tested:
@Override
public void beforeEnter(BeforeEnterEvent event) {
if (authenticatedUser.get().isPresent()) {
Map<String, List<String>> params = event.getLocation().getQueryParameters().getParameters();
if (params.containsKey("redirect")) {
String redirectTarget = params.get("redirect").getFirst();
if (redirectTarget != null && !redirectTarget.isBlank()) {
setOpened(false);
event.getUI().getPage().setLocation(redirectTarget); // <- This is what I need to test
return;
}
}
setOpened(false);
event.forwardTo("");
}
}
My test:
@Test
void givenRedirectQueryParam_afterLogin_thenRedirect() {
accountRepository.deleteAll();
Account account = new AccountBuilder().build();
accountRepository.save(account);
// Step 1: Navigate to login with redirect param (unauthenticated)
Map<String, List<String>> queryParams = Map.of("redirect", List.of("/api/agent/callback"));
QueryParameters queryParameters = new QueryParameters(queryParams);
getCurrent().navigate(LoginView.class, queryParameters);
// Step 2: Authenticate the user (simulating successful login)
// Navigate to login AGAIN with the same redirect param
loginAndNavigate(LoginView.class, queryParameters);
LoginOverlay loginOverlay = LocatorJ._get(LoginOverlay.class);
assertThat(loginOverlay.isOpened()).isFalse();
verifyCurrentViewPath("/api/agent/callback");
}
The issue appears to be that setLocation() performs a browser-level redirect that Karibu can’t simulate. I can see in the debugger that setLocation() is called with the correct URL, but Karibu doesn’t actually navigate there.
Is there a recommended way to test this behavior? Should I:
- Refactor to use
forwardTo()instead (but my redirect target is a Spring controller endpoint, not a Vaadin route) - Use a different testing approach for this scenario
- Accept this as a Karibu limitation and rely on E2E tests
Any guidance would be appreciated! Using Vaadin 24 with Karibu Testing.