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 (e.g., "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:
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...
}
}
Related
For example, I have a class of Junit tests which inlcudes 100 tests. is there a way to automatically run all these tests right after I build the project? Is there any setting that I need to toggle on and off or any annotations I need to add to my test class to trigger that?
My current setting is below: it doesn't automatically run the test upon build
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class someTest {
#Test
#Order(1)
public void Test44(){
}
etc...
}
In IntelliJ you usually do it the other way round: You run all tests, which will trigger a re-build if required, ie if there are code or config changes that should trigger a rebuild.
IntelliJ chooses the build actions depending on you project’s build tool, e.g. Maven or Gradle.
I am testing a Spring Boot application using code from a tutorial. The tutorial describes the setup and configuration of a Spring Boot application, and also describes a test that uses JAssert calls in the following manner:
package hello;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class SmokeTest {
#Autowired
private HomeController controller;
#Test
public void contexLoads() throws Exception {
assertThat(controller).isNotNull();
}
}
The test, unfortunately, will not compile in my IDE. The compilation is failing on the assertThat() method.
I am using Eclipse with Maven for my IDE. I have checked the Maven dependencies and see that the JAssert core library is included. Unfortunately, despite this the compiler can not seem to "find" the assertThat() call.
It fails to compile the test for that reason.
How do I get the test to utilize JAssert and find the calls to JAssert functions?
I'm not an eclipse expert, but it seems like you have some configuration issue here.
If you're sure that the dependency appears in pom.xml in scope test then try to eliminate eclipse related issues by running the tests directly via maven:
mvn test
will do the job
If it runs sucessfully, then re-create eclipse configurations from pom.xml / re-import the project.
If not, its a pom.xml related issue and has nothing to do with eclipse, you'll have to fix the pom or maven ecosystem. I suggest the following:
go to the local repository and remove the dependency from the file system (manually, the whole directory, with jar, pom.xml and everything) and then rerun mvn test
Sometimes dependencies are downloaded corrupted and despite being defined correctly in pom.xml they do not really contain classes in a form that java/maven can read
Actually:
It turns out that neither the Boot starter nor the IDE seems to find the necessary import declaration for the JAssert functions. Could it be because they are static?
In looking at some example code, I found in the code the import declaration for the assertThat() method. Usually, Eclipse would have suggested this declaration but it does not.
What is weird is that I put in the declaration by hand, and not only does this allow me to compile, but Eclipse, once the import is included, the code assistant makes proper assertThat() suggestions without problems!
There could be a bug somewhere, but that is beyond the scope of my particular problem. My test now compiles without problems.
I hope someone looking at this can figure out why Elipse's code assist does not work for this particular library properly. I wouldn't be surprised to discover that there are other objects/libraries in Spring Boot that don't get properly handled by the code assistant.
While generation unit test for java class Idea adds default static import
import static org.junit.Assert.*;
I want to extend this section by some more imports but I haven't managed to find a place where to configure them.
I'm using IntelliJ IDEA 2017.1 Ultimate
You are able to edit it through getting to:
Settings -> Editor -> File and Code Templates -> Junit4 Test Class
I want to generate automation test report in eclipse project. I didn't create a marven project and I used Eclipse, Cucumber and Selenium web driver for automation scripting. But I cannot find how to generate an automation report.
Please tell me if there is a way to generate an automation report for a Java eclipse project. I have created the main class. It is as belows.
package mCollector;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
format = {"pretty","html:target"},
features = {"src"}
)
public class mCollectorRunner {
}
But only from this, I could not generate automation script. What are the additional parts that I have been missed.
Run your class as JUnit, then you should have report.html in target folder after test, you can change directory of report output by changing line
...
format = {"pretty","html:target"},
...
to
...
format = {"pretty","html:test-reports"},
...
It will create folder called 'test-reports' in your project directory.
If you are using junit, the class should have a Test annotation. And when you are running it, choose run as junit instead of run as java application.
After that, use ant to generate report is a easy way.
When I use Eclipse to create a JUnit test suite, it does not detect any existing tests, and warns "No test classes selected."
I started from the test class package (test/com/.../package), and the package is selected. There are several JUnit tests there, also created through the same version of Eclipse, but there is no way to select them.
I am using JUnit 4.
Thanks!
The wizard for creating a suite currently only works with JUnit 3 tests, see corresponding bugzilla entry.
A Suite that works for me is :
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
...
#RunWith(Suite.class)
#SuiteClasses( { MyTest.class })
public class SeleniumSuite {
...
}
This helps if you want to run just a subset of tests defined in a package. What are you tests called? Try re-factoring them so they are called either Test*.java or *Test.java.
In Eclipse, you can simply right-click on the project / package you want to run tests in and select Run as > Junit Test - you can avoid needing to programmatically create a test suite class completely.