### Question:
CompositeTrustManager extends AbstractTrustManager { @Override public void checkClientTrusted(X509Certificate[] clientCertificates, String authType) throws CertificateException { for (TrustManager trustManager : trustManagers) { try { X509TrustManager x509TrustManager = (X509TrustManager) trustManager; x509TrustManager.checkClientTrusted(clientCertificates, authType); return; } catch(CertificateException ex) { } } throw new CertificateException("Certificate is not trusted by any of the trust managers"); } CompositeTrustManager(TrustManager[] trustManagers); @Override void checkClientTrusted(X509Certificate[] clientCertificates, String authType); @Override void checkServerTrusted(X509Certificate[] serverCertificates, String authType); @Override X509Certificate[] getAcceptedIssuers();  }### Answer:
@Test public void testCheckClientTrustedByCA() throws CertificateException { doNothing().when(x509TrustManager).checkClientTrusted(any(), any()); doThrow(new CertificateException("TOFU FAILED")).when(trustOnFirstUseManager).checkClientTrusted(any(), any()); trustManager.checkClientTrusted(new X509Certificate[]{certificate}, "s"); }
@Test public void testCheckClientTrustedByTOFU() throws CertificateException { doNothing().when(trustOnFirstUseManager).checkClientTrusted(any(), any()); doThrow(new CertificateException("CA FAILED")).when(x509TrustManager).checkClientTrusted(any(), any()); trustManager.checkClientTrusted(new X509Certificate[]{certificate}, "s"); }
@Test public void testCheckClientFailedWhenBothTMFailed() throws CertificateException { doThrow(new CertificateException("TOFU FAILED")).when(trustOnFirstUseManager).checkClientTrusted(any(), any()); doThrow(new CertificateException("CA FAILED")).when(x509TrustManager).checkClientTrusted(any(), any()); try { trustManager.checkClientTrusted(new X509Certificate[]{certificate}, "s"); failBecauseExceptionWasNotThrown(Exception.class); } catch (Exception ex) { assertThat(ex).isInstanceOf(CertificateException.class); } }