@Test
    void testReauthentication() throws Exception {
        final BasicSchemeFactory myBasicAuthSchemeFactory = new BasicSchemeFactory() {

            @Override
            public AuthScheme create(final HttpContext context) {
                return new BasicScheme() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getName() {
                        return "MyBasic";
                    }

                };
            }

        };

        final CredentialsProvider credsProvider = Mockito.mock(CredentialsProvider.class);
        Mockito.when(credsProvider.getCredentials(Mockito.any(), Mockito.any()))
                .thenReturn(new UsernamePasswordCredentials("test", "test".toCharArray()));

        final RequestConfig config = RequestConfig.custom()
                .setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
                .build();
        final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
                .register("MyBasic", myBasicAuthSchemeFactory)
                .build();

        final Authenticator authenticator = new BasicTestAuthenticator("test:test", "test realm") {

            private final AtomicLong count = new AtomicLong(0);

            @Override
            public boolean authenticate(final URIAuthority authority, final String requestUri, final String credentials) {
                final boolean authenticated = super.authenticate(authority, requestUri, credentials);
                if (authenticated) {
                    return this.count.incrementAndGet() % 4 != 0;
                }
                return false;
            }
        };

        configureServer(bootstrap -> bootstrap
                .setExchangeHandlerDecorator(requestHandler ->
                        new AuthenticatingDecorator(requestHandler, authenticator) {

                            @Override
                            protected void customizeUnauthorizedResponse(final ClassicHttpResponse unauthorized) {
                                unauthorized.removeHeaders(HttpHeaders.WWW_AUTHENTICATE);
                                unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, "MyBasic realm=\"test realm\"");
                            }

                        }
                )
                .register("*", new EchoHandler()));

        final HttpHost target = startServer();

        configureClient(builder -> builder
                .setDefaultAuthSchemeRegistry(authSchemeRegistry)
        );
        final TestClient client = client();

        final HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        for (int i = 0; i < 10; i++) {
            final HttpGet httpget = new HttpGet("/");
            httpget.setConfig(config);
            client.execute(target, httpget, context, response -> {
                final HttpEntity entity = response.getEntity();
                Assertions.assertEquals(HttpStatus.SC_OK, 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