Let’s say you are developing a bunch of Eclipse plugins, and you want to write some unit tests. I will show you how to develop them in this snippet. This is valid for Eclipse 3.2 and above.
1. As much as possible, try to write the unit tests in a separate plugin.
Let us assume that you have a plugin called com.xyzco.network that has some classes that you want to unit test. For writing the JUnit tests, you can write a separate plugin com.xyzco.network.tests. You might ask, why don’t I create a separate folder “tests” under my plugin, add it to source path and have my test cases under that directory?. That is perfectly valid, and is also a matter of taste. But I find that if you do it this way, it is more easier to ignore the *.tests plugins in the product feature when packaging for the release.
2. Create the test plugin
Create a new plugin project as you would do for any other plugins. Add dependencies to the plugins that you will refer to. In our example, definitely add com.xyzco.network. Write your junit test cases.
3. Run the unit tests
Select Run => Run As => JUnit Plug-in Test
The test case should run, however, a new eclipse IDE will also start. In most cases, you might not need the UI to appear. To get rid of the IDE, view the run configuration. Under the “Main” tab, the “Run a product” radio button would be checked. Instead, check the “Run an application” and select “[No Application] – Headless Mode”. Now it should run without the IDE. Happy junit plugin programming!