I have a final class and want to test the catch block code through JUnit test cases. But I am not sure how can I mock the returnObject() which is being called inside the constructor, such that it throws SomeException. I used PowerMockito @Spy but in vain. I tried making the code inside returnObject() but the code is in such a manner that I cannot mock them to throw an exception. So the only option I have is to use spy the returnObject() to throw an exception.
public final class MyClass{
public MyClass(){
try{
returnObject();
}
catch(SomeException se){
//Logic which needs to be Junit tested
}
}
private SomeObjectClass returnObject() throws SomeException{
return <StaticMethodCall of another class>;
}
}