添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Is there any chance to find out why test keycloak create user fails?

the code looks like:

public void registerUser(UserRegistrationRequest userRegistrationRequest) throws Exception {
       Keycloak keycloak = KeycloakBuilder
                .builder()
                .serverUrl(keycloakUrl)
                .realm(keycloakRealm)
                .username(keycloakUser)
                .password(keycloakPassword)
                .clientId(keycloakClientId)
                .build();
        CredentialRepresentation credential = createPasswordCredentials(userRegistrationRequest.getPassword());
        UserRepresentation user = new UserRepresentation();
        user.setEmail(userRegistrationRequest.getEmail());
        user.setCredentials(Collections.singletonList(credential));
        user.setEnabled(true);
            // Get realm
            RealmResource realmResource = keycloak.realm(keycloakRealm);
            UsersResource usersResource = realmResource.users();
            Response response = usersResource.create(user);
            String userId = CreatedResponseUtil.getCreatedId(response);
            UserResource u = usersResource.get(userId);
            List<String> actions = new LinkedList<>();
            actions.add(UserModel.RequiredAction.VERIFY_EMAIL.name());
            u.executeActionsEmail(keycloakClientId, registrationSuccessUrl, actions);
        }catch(Exception e){
            if (isHttpClientErrorWithConflictStatus(e)) {
                throw new ConflictUserRegistrationException("The user already exists");
            throw new Exception(e);

the test looks like:

@ExtendWith({SpringExtension.class})
class AuthClientTest {
    @MockBean
    private RemoteAuthClient authClient;
    @Mock
    UsersResource usersResource;
    @BeforeEach
    void setup() {
        authClient = new RemoteAuthClient(
                "testAuthAdaptorUrl.com",
                "testRealm",
                "testUser",
                "testPassword",
                "testClientId",
                "testRedirectionUrl"
    @Test
    void registerUser_shouldMakeRequestViaRestTemplate() throws Exception {
        Response mockResponse = buildMockResponse(Response.Status.CREATED, "created");
        when(usersResource.create(any(UserRepresentation.class))).thenReturn(mockResponse);
        authClient.registerUser(new UserRegistrationRequest("[email protected]", "1234567890", "1234567890"));
        verify(usersResource, times(1)).create(any(UserRepresentation.class));
    private Response buildMockResponse(Response.Status status, String msgEntity) {
        Response mockResponse = mock(Response.class);
        when(mockResponse.readEntity(String.class)).thenReturn(msgEntity);
        when(mockResponse.getStatus()).thenReturn(status.getStatusCode());
        when(mockResponse.getStatusInfo()).thenReturn(status);
        return mockResponse;

The error:

Caused by: javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: org.apache.http.client.ClientProtocolException
	at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:328)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:443)
	at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:149)
	at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
	at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
	at com.sun.proxy.$Proxy59.grantToken(Unknown Source)
	at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:90)
	at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:70)
	at org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:65)
	at org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:579)
	at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:440)
	at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:149)
	at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
	at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
	at com.sun.proxy.$Proxy65.create(Unknown Source)
	at uk.co.deloitte.alpha.ps.application.client.RemoteAuthClient.registerUser(RemoteAuthClient.java:79)
	... 91 more
Caused by: org.apache.http.client.ClientProtocolException
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
	at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:323)
	... 107 more
Caused by: org.apache.http.ProtocolException: Target host is not specified
	at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:71)
	at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
	... 110 more

Thankssss in advance