0

I want to assert that a certain exception (SSLHandshakeException) is thrown when running some code.

assertThatThrownBy(() -> {
        // some code
    }).isInstanceOf(SSLHandshakeException.class);

However, this fails because the failure trace says:

java.lang.AssertionError: 
Expecting:
  <javax.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException:     Received fatal alert: handshake_failure>
to be an instance of:
  <javax.net.ssl.SSLHandshakeException>
but was:
  <"javax.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Checking on ProcessingException would work, but is too general. I need to make sure that the code snippet fails because of SSL handshake.

How can I change it in a way that the "second" exception should be considered?

Patze
  • 859
  • 10
  • 19

2 Answers2

2

You can simply use hasCauseInstanceOf.

Joel Costigliola
  • 6,308
  • 27
  • 35
1

A getCause() should help here to access the inner exception.

assertThatThrownBy(() -> {
        // some code
    }).getCause().isInstanceOf(SSLHandshakeException.class);
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222