During migration of a Java Spring Boot project to Keycloak 26.1.0, I encountered a problem while trying to migrate the tests that use KeycloakSession. The following is how it was in the code (for version 20.0.5):
public static KeycloakSession prepareKeycloakSession() {
KeycloakSession session = KeycloakSessionUtil.getKeycloakSession();
RealmModel realm = Mockito.mock(RealmModel.class);
session.getContext().setRealm(realm);
return session;
}
I tried instantiating the session from DefaultKeycloakSessionFactory, but it is abstract. KeycloakSessionUtil.getKeycloakSession() returns null. I also tried injecting it using @Context to no avail. Also, I considered mocking it, but it is used in great many use cases and mocking all of it would require a very profound knowledge of Keycloak, which I lack completely.