I have to refactor a Java program created with Eclipse RCP. My predecessor handed over a ViewPart Implementation through several calls down to the class I want to write Unit Tests for. The ViewPart is handed over for exception handling purposes. Every time an error occurs the error message will be written onto the ViewPart’s StatusLineManager.
What I want to do now is to change this exception handling process, because it causes me a lot of problems when I want to make my Unit Tests.
Is there a way to pass these Exceptions to the ViewPart without handing it down over several method calls?
My initial ideas to create some kind of ExceptionMessenger class that will be initialized every time an exception occurs. But I didn’t find a way to access the ViewPart then.
edit
code example:
class A {
void func() {
B.funcB(arg, arg1, arg2, ViewPart view):
}
}
class B {
void funcB(arg, arg1, arg2, ViewPart view) {
C.funcC(arg, arg1, ViewPart view);
}
}
class C {
void funcC(arg, arg1, ViewPart view) {
try{
doSomeThing():
catch(Exception e) {
doSomeErrorMessages(e, view);
}
}
void doSomeErrorMessages(e, ViewPart view) {
SomeOtherClass.writeToStatusLine( StatusCategory.Processing, e.getMessage(), view);
}
}
class SomeOtherClass {
void writeToStatusLine(final StatusCategory category, final String message, final ViewPart view) {
IActionBars bars = view.getViewSite().getActionBars();
IStatusLineManager statusLineManagerRef = bars.getStatusLineManager();
statusLineManagerRef.setErrorMessage(null); // clear any error msg
doWhatEver();
}
}
If I understood your task correctly, there are two parts where you are dealing with exceptions:
ViewPart). Since this is not part of exception handling to recover or otherwise cope with exceptional conditions, I would deliberately not use Java’s exception handling for that, i.e. avoid misusing exception handling for control flow. Hence I think your predecessor’s design to pass down theViewPartto then pass the Exceptions as parameters to yourViewPartis good.An alternative would be to let the exceptions get thrown upward and then at some sensible border of layer/framework do the visualisation. But
ViewPart(and definitely mix the two parts listed above).What are your problems when unit testing?