@Test
    void testPreemptiveAuthenticationFailure() throws Exception {
        final Authenticator authenticator = Mockito.spy(new BasicTestAuthenticator("test:test", "test realm"));
        configureServerWithBasicAuth(authenticator,
                bootstrap -> bootstrap
                        .register("*", new EchoHandler()));
        final HttpHost target = startServer();

        final TestClient client = client();

        final HttpClientContext context = HttpClientContext.create();
        final AuthCache authCache = new BasicAuthCache();
        authCache.put(target, new BasicScheme());
        context.setAuthCache(authCache);
        context.setCredentialsProvider(CredentialsProviderBuilder.create()
                .add(target, "test", "stuff".toCharArray())
                .build());

        final HttpGet httpget = new HttpGet("/");
        client.execute(target, httpget, context, response -> {
            final HttpEntity entity1 = response.getEntity();
            Assertions.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getCode());
            Assertions.assertNotNull(entity1);
            EntityUtils.consume(entity1);
            return null;
        });

        Mockito.verify(authenticator).authenticate(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