With the recent ME Framework open sourcing, and with all those discussions on what ME Framework can do for application developers (as opposed to TCK writers), I took a look at the APIs for popular JUnit-like unit testing frameworks for Java ME, to see what we can do better in our ME Framework.
Please note that this comparison is focused mostly on the API.
Popular Unit testing framework for Java ME:
- J2MEUnit
- JMUnit
- Sony Ericsson Mobile JUnit 1.0
All three frameworks are close to the original JUnit, with adjustments needed to support the Java ME Platform. In all 3 cases, a test class should extend a base class from the appropriate framework. And all typical JUnit helper methods are provided to the subclasses, e.g.: assertTrue, assertFalse, assertEquals, etc. (J2MEUnit provides the bare minimum of such methods, leaving quite some of them out).
JUnit standard hook methods are also present: setUp() and tearDown(), to setup test environment and to tear it down after the test is done.
So, in all 3 cases, developers who are familiar with JUnit, can start writing new unit tests for Java ME with fewer problems, since they basically know the core API.
The differences can be seen in areas where support for Java ME was added (one of the major problems is the lack of reflection in Java ME, while the original JUnit relies heavily on it). While the tests themselves look pretty similar for all three frameworks, the way the test suites are constructed differs.
With absence of reflection, J2MEUnit authors decided that test suites should be created manually, explicitly adding new tests to the test suite, like this:
aSuite.addTest(
new TestOne("testOne", new TestMethod() {
public void run(TestCase tc) {
((TestOne) tc).testOne();
}
})
);
The name of the test provided in the constructor, and the functionality of the test is enclosed in the anonymous inner class that implements the TestMethod.
Pretty heavyweight and not that simple, if you ask me.
JMUnit test class must call a two-parameters constructor of the base class, specifying the number of the “sub”-tests, and the name of the test.
public TemperatureConversionTest() {
super(4,"TemperatureConversionTest");
}
The test selection and execution is done via method test(int), that should be implemented by every test:
public void test(int testNumber) throws Throwable {
switch(testNumber) {
case 0:testfahrenheitToCelsius();break;
case 1:testcelsiusToFahrenheit();break;
case 2:testisHotter();break;
case 3:testisCooler();break;
default: break;
}
}
The base class will call this method with all int values, from 0 to number-of-tests.
In my opinion, this approach is less convoluted and more straightforward then J2MEUnit. JMUnit also provides better GUI on a device that shows the results.
JMUnit provides two versions, one is suitable for CLDC 1.0, and the another one is for CLDC 1.1.
Overall, JMUnit provides more extensive set of helper methods (asserts), with better GUI, and with more straightforward configuration.
Netbeans’ Mobility Pack has switched from J2MEUnit to JMUnit recently.
The folks from Sony Ericsson did an excellent job on this, providing good extension to JUnit for Java ME, and with great documentation 
Sony Ericsson approach, it seems, was to provide experience as close to original JUnit as possible. In most cases, to port JUnit tests to Sony Ericsson unit tests is just a matter of replacing the base test class all the tests extend (from JUnit one to Sony Ericsson one.)
Couple of innovative things were done in this library. The absence of reflection is handled on Java SE side, where the tests are being analyzed and proper extra classes are created, so that developers are freed from manual work of specifying all their test names. Sony Ericsson’s version is clearly better here then the two alternatives (J2MEUnit and JMUnit).
The library also provides nice additions on top of typical JUnit API. For example, it not only provides setUp() method, but also setUp(MIDlet midlet), for those tests that would like to access the midlet that runs the tests.
The GUI on the device looks nice too, with tabbed interface for passed/failed tests. Sample project for WTK is also provided. And the basic code coverage can be calculated, which is an unexpected plus.
Integration with Ant, NetBeans and Eclipse is provided.
Overall, Sony Ericsson Mobile JUnit 1.0 is very well done and easily beats other alternatives, discussed above. They provide the best JUnit “emulation”, and resolve absence of reflection in innovative way simplifying developers life.
For additional information, take a look at the following article: “JUnit Testing Using Java ME JUnit Frameworks“.