Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit Testing for Eclipse RCP. How to do it?

I would like to write JUnit test for my Eclipse RCP while I continue developing the code. When starting the application the different plugins initialize variables of various plugins/classes (mostly within their start methods) which are needed for the correct functionality. If this initialization doesn't happen, it is impossible to test code because it depends on those values.

How do I solve this issue without creating a lot of dummy values? What is the general approach to testing Eclipse RCPs?

like image 436
Markus Avatar asked Feb 03 '14 08:02

Markus


2 Answers

You're facing a common problem: Too many dependencies. You need to cut them.

With Eclipse 3, this is going to be somewhat hard. Try to split the code into things that depend on the Eclipse platform running and everything else. Eclipse often uses interfaces, so you can test many things using mocks.

With e4, things got more simple since many services will be injected, making mocking and testing even easier.

But the goal must always be to have as much code as possible that doesn't depend on SWT or the platform. Create your own interfaces if you have to. The runtime imlementations just wrap Eclipse code. For tests, you can use mocks to simulate the Eclipse runtime.

like image 92
Aaron Digulla Avatar answered Sep 22 '22 01:09

Aaron Digulla


You can run tests using JUnit plugintest, that will start up the plugin framework and will allow for testing of plugins. But this usually only solves some of the issues. The best suggestion is as Aaron suggests to separate functionality as much as possible to the point where all your actual code are just plain old java objects that you can test normally. All dependencies to Eclipse are in different classes and are kept as thin as possible so that they dont require testing.

This can be difficult to achieve, so mocking may be required. Another trick I've resorted to at times is to use Java reflection to change values of private fields, see this question

like image 35
Fredrik Avatar answered Sep 22 '22 01:09

Fredrik