Support free tutorials

vogella training Training Books

Eclipse 4 Testing - Tutorial

Based on Eclipse 4.2

Lars Vogel

Version 5.7


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. Learn more about Eclipse 4 RCP development
3. About this website
3.1. Donate to support free tutorials
3.2. Questions and discussion
3.3. License for this tutorial and its code
4. Links and Literature
4.1. Source Code

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. 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.

3. About this website

3.1. Donate to support free tutorials

Please consider a contribution Support free tutorials if this article helped you. It will help to maintain our content and our Open Source activities.

3.2. Questions and discussion

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

3.3. License for this tutorial and its code

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

4. Links and Literature

4.1. Source Code

Source Code of Examples Eclipse E4 - Wiki

Eclipse RCP

Eclipse EMF

Dependency Injection