@Test
    void testAuthenticationCredentialsCachingReAuthenticationOnDifferentRealm() throws Exception {
        configureServerWithBasicAuth(new Authenticator() {

            @Override
            public boolean authenticate(final URIAuthority authority, final String requestUri, final String credentials) {
                if (requestUri.equals("/this")) {
                    return "test:this".equals(credentials);
                } else if (requestUri.equals("/that")) {
                    return "test:that".equals(credentials);
                } else {
                    return "test:test".equals(credentials);
                }
            }

            @Override
            public String getRealm(final URIAuthority authority, final String requestUri) {
                if (requestUri.equals("/this")) {
                    return "this realm";
                } else if (requestUri.equals("/that")) {
                    return "that realm";
                } else {
                    return "test realm";
                }
            }

        }, bootstrap -> bootstrap.register("*", new EchoHandler()));
        final HttpHost target = startServer();

        final DefaultAuthenticationStrategy authStrategy = Mockito.spy(new DefaultAuthenticationStrategy());

        configureClient(builder -> builder
                .setTargetAuthenticationStrategy(authStrategy)
        );
        final TestClient client = client();

        final CredentialsProvider credsProvider = CredentialsProviderBuilder.create()
                .add(new AuthScope(target, "this realm", null), "test", "this".toCharArray())
                .add(new AuthScope(target, "that realm", null), "test", "that".toCharArray())
                .build();

        final HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);

        final HttpGet httpget1 = new HttpGet("/this");

        client.execute(target, httpget1, context, response -> {
            final HttpEntity entity1 = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
            Assertions.assertNotNull(entity1);
            EntityUtils.consume(entity1);
            return null;
        });

        final HttpGet httpget2 = new HttpGet("/this");

        client.execute(target, httpget2, context, response -> {
            final HttpEntity entity2 = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
            Assertions.assertNotNull(entity2);
            EntityUtils.consume(entity2);
            return null;
        });

        final HttpGet httpget3 = new HttpGet("/that");

        client.execute(target, httpget3, context, response -> {
            final HttpEntity entity3 = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_OK, response.getCode());
            Assertions.assertNotNull(entity3);
            EntityUtils.consume(entity3);
            return null;
        });

        Mockito.verify(authStrategy, Mockito.times(2)).select(Mockito.any(), Mockito.any(), Mockito.any());
    }
Unit tests for automatic client authentication.
httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestClientAuthentication.java