During unit testing, jmock can easily build a mock object, greatly improving the time for writing test cases. This article describes how to use jmock for unit testing in RCP development.
1. Import the jmock package
In eclipse RCP development, the dependencies between packages are implemented through osgi, so it is more complicated. It's like JUnit is made into a plug-in eclipse, so I also use the tried-and-tested "plug-in from existing jar archives ".
- Download all jar packages (http://www.jmock.org/download.html) from jmock's website ).
- Go to eclipse and choose new project> plug-in development> plug-in from existing jar archives. then select "add external... ", select all jar files, and click Next. Enter a project name or something like this (I usually do not select "unzip the jar archives into project", so there are too many files after unzip .) Then, "Finish" is generated for the new jmock plugin.
2. Use jmock to write the test
Now we can use the generated jmock plugin to write the test.
First, we will build a new plugin project, in which we will write all the test cases. I believe that all RCP students know this. (In some of my practices, this test project is best performed in fragment form, which not only effectively isolates the test code, but also facilitates the use of the tested code for the test code, ).
After the project is created, we first need to modify its manifest. MF file, add the relevant plugin. Mainly add two plugins, one is JUnit, and the other is the one we just generated, such:
The plugin named XX. testutil in the figure contains jmock. Please note that
, The plugin of junit4 must be placed at the end of the list, at least back than jmock. Otherwise, there will be errors during the running.
Now, please write a test to try it.