Eclipse 4 Testing - Tutorial

Lars Vogel, Simon Scholz (c) 2009, 2016 vogella GmbH Version 0.3, 06.09.2016 :docinfodir: ../ :vgwort: Eclipse4Testing

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.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.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 {
        @Test
        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 4 RCP development

I hope you enjoyed this tutorial. You find this tutorial and much more information also in the http://www.vogella.com/books/eclipsercp.html - Eclipse 4 RCP book from this author.

4. About this website

4.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

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

See Licence.

Unresolved directive in <stdin> - include::./web/vgwort/{vgwort}.adoc[]