Version 5.6
Copyright © 2009, 2010 , 2011, 2012 Lars Vogel
03.11.2011
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 14.02.2009 | Lars Vogel |
created |
| Revision 0.2 - 5.6 | 16.02.2009 - 03.11.2011 | Lars Vogel |
bug fixes and enhancements |
Table of Contents
In general all Java classes in an Eclipse 4 application can be tested similarly to other Java applications. This description highlights the special Eclipse 4 constructs.
Eclipse 4 makes testing for user interface components easier compared to Eclipse 3.x. As the components have no hard dependency on the framework, these components can be directly used in a test.
For example take the following part.
package com.example.e4.rcp.todo.parts; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; public class TodoOverviewPart { @PostConstruct public void createControls(Composite parent){ parent.setLayout(new GridLayout(2, false)); Button button = new Button(parent, SWT.PUSH); button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); button.setText("Load Data"); Label label = new Label(parent, SWT.NONE); label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); label.setText("Data not available"); } @PreDestroy public void dispose(){ } }
This part can be created via a simple Java Program which has the SWT library included in its classpath.
package com.example.e4.rcp.todo.parts; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class TodoOverviewPartTest { public static void main(String... main) { Display display = new Display(); Shell shell = new Shell(display); TodoOverviewPart part = new TodoOverviewPart(); part.createControls(shell); shell.open(); // Create and check the event loop while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }
The above code can be easily changed to a unit test. Your test class can create the class, provide the required dependencies and run the tests.
You can also include the process of dependency injection into the
test.
Create your own
IEclipseContext
and use the
ContextInjectionFactory.make()
method to create the object which should be tested.
The following code shows an example of how to create your own context and construct the object based on this construct. This test needs to run as Plugin test.
package testing; import org.eclipse.e4.core.contexts.ContextInjectionFactory; import org.eclipse.e4.core.contexts.EclipseContextFactory; import org.eclipse.e4.core.contexts.IEclipseContext; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class CreateContextText { @Test public void testCreation() { IEclipseContext context = EclipseContextFactory.create(); // Put objects into the context context.set("myvalue1", "For testing"); // More things, for example a LayoutManager MyClass test = ContextInjectionFactory.make(MyClass.class, context); } }
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
http://wiki.eclipse.org/E4 Eclipse E4 - Wiki