Does anyone have any recommendations on properly unit testing an EventListenerProvider implementation? I suppose this would actually be more functional/integration testing, as it would ideally involve bringing up a Keycloak instance, installing/activating the listener, and performing some actions, and then validating that an event had been received by the listener.
I’ve been thinking about either using the org.keycloak.testsuite.KeycloakServer in junit or the testcontainers approach. I wanted to see if anyone here had a recommendation or template.
Also, I tried another approach that uses the org.keycloak.testsuite.KeycloakServer. The downside is that because keycloak-testsuite-utils isn’t in Maven Central, you have to build it yourself in order to use this method. The upside is that it works with JUnit4, which I had the requirement of sticking to. This also ran much faster than the testcontainers approach, but is less flexible in features, control, and similarity to an actual production environment.
This is how to use it in JUnit4:
@ClassRule public static KeycloakSuite server = KeycloakSuite.SERVER;
@BeforeClass
public static void commonSetup() {
Keycloak keycloak = server.client();
//do setup here that is common to all tests in this class
}
@Test
public void exampleTest() {
Keycloak keycloak = server.client();
RealmRepresentation realm = keycloak.realm("master").toRepresentation();
assertThat(realm.getId(), is("master"));
}