Reputation: 994
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:
Upvotes: 0
Views: 1512
Reputation: 994
I've managed to stumble my way through most of this, here are my steps and some sample code for anyone interested:
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.
You create the test class like a regular JUnit test class.
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...
}
}
Upvotes: 2