Writing RedDeer JUnit Test Extensions

Version 1

    This article will show you what is and how to simply write test extensions with JBoss RedDeer.


    We will go though:

    • Extension introduction and what they are suitable for
    • How to configure plugin for using RedDeer JUnit extension
    • How to create extension implementation

    Overview


    JUnit test extensions for RedDeer are operations that can be automatically invoked before of after each and every RedDeer UI test. As a JUnit test programmer you would use @Before and @After for that. But what if there are actions that you need to do for every test and what if you you need to so some actions only for particular environment. That time you would like something more universal. And that’s where RedDeer JUnit Test Extension comes into action.
    From Eclipse development perspective if you’re familiar with Extensions that’s practically everything you need to know.
    There are two Extension points defined in JUnit, org.jboss.reddeer.junit.before.test and org.jboss.reddeer.junit.after.test which are exactly the extensions points we are going to use.



    It’s handy to create your extensions(s) in separate plugin which gives you freedom to use or not to use them without affecting other plugins. You can simply add, resp. remove your plug-in to use, resp. not to use your extensions as you wish. That might be handy when you’re testing your plugins on various set of eclipse plugins, for example as RCP where you might not need the extension and under full Eclipse platform you might want to have it. Of course you can also implement additional switch to disable or enable it regardless to your running OSGi platform.


    Also note that there are already several  pre-created extension in RedDeer that are available out-of-box. There are these:

    • CloseWelcomeScreenExt
    • DoNotDownloadMavenIndexesExt
    • SetOpenAssociatedPerspectiveExt
    • CloseAllShellsExt


    It’s quite obvious what they are for and you can find more details here but it should give you some picture where is the usage area for RedDeer JUnit Requirements.


    Implementing your own test extension


    We will try to implement requirement that will check the Always run in background setting under General Preferences and will set it if not set before each test. I should note here that might be various approaches how to do the same things but it’s up to your decision which one you will choose.


    Implementing consists of these step:

    • Create (or open existing) eclipse plug-in
    • Add and setup extension
    • Create class implementing proper interface


    Prerequisites

    You will need JBDS or Eclipse (Luna or Mars) and RedDeer last stable (0.7) or master version, follow these links:


    Creating plug-in

    Creating eclipse plugin is easy and straight-forward task and there is nothing specific for our needs. You can also use RedDeer Test plugin wizard which will give you some dependencies predefined.


    Add and setup extension

    When you open plug-in manifest (META-INF/MANIFEST.MF) and your project is created by RedDeer Wizard you will see this after adding extension. If you created simple eclipse plug-in you need to add at least org.jboss.reddeer.junit plugin into your dependencies plugin.


    Once you have it go to Extensions tab and Add new extension.



    As we want action which will be performed before each test let’s select org.jboss.reddeer.junit.before.test as a desired extension point.


    Now add Extension and also set “client” class under this extension definition which allows us to point to desired implementation.


    Once you click on class link Eclipse will provide you properly filled Java class wizard and you can simply click Finish. As you can see it’s simple class implementing IBeforeTest interface. As you can assume if we use org.jboss.reddeer.junit.after.test extension point class will have to implement IAfterTest. Now we create runBeforeTest() code that will check and set if needed the “Always run in background” settings. We can also specify when it has to run by defining hasToRun() code. Right now just return true as we don't need more advanced control over the extensions.


    package org.myproject.reddeer.extension;


    import org.eclipse.core.runtime.Platform;

    import org.eclipse.core.runtime.preferences.IPreferencesService;

    import org.jboss.reddeer.eclipse.jdt.ui.WorkbenchPreferenceDialog;

    import org.jboss.reddeer.junit.extensionpoint.IBeforeTest;

    import org.jboss.reddeer.swt.impl.button.LabeledCheckBox;


    public class AlwaysRunInBackgroundExt implements IBeforeTest {


      @Override

      public void runBeforeTest() {

        IPreferencesService prefServices = Platform.getPreferencesService();

        String value = prefServices.getString("org.eclipse.ui.workbench", "RUN_IN_BACKGROUD",

            "true", null);

        boolean isSet = value.equalsIgnoreCase("true");

        if (!isSet) {

          WorkbenchPreferenceDialog preferencesDialog = new WorkbenchPreferenceDialog();

          preferencesDialog.open();

          preferencesDialog.select("General");

          new LabeledCheckBox("Always run in background").click();

          preferencesDialog.ok();   

        }

      }


      @Override

      public boolean hasToRun() {

        return true;

      }

    }


    Now we have JUnit RedDeer extension implemented and we can start to use it. To test if it let's create simple RedDeer Test (preferably in another plugin).


    package org.myproject.reddeer.test;


    import static org.junit.Assert.*;


    import org.eclipse.core.runtime.Platform;

    import org.eclipse.core.runtime.preferences.IPreferencesService;

    import org.jboss.reddeer.junit.runner.RedDeerSuite;

    import org.junit.Test;

    import org.junit.runner.RunWith;


    @RunWith(RedDeerSuite.class)

    public class TestExtension {


      @Test

      public void test() {

        IPreferencesService prefServices = Platform.getPreferencesService();

        String value = prefServices.getString("org.eclipse.ui.workbench", "RUN_IN_BACKGROUND",

            "n/a", null);


        boolean isSet = value.equalsIgnoreCase("true");

        assertTrue("Alway run in background is expected to be set by the extension", isSet);

      }


    }



    Now we have a test and we can run it as RedDeer test.



    After the test is executed we should see green report of passing test. Once we remove extension plugin from target platform this test will not pass as the variable was not set by the extension and it’s false by default. You can check that by your own.


    As long as your extension plugin is part of running platform before each test extension facility will take care that desired property is set as expected without any additional effort and that was the goal.