JUnit Testing for Eclipse RCP. How to do it?
Asked Answered
E

2

6

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?

Elamite answered 3/2, 2014 at 8:37 Comment(0)
C
7

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.

Contrarily answered 3/2, 2014 at 8:43 Comment(0)
S
4

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

Suzisuzie answered 3/2, 2014 at 9:21 Comment(1)
Thank you for proposing a way to test the running application!Elamite

© 2022 - 2024 — McMap. All rights reserved.