NOW Hiring

Quick links

Eclipse 4 Testing - Tutorial

Based on Eclipse 4.6

Lars Vogel

Simon Scholz

Version 6.0


Testing Eclipse 4 components

This tutorial gives an overview how to test Eclipse 4 components.

Table of Contents

1. Testing Eclipse 4 application
1.1. General testing
1.2. Fragment projects
1.3. Testing user interface components
1.4. Testing dependency injection
2. Exercise - Add test dependencies
3. Learn more about Eclipse 4 RCP development
4. About this website
5. Links and Literature

1. Testing Eclipse 4 application

1.1. General testing

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.

1.2. Fragment projects

Tests for Eclipse plug-ins are typically contained in a fragment project. This way the tests can access all classes in their host plug-in.

1.3. Testing user interface components

Eclipse classes that are using the application model have no hard dependency on the Eclipse framework. Therefore you can test these components directly with JUnit.

For example take the following part.


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 {
  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,
    button.setText("Load Data");
    Label label = new Label(parent, SWT.NONE);
    label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
    label.setText("Data not available");
  public void dispose(){

This part can be created via a simple Java Program which has the SWT library included in its classpath.


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();
    // create and check the event loop
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())


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.

1.4. Testing dependency injection

You can 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 JUnit Plug-in 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 {
  public void testCreation() {
    IEclipseContext context = EclipseContextFactory.create();
    // prepare the context for the test
    context.set("myvalue1", "For testing");
    // more things, for example a LayoutManager
    MyClass test = ContextInjectionFactory.make(MyClass.class, context);

2. Exercise - Add test dependencies

Popular test frameworks, like JUnit, Hamcrest, Mockito and others can be obtained from different updatesites.

Eclipse Orbit for example provides a Test category which contains these test frameworks mentioned earlier.

In case a target definition file does not exist yet, it needs to be created. For standard RCP applications the following dependies are necessary.

The Orbit Repositories can be found here: Orbit Downloads.

3. Learn more about Eclipse 4 RCP development

I hope you enjoyed this tutorial. You find this tutorial and much more information also in the Eclipse 4 RCP book from this author.

4. About this website

5. Links and Literature Eclipse E4 - Wiki

Eclipse RCP

Eclipse EMF

Dependency Injection