### Question:
ClientCredentialsGrantTypeHandler extends GrantTypeHandler { public AccessToken handle(OAuth2Request request, ClientRegistration clientRegistration, OAuth2ProviderSettings providerSettings) throws InvalidRequestException, ServerException, UnauthorizedClientException, InvalidScopeException, NotFoundException, InvalidClientException { for (final ClientCredentialsRequestValidator requestValidator : requestValidators) { requestValidator.validateRequest(request, clientRegistration); } final Set<String> scope = Utils.splitScope(request.<String>getParameter(SCOPE)); final Set<String> validatedScope = providerSettings.validateAccessTokenScope(clientRegistration, scope, request); final String validatedClaims = providerSettings.validateRequestedClaims( (String) request.getParameter(OAuth2Constants.Custom.CLAIMS)); final String grantType = request.getParameter(GRANT_TYPE); final AccessToken accessToken = tokenStore.createAccessToken(grantType, BEARER, null, clientRegistration.getClientId(), clientRegistration.getClientId(), null, validatedScope, null, null, validatedClaims, request); providerSettings.additionalDataToReturnFromTokenEndpoint(accessToken, request); if (validatedScope != null && !validatedScope.isEmpty()) { accessToken.addExtraData(SCOPE, Utils.joinScope(validatedScope)); } tokenStore.updateAccessToken(accessToken); return accessToken; } @Inject  ClientCredentialsGrantTypeHandler(ClientAuthenticator clientAuthenticator,
            List<ClientCredentialsRequestValidator> requestValidators, TokenStore tokenStore,
            OAuth2UrisFactory urisFactory,OAuth2ProviderSettingsFactory providerSettingsFactory); AccessToken handle(OAuth2Request request, ClientRegistration clientRegistration,
            OAuth2ProviderSettings providerSettings);  }### Answer:
@Test public void shouldHandle() throws Exception { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); Set<String> validatedScope = new HashSet<String>(); AccessToken accessToken = mock(AccessToken.class); given(uris.getTokenEndpoint()).willReturn("Endpoint"); given(clientAuthenticator.authenticate(request, "Endpoint")).willReturn(clientRegistration); given(clientRegistration.getClientId()).willReturn("CLIENT_ID"); given(providerSettings.validateAccessTokenScope(eq(clientRegistration), anySetOf(String.class), eq(request))).willReturn(validatedScope); given(tokenStore.createAccessToken(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anySetOf(String.class), Matchers.<RefreshToken>anyObject(), anyString(), anyString(), eq(request))) .willReturn(accessToken); final AccessToken actualAccessToken = grantTypeHandler.handle(request); verify(requestValidator).validateRequest(eq(request), Matchers.<ClientRegistration>anyObject()); verify(providerSettings).additionalDataToReturnFromTokenEndpoint(accessToken, request); verify(accessToken, never()).addExtraData(eq("scope"), anyString()); assertEquals(actualAccessToken, accessToken); }
@Test public void shouldHandleAndIncludeScopeInAccessToken() throws Exception { OAuth2Request request = mock(OAuth2Request.class); ClientRegistration clientRegistration = mock(ClientRegistration.class); Set<String> validatedScope = Collections.singleton("SCOPE"); AccessToken accessToken = mock(AccessToken.class); given(uris.getTokenEndpoint()).willReturn("Endpoint"); given(clientAuthenticator.authenticate(request, "Endpoint")).willReturn(clientRegistration); given(providerSettings.validateAccessTokenScope(Matchers.<ClientRegistration>anyObject(), anySetOf(String.class), eq(request))).willReturn(validatedScope); given(clientRegistration.getClientId()).willReturn("CLIENT_ID"); given(tokenStore.createAccessToken(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anySetOf(String.class), Matchers.<RefreshToken>anyObject(), anyString(), anyString(), eq(request))) .willReturn(accessToken); final AccessToken actualAccessToken = grantTypeHandler.handle(request); verify(requestValidator).validateRequest(eq(request), Matchers.<ClientRegistration>anyObject()); verify(providerSettings).additionalDataToReturnFromTokenEndpoint(accessToken, request); verify(accessToken).addExtraData(eq("scope"), anyString()); assertEquals(actualAccessToken, accessToken); }