@Test
    void testBearerTokenAuthentication() throws Exception {
        final SecureRandom secureRandom = SecureRandom.getInstanceStrong();
        secureRandom.setSeed(System.currentTimeMillis());
        final StringBuilder buf = new StringBuilder();
        for (int i = 0; i < 16; i++) {
            buf.append(CHARS.charAt(secureRandom.nextInt(CHARS.length() - 1)));
        }
        final String token = buf.toString();
        configureServer(bootstrap -> bootstrap
                .setExchangeHandlerDecorator(requestHandler ->
                        new AuthenticatingDecorator(
                                requestHandler,
                                new BearerAuthenticationHandler(),
                                new BasicTestAuthenticator(token, "test realm"))
                )
                .register("*", new EchoHandler()));

        final HttpHost target = startServer();

        final TestClient client = client();

        final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class);

        final HttpClientContext context1 = HttpClientContext.create();
        context1.setCredentialsProvider(credsProvider);
        final HttpGet httpget1 = new HttpGet("/");
        client.execute(target, httpget1, context1, response -> {
            final HttpEntity entity = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getCode());
            Assertions.assertNotNull(entity);
            EntityUtils.consume(entity);
            return null;
        });
        Mockito.verify(credsProvider).getCredentials(
                Mockito.eq(new AuthScope(target, "test realm", "bearer")), Mockito.any());

        final HttpClientContext context2 = HttpClientContext.create();
        Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
                .thenReturn(new BearerToken(token));
        context2.setCredentialsProvider(credsProvider);
        final HttpGet httpget2 = new HttpGet("/");
        client.execute(target, httpget2, context2, response -> {
            final HttpEntity entity = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
            Assertions.assertNotNull(entity);
            EntityUtils.consume(entity);
            return null;
        });

        final HttpClientContext context3 = HttpClientContext.create();
        Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
                .thenReturn(new BearerToken(token + "-expired"));
        context3.setCredentialsProvider(credsProvider);
        final HttpGet httpget3 = new HttpGet("/");
        client.execute(target, httpget3, context3, response -> {
            final HttpEntity entity = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getCode());
            Assertions.assertNotNull(entity);
            EntityUtils.consume(entity);
            return null;
        });
    }
Unit tests for automatic client authentication.
httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java