You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To create unit tests for an Eclipse plug-in a Fragment Project is used. When creating a Fragment Project we assign the plug-in we wish to test as a Host Plug-in.
3
+
To create unit tests for an Eclipse plug-in, a Fragment Project is used.
4
+
When creating a Fragment Project, we assign the plug-in we wish to test as a Host Plug-in.
4
5
Eclipse automatically gives the Fragment access to the classes in the original plug-in.
5
-
In the Fragment Project we create classes to test the classes in the original plug-in.
6
+
In the Fragment Project we implement test classes to test the original plug-in's implementation.
6
7
7
8
## A simple example
8
9
9
-
Open the wizard for creating a standard plug-in in Eclipse (File->New->Plug-in Project) and complete the following steps (if values not specified then use the defaults):
10
+
Open the wizard for creating a standard plug-in in Eclipse (File->New->Plug-in Project) and complete the following
11
+
steps (if values not specified then use the defaults):
10
12
11
-
* Set "Project name" to org.myexample.plugin
13
+
* Set "Project name" to `org.myexample.plugin`
12
14
* Click "Next"
13
15
* Uncheck "Generate an activator, a Java class that controls the plug-in's life cycle"
14
16
* Uncheck "This plug-in will make contributions to the UI"
15
17
* Put "No" for "Would you like to create a rich client application?"
16
18
* Click "Finish"
17
19
18
-
The will create the plug-in. Inside the src folder create a package called org.myexample.plugin.classes and add a class called StringManipulator.
19
-
Add the following code to the class:
20
+
This will create the plug-in. Inside the src folder create a package called `org.myexample.plugin.classes` and add a
21
+
class called StringManipulator. Add the following code to the class:
20
22
21
-
```
22
-
package org.myexample.plugin.classes;
23
-
public class StringManipulator {
24
-
25
-
public String addStrings(String one, String two) {
26
-
return one;
27
-
}
23
+
```java
24
+
packageorg.myexample.plugin.classes;
25
+
publicclassStringManipulator {
26
+
27
+
publicStringaddStrings(Stringone, Stringtwo) {
28
+
return one;
28
29
}
30
+
}
29
31
```
30
32
31
-
Now to create the Fragment Project. Open the Fragment Project wizard under File->New->Other->Plug-in Development->Fragment Project and complete the following steps:
33
+
Now create the Fragment Project. Open the Fragment Project wizard under
34
+
File->New->Other->Plug-in Development->Fragment Project, and complete the following steps:
32
35
33
-
* Set "Project name" to org.myexample.plugin.tests (i.e the original plug-in name plus ".tests" - this is our naming convention)
36
+
* Set "Project name" to `org.myexample.plugin.tests` (i.e. the original plug-in name plus `.tests` - this is our naming
37
+
convention)
34
38
* Click "Next"
35
39
* Under "Host Plug-in" click the "Browse" button and select the original plug-in
36
40
* Click "Finish"
37
41
38
-
Eclipse will now create the Fragment Project. We need to manually add the JUnit plug-in as a dependency for the Fragment Project, to do this:
42
+
Eclipse will now create the Fragment Project. We need to manually add the JUnit plug-in as a dependency for the
43
+
Fragment Project, to do this:
39
44
40
-
* Open the MANIFEST.MF file
45
+
* Open the `MANIFEST.MF` file
41
46
* Select the "Dependencies" tab
42
47
* Under "Required Plug-ins" click the "Add" button
43
-
* In the dialog, type org.junit and select the plug-in listed (it should be version 4+)
48
+
* In the dialog, type `org.junit` and select the plug-in listed (it should be version 4+)
44
49
* Click "OK"
45
50
* Save the changes
46
51
47
-
In the src directory of the Fragment Project create a package called "org.myexample.plugin.tests".
48
-
Add a class called "StringManipulatorTest" - the name **MUST** end in Test for the build system to recognise it.
52
+
In the src directory of the Fragment Project create a package called `org.myexample.plugin.tests`.
53
+
Add a class called `StringManipulatorTest` - the name **MUST** end in Test for the build system to recognise it.
54
+
55
+
Now create a test, add the following code to the `StringManipulatorTest` class:
49
56
50
-
Okay let's create a test, add the following code to the StringManipulatorTest class:
It is useful to see what parts of a plug-in's code are used or not used by the unit tests.
207
-
If a piece of code is not used by the unit tests then that may mean that an extra test is required
208
-
209
-
Unit test code coverage can be examined inside Eclipse using EclEmma which can be installed via the Eclipse Marketplace (under the "Help" menu).
210
-
Once EclEmma is installed the coverage of the unit tests can be examined. Right-click on the test project and select Coverage As->JUnit Test. This will run the tests and calculate the coverage, the results should look something like this:
186
+
If a piece of code is not used by the unit tests, then that may mean that an extra test is required.
211
187
188
+
To examine code coverage, right-click on the test project and select Coverage As->JUnit Test.
189
+
This will run the tests and calculate the coverage, the results should look something like this:
212
190
213
191

214
192
215
193
From the results it can be seen that 63.2% of the StringManipulator code is used by the unit tests.
216
-
The code that isn't used is highlighted in red - for this example we can see that we need to write a test that tests the reverseString method.
194
+
The code that isn't used is highlighted in red - for this example we can see that we need to write a test that tests
195
+
the `reverseString` method.
217
196
197
+
:::{tip}
198
+
Code coverage is provided by the `EclEmma` add-on, which is installed by default in the Eclipse for RCP developers
199
+
build of the IDE. If it is not already installed, it can be installed from the Eclipse marketplace.
200
+
:::
218
201
219
202
## Troubleshooting
220
203
221
-
### ClassNotFoundException
204
+
### `ClassNotFoundException`
222
205
223
206
Running the tests in Eclipse might crash with an error like:
224
207
225
208
```
226
-
Class not found org.myexample.plugin.tests.StringManipulatorTest
at java.net.URLClassLoader.findClass(Unknown Source)
212
+
at java.lang.ClassLoader.loadClass(Unknown Source)
213
+
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
214
+
at java.lang.ClassLoader.loadClass(Unknown Source)
215
+
...
233
216
```
234
217
235
218
This is a known bug and there is a workaround:
@@ -240,16 +223,20 @@ This is a known bug and there is a workaround:
240
223
* Select "Add Folders" and click "OK"
241
224
* In the new dialog, expand the test plug-in and select the "bin" folder and click "OK"
242
225
* On the original dialog, click "Apply" and then "Run"
243
-
*Hopefully, the tests will now work and you should be able to re-run them in the normal way
226
+
*The tests should now work, and you should be able to re-run them in the normal way
244
227
245
228
### Eclipse is not picking up new tests
246
229
247
-
If Eclipse is not picking up changes when you add tests you may need to change the default output folder for tests for Maven to pick it up.
230
+
If Eclipse is not picking up changes when you add tests, you may need to change the default output folder for tests for
231
+
Maven to pick it up.
248
232
249
233
* Right-click on the tests plug-in, go to properties, Java build path
250
-
* Change the output folder to target/test-classes (you may need to create this folder first by clicking browse, selecting target and adding the test-classes folder)
251
-
* If this does not work try deleting the target/test-classes folder first, if it existed already, and do a clean rebuild of the workspace
234
+
* Change the output folder to target/test-classes (you may need to create this folder first by clicking browse,
235
+
selecting target and adding the test-classes folder)
236
+
* If this does not work try deleting the target/test-classes folder first, if it existed already, and do a clean
237
+
rebuild of the workspace
252
238
253
-
### IncompatibleClassChangeError
239
+
### `IncompatibleClassChangeError`
254
240
255
-
If the tests are failing because of an IncompatibleClassChangeError error then the solution is to delete the bin and target folders for both the main plug-in and the corresponding test plug-in
241
+
If the tests are failing because of an IncompatibleClassChangeError error then the solution is to delete the bin and
242
+
target folders for both the main plug-in and the corresponding test plug-in.
0 commit comments