Home Tutorials Training Consulting Products Books Company Donate Contact us

vogella commercial offerings

Training Events

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

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.

package com.vogella.tasks.ui.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 {

    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.

package com.vogella.tasks.ui.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();
        // 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 dependencies are necessary.

The Orbit Repositories can be found on the Orbit Downloads website.

testing target platform

3. Learn more about Eclipse RCP development

I hope you enjoyed this tutorial. You find an extended online training at the Eclipse RCP online training website.

Nothing listed.

5. vogella training and consulting support

Copyright © 2012-2019 vogella GmbH. Free use of the software examples is granted under the terms of the Eclipse Public License 2.0. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.