Writing and running JMUnit tests
In this section, you will be using the JMUnit testing
framework to write and run tests. To get started with JMUnit you can refer to
JMUnit user guide.
JMUnit testing framework has two main concepts, test case and test
suite. A test case is a class where tests targeting another class are written.
Test suite is a class that aggregates lots of test cases, so if you wish to
execute several test cases at once you must create a test suite to wrap them
Creating a Test Case
- In the Package Explorer view, right click the class you want to test
- On the New JMUnit Test Case wizard, select the
appropriate information for the Test Case to be generated.
- You will see a warning message asking you to add the JMUnit library to
the build path. Use the Click here link to add the JMUnit
- After having added JMUnit on the class path in case it was not already
configured, the Next and Finish buttons will
Note: You now can either click Finish to create the
Test Case, or click Next to set up the methods to be tested on
the Test Methods page.
- The Test Methods page is used to choose all methods
to be tested in the new Test Case that will be created.
- Select all methods that should be tested in your Test Case.
To finish the JMUnit Test Case creation wizard, simply click on the Finish
button and the new test case will be created in your workspace.
If you have completed the above steps, the Package
Explorer view will now contain the JMUnit Test Case we just created, and the it
will be opened by the java editor.
Creating a Test Suite
- In the Package Explorer view, right click the
- On the New JMUnit Test Suite wizard, select the
appropriate information for the Test Suite to be generated.
- Select the classes that should be included in the suite. We currently
have a single test class only, but you can add to the suite later.
You can add or remove test classes from the test suite in two ways:
- Manually by editing the test suite file
- By selecting a test suite in the source tree and selecting "Update
JMUnit Test Suite..."
Note: the wizard adds 2 markers,
A new TestSuite Class is created and opened on the java editor.
into the created Test suite class, which allows the wizard to update existing
test suite classes. Editing code between the markers is not recommended.
- Running either a test case or a test suite is done by running the
class as an "Emulated Java ME MIDlet" as show bellow.
- The Emulator shall initialize into the screen below:
- Select "Test" to run the tests.
- The test results are shown bellow.
- Debugging tests is done as debugging a regular Java class since both
TestCase and TestSuite extend MIDlet class.