### Question:
CompositeTrustManager extends AbstractTrustManager { @Override public void checkServerTrusted(X509Certificate[] serverCertificates, String authType) throws CertificateException { for (TrustManager trustManager : trustManagers) { try { final X509TrustManager x509TrustManager = (X509TrustManager) trustManager; x509TrustManager.checkServerTrusted(serverCertificates, 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 testCheckServerTrustedByCA() throws CertificateException { doNothing().when(x509TrustManager).checkServerTrusted(any(), any()); doThrow(new CertificateException("TOFU FAILED")).when(trustOnFirstUseManager).checkServerTrusted(any(), any()); trustManager.checkServerTrusted(new X509Certificate[]{certificate}, "s"); }
@Test public void testCheckServerTrustedByTOFU() throws CertificateException { doNothing().when(trustOnFirstUseManager).checkServerTrusted(any(), any()); doThrow(new CertificateException("CA FAILED")).when(x509TrustManager).checkServerTrusted(any(), any()); trustManager.checkServerTrusted(new X509Certificate[]{certificate}, "s"); }
@Test public void testCheckServerFailedWhenBothTMFailed() throws CertificateException { doThrow(new CertificateException("TOFU FAILED")).when(trustOnFirstUseManager).checkServerTrusted(any(), any()); doThrow(new CertificateException("CA FAILED")).when(x509TrustManager).checkServerTrusted(any(), any()); try { trustManager.checkServerTrusted(new X509Certificate[]{certificate}, "s"); failBecauseExceptionWasNotThrown(Exception.class); } catch (Exception ex) { assertThat(ex).isInstanceOf(CertificateException.class); } }