com.tngtech.keycloakmock.junit.KeycloakMockRule Maven / Gradle / Ivy
Show all versions of mock-junit Show documentation
package com.tngtech.keycloakmock.junit;
import com.tngtech.keycloakmock.api.KeycloakMock;
import com.tngtech.keycloakmock.api.ServerConfig;
import com.tngtech.keycloakmock.api.TokenConfig;
import javax.annotation.Nonnull;
import org.junit.rules.ExternalResource;
/**
* A JUnit4 rule to automatically start and stop the keycloak mock.
*
* Example use:
*
*
* {@literal @}ClassRule
* public static KeycloakMockRule mock = new KeycloakMockRule();
*
* {@literal @}Test
* public void testStuff() {
* String token = mock.getAccessToken(aTokenConfig().build());
* }
*
*/
public class KeycloakMockRule extends ExternalResource {
@Nonnull private final KeycloakMock mock;
/**
* Create a mock instance with default configuration.
*
* The instance generates tokens for realm 'master'.
*
*
The JWKS endpoint listens at port 8000.
*
*
The JWKS endpoint is served via HTTP.
*
* @see KeycloakMockRule#KeycloakMockRule(ServerConfig)
*/
public KeycloakMockRule() {
mock = new KeycloakMock();
}
/**
* Create a mock instance for a given server configuration.
*
* @param serverConfig the port of the mock to run
* @see KeycloakMockRule#KeycloakMockRule()
*/
public KeycloakMockRule(@Nonnull final ServerConfig serverConfig) {
mock = new KeycloakMock(serverConfig);
}
/**
* Get a signed access token for the given parameters.
*
* @param tokenConfig the configuration of the token to generate
* @return an access token in compact JWT form
* @see TokenConfig.Builder
*/
@Nonnull
public String getAccessToken(@Nonnull final TokenConfig tokenConfig) {
return mock.getAccessToken(tokenConfig);
}
@Override
protected void before() {
mock.start();
}
@Override
protected void after() {
mock.stop();
}
}