Testing Exception different than thrown Exception

Viewed 28

I have a method that throws Exception. Later on inside the method, there is a conditional checking the Instance type of the Exception cause, namely SocketTimeoutException:

public methodName() throws Exception{
  //some code...then...
  try{        
     //code
    } catch(Exception ex){
     if(ex.getCause() instanceof SocketTimeoutException){
      //Here is where I want to increase code coverage.
      //this part is not covered in the test.
     }
 }

This does not work:

when(object.methodCall()).thenThrow(SocketTimeoutException);

My question: is there any way to increase code coverage by accessing the SocketTimeoutException branch of the code?

Having a Unit test throw Exception will not lead to the SocketTimeoutException, right?

Can I inject the instance type of the Exception to be SocketTimeoutException?

0 Answers
Related