'Mocking local object method invocation to throw exception
I have classes in the following way,
class A {
void methodA() throws Exception;
}
class MyExceptionWrapper extends Exception {
...
}
class B {
void methodB() throws MyExceptionWrapper {
try {
A a = new A();
a.methodA();
} catch(Exception e) {
throw new MyExceptionWrapper(e);
}
}
}
I need to test methodB throws MyExceptionWrapper when there is any exception happened in the implementation. I need to include line throw new MyExceptionWrapper(e); in test coverage.
I do not want to inject instance of A as it is an implementation detail. I also feel it is pointless to test this exception line as it tightly depends on implementation logic. Is it okay to skip such cases from code coverage and reduce target percentage ? what is the best practice here ?
Appreciate your help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
