I have a class as follows:
class Abc{
static void fun() throws AbcException{
throw new Exception("Some exception message");
}
static{
try{
fun();
}catch(AbcException ex){
ex.printStackTrace();
}
}
}
fun() calls some other function and there is a chain of functions following it which throws AbcException if the environment is not configured properly. In Unit Test environment, I faced the AbcException as explained below.
(Also, there are many other methods in this class which I have mentioned)
I want to create an instance of class Abc in my test file. I'm using JUnit 4 and Mockito 4.8.1. I tried the following :
I wrote my test as follows:
@Before
public void setup(){
try (MockedStatic<Abc> abc = mockStatic(Abc.class)) {
// some test setup
}
}
This didn't help because the AbcException was thrown when mockStatic(Abc.class) was called.
Is there a way to mock Abc class successfully in this case?