2

My Spring Boot application is connecting to third parties servers using a third librabry/framework.

One of these remote servers (not yet available) will require mutual TLS (HTTPS) connection. My code will provide the P12 and password to the framework in order to connect to this remote server. I need to test this part with JUnit integration test.

MockServer is already used for our JUnit IT but not yet for mutual TLS authentication and, as a newby, I'm quite confused on how to implement this.

Form the doc I added in my JUnit test the following configuration:

@BeforeAll
public static void initServer(){

  // ensure https will use SSL context defined by MockServer to allow
  // dynamically generated certificates to be accepted
  HttpsURLConnection.setDefaultSSLSocketFactory(new KeyStoreFactory(new 
  MockServerLogger()).sslContext().getSocketFactory());

  File certif = ResourceUtils.getFile("classpath:mtls/test-cert.crt");
  ConfigurationProperties.tlsMutualAuthenticationCertificateChain(certif.getPath());
  ConfigurationProperties.tlsMutualAuthenticationRequired(true);

  mockServer = ClientAndServer.startClientAndServer(80,443);

  etc…
}

But nothing works, I'm missing some code in my test?

In log I can see for i.e:

MockServerEventLog - no tls for connection

I cannot find any clear and simple tutorial for this use case. Any help would be greatfull.

So how to implement a full JUnit integration test using a MockServer with mutual TLS authentication?

dur
  • 15,689
  • 25
  • 79
  • 125
ThierryC
  • 1,794
  • 3
  • 19
  • 34

1 Answers1

2

Was having this issue, I added .withSecure(true) when creating expectations.

@Test
public void myTest() {
    mockServer
        .withSecure(true)
        .when(request()
            .withPath("/test"))
        .respond(response()
            .withBody("hello world"));
}
dur
  • 15,689
  • 25
  • 79
  • 125
Jasper
  • 61
  • 1
  • 2