Testing Java ME Code in the Mobility Pack with JMUnit
Unit testing improves code quality by locally testing the individual components of your application
and enabling you to identify and fix bugs quickly. Fixing bugs early in the development process is much
easier and far less costly than finding and debugging them later in the development cycle.
JUnit is a popular testing tool for Java Standard Edition
(Java SE) and Enterprise Edition (Java EE) that provides a framework for creating automated individual tests
and test suites. However JUnit depends on the reflective API to work and this feature is not supported in Java ME.
For Java ME developers, there are two JUnit type frameworks to use for testing, JMUnit
and J2MEUnit. The NetBeans Mobility Pack 5.5 for CLDC
now includes the JMUnit test framework for testing Java Micro Edition (Java ME) code. This tutorial provides
a simple demonstration of JMUnit testing support now available in NetBeans Mobility Pack for CLDC.
Requirements
Before you can begin you need to install the following software on your computer:
- Java Standard Edition (Java SE) Development Kit (JDK) version 5 (download) or higher.
- NetBeans IDE 5.5 (download)
- NetBeans Mobility Pack 5.5 (download)
If you are new to the NetBeans Mobility Pack, you should go through the NetBeans Mobility Pack 5.5 for
CLDC Quick Start Guide before continuing.
Application Overview
This example uses a calculator application to demonstrate how to write unit tests in NetBeans Mobility Pack to target specific features of a project. You can dowload the Calculator project we created for this example and try it yourself. After downloading the project, the files must be unzipped and placed in your IDE's project home directory. After that we're ready to go.
Installing and Running the Calculator Application
- Choose File > Open Project (Ctrl-Shift-O). Navigate to the folder where the unzipped and installed Calculator project folder is located.
- Expand the Calculator project to view its files. The project contains a CalculatorMIDlet MIDlet that provides the UI for the program and a math.Arithmetic.java file that provides the operational logic for the calculator program.
- Click to expand the Calculator project files. Expand the
Math node in the Projects window and the Arithmetic.java appears.
- Now let's run the application just to be sure it works as expected. From the
Main Menu, choose Run > Run Main Project.
In the emulator, use the pointer key to highlight Calculator, then click the button under Launch to load the application. The Calculator loads in the emulator in it should look like this.

Adding CLDC 1.1 Test Libraries
The IDE generates JMUnit CLDC 1.0 test cases by default for J2ME projects. For this example we want to remove the CLDC 1.0 libraries and add the JMUnit CLDC 1.1 libraries to the project using the Properties Window of the IDE. The CLDC 1.1 library includes added support for the assertEquals() and assertNotEquals() methods to support Java floating-point primitives that are necessary for testing the Calculator application.
- Right-click the CalculatorMIDlet project and choose Properties.
- In the Categories window of the Project Properties dialog box, select Build > Libraries & Resources.
- Choose the JMUnit libraries for CLDC 10 library then click the Remove button.
- Click the Add Library button.

- In the Add Libraries dialog box, select JMUnit for CLDC 11, then click Add Library.
- Click OK in the Properties Window to finish.
Creating and Configuring Unit JMUnit Tests
- Right-click the
Arithmetic.java node in the Projects window
and choose Tools > Create JUnit Tests.
The Create Tests dialog box appears displaying options for generating
JMUnit tests.
- For this step leave
all the default options selected and click the OK button.
- An AritmeticTest.java file appears in the Projects window as under the
Math node and the source of the file opens in the Source Editor. This file contains skeleton test methods for each of the methods in the Arithmetic.java file. Each test is set to fail by default so you must write testing logic for each test listed in order for it to actually produce a meaningful result.
Implementing the Test Methods
Now that we have test skeletons in place, we need to change the values from "null" to the expected results, or test assertions.
- In the testgetInstance method, make the following changes in bold:
public void testgetInstance() throws AssertionFailedException {
System.out.println("getInstance");
math.Arithmetic instance = Arithmetic.getInstance();
math.Arithmetic expectedResult = Arithmetic.getInstance();
math.Arithmetic result = instance.getInstance();
assertEquals(expectedResult, result);
}
- Remove the following line:
fail("The test case is a prototype.");
- Change the testadd method to look like the following:
public void testadd() throws AssertionFailedException {
System.out.println("add");
math.Arithmetic instance = Arithmetic.getInstance();
for (int a=0;a<=10;a++)
for (int b=0;b<=10;b++) {
int expectedResult = a+b;
int result = instance.add(a,b);
assertEquals(expectedResult, result);
}
}
What we have done is added logic that asserts an expected result. Any result other than what we asserted results in a Fail message when you run the test. Thus it is important to know what result you want and expect when creating the asserted values.
- Change the other test methods to implement the desired test logic. To see the implementation of all the test methods we used, look at the completed test file. Notice that one of the tests in it has been written to fail.
/**
* Test of divide method, of class math.Arithmetic.
*/
public void testdivide() throws AssertionFailedException {
System.out.println("divide");
math.Arithmetic instance = Arithmetic.getInstance();
for (int a=1;a<=10;a++)
for (int b=1;b<=10;b++) {
double expectedResult = a/b;
/* In order to make this not fail we must cast each
* variable before the divide operation
*
* double expectedResult = (double) a/(double) b;
*/
double result = instance.divide(a,b);
assertEquals(expectedResult, result);
}
}
Running the JMUnit Test
- Choose Run > Run Main Project.
This time, the emulator displays two programs you can run: Calculator and
TestSuite.
- Select the TestSuite program and Run it. The emulator shows you the following screen. No tests have been run at this point.
- Run the tests using the right button on the emulator located under the Test command. The emulator returns results listing the units that passed and failed, the number of errors returned, and the time (in milliseconds) it took to run the process.
Interpreting Test Results
The phone emulator screen and IDE Output window both give information about what happened when tests are performed. Here's an overview of how to read the results.
- A Pass message in the emulator indicates that the assertion defined in the test code performed as expected.
- A Fail message in the emulator indicates that the assertion failed to match the result defined in the test code. The Output window shows more information along with the result it expected from the test. Here's an example of an assertion method failing and the error message it displays in the IDE's Output window.

- The Error message in the emulator indicates the test framework code itself was the problem. In this case you need to look at the IDE's Output window to see where the problem occurred. For example, the test assertion is malformed or written for something not included in the code being tested. Here's an example of this type of error message.


Next Steps and related articles: