Back to top

vogella training Training Books

Eclipse 4 Testing - Tutorial

Based on Eclipse 4.2

Lars Vogel

Version 5.6

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

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. Testing user interface components
1.3. Testing Dependency Injection
2. Thank you
3. Questions and Discussion
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. Testing user interface components

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.

1.3. Testing Dependency Injection

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);
  }
} 

2. Thank you

Please help me to support this article:

Flattr this

3. Questions and Discussion

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.

4. Links and Literature

4.1. Source Code

Source Code of Examples

http://wiki.eclipse.org/E4 Eclipse E4 - Wiki

Eclipse RCP

Eclipse EMF

Dependency Injection