简体   繁体   中英

How to set up environment for an Eclipse JUnit plugin test

There are a few questions such as this one which touch on the subject of running an Eclipse JUnit Plugin Test, but I have not been able to find an example of how to trigger some aspect of the plugin I'm testing.

I've followed this documentation which mentions a test harness, but I haven't found any more documentation on the harness. I can run JUnit tests as Eclipse Plugin Tests, but I'm not sure how to make them do useful things.

With that context, here are my specific questions:

  • How do I set up the context within which the plugin under test is invoked (eg, "IResource xyz is selected")?
  • How do I get a handle to the plugin under test in order to invoke it? I'm assuming there's a way to invoke one of its extensions?
  • Can I programmatically inject resources into the Eclipse JUnit Test workspace?

I've managed to stumble my way through most of this, here are my steps and some sample code for anyone interested:

  1. A plugin test needs to be defined in an Eclipse Plugin project. I prefer to do this in a separate project from the plugin I'm testing. If you follow this approach, you'll need to add the plugin you're testing as a dependency.

  2. You create the test class like a regular JUnit test class.

  3. You can run your test class by using the "Run as JUnit Plugin Test" run configuration. (Right click --> Run as --> JUnit Plugin Test). This will create a new instance of Eclipse and run your test inside it.

Below is some sample code for a test that creates some resources in the junit workspace using content from the test plugin's src directory, then makes some assertions.

Setting up resources:

package org.sampletest;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IWorkbench;
import org.junit.Test;

import com.example.myplugin.MyPlugin

public class MyPluginTest{

    @Test
    public void testMyPlugin() throws Exception{
         // assume an empty workspace - "Run as JUnit Plugin Test can be configured to clear the workspace before each run.
         String name = "myPluginTestProject";
         IProjectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(name);
         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
         project.create(desc, new NullProgressMonitor());
         project.open(new NullProgressMonitor());
         IFile file = project.getFile("myPluginTestFile");
         InputStream source = getClass().getClassLoader().getResourceAsStream("sampleFile");
         file.create(source, IFile.FORCE, null);

         // Assuming MyPlugin has some operation on an IStructuredSelection
         MyPlugin plugin = new MyPlugin();
         IStructuredSelection selection = new StructuredSelection(file);
         plugin.invoke(selection);

         // Make some assertions...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM