I would like to test specific parts of the UI by writing a Java class with a main method, following an example found in Lars Vogel's book about Eclipse 4. Something similar to this:
public static void main(Object...args) {
Display display = new Display();
Shell shell = new Shell(display);
MyComponentToTest c = new MyComponentToTest();
c.buildUI(shell, display); // @PostConstruct method
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
MyComponentToTest is a Part and uses @Inject and other annotations to signal lifecycle events and receive objects from the EclipseContext during creation. So I transformed the code, following another example found in Lars' book about testing dependency injection :
public static void main(Object...args) {
Display display = new Display();
Shell shell = new Shell(display);
IEclipseContext ctx = EclipseContextFactory.create();
// populate ctx : add the components required by MyComponentToTest + display & shell
ContextInjectionFactory.make(MyComponentToTest.class, ctx);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
When running this application, I get a NullPointerException, caused by the fact that EclipseContext declares this variable
static private final IEclipseContextDebugger debugAddOn = ContextDebugHelper.getDebugger();
Its initialization invokes OSGi-related code that obviously doesn't work if not run in a OSGi runtime.
Is there a way to deactivate this OSGi code or change something in the configuration so that I can test my component ?
Thanks in advance for any help.