Advertisment

Unit Testing Mobile Applications

author-image
PCQ Bureau
New Update

In the last issue we talked about the crucial differences in Java ME and Java

SE and how to use JMUnit for unit testing your mobile applications



in the absence of Reflection classes in the API.

Advertisment

J2MEUnit is another JUnit based framework for unit testing mobile

applications. In this part, we will see how to use it and how it is different



from JMUnit.

Getting started



J2MEUnit has only one JAR file that should be included in the 'classpath' or
added to your IDE's environment. Test cases in J2MEUnit must inherit from

j2meunit.framework.

Applies To: Java ME Developers



USP: Learn differences between JMUnit and J2MEUnit.


Primary Link: http://j2meunit.sourceforge.net




Google Keywords: J2MEUnit

Advertisment

TestMethod. Like in JMUnit, your tests are implemented in test methods and

conventionally named test+method NameToBeTested. Unlike JMUnit, test methods are

not required to throw any exception. Importantly, J2MEUnit does not support as

full or rich set of assert methods as does JMUnit. J2MEUnit only supports the

following assertions:

assertTrue(expression)



assertSame(expected,actual)


assertEquals(expected,actual)


assertNotEquals(expected,actual)


assertNull(object)


Except for the lack of assertion methods, J2MEUnit test case methods do not

vary greatly from JMUnit test case methods.

Advertisment

Sample midlet



Consider a simple Midlet that provides addition, subtraction, multiplication
tasks. The source code for this would be as follows.

import javax.microedition.midlet.*;



import javax.microedition.lcdui.*;


public class PCQMidlet extends MIDlet implements CommandListener {


public PCQMidlet(){


}


public void initialize(){


getDisplay.setCurrent(getPCQForm());


}


public void getPCQForm(){


.


.


}


public int PCQAdd(int num1, int num2){


return (num1+num2);


}


public int PCQSub(int num1, int num2){


return (num1-num2);


}


public int PCQMul(int num1, int num2){


return (num1*num2);


}


















We'll take you through how to create test cases asn suites and how can we

unit test the add, subtract and multiply methods of this Midlet using J2MEUnit.

Advertisment

J2MEUnit TestSuite



The creation and execution of suites in J2MEUnit varies quite a bit from JMUnit.
J2MEUnit suites are more reminiscent of regular JUnit suites (except for the

lack of reflection).

In order to create a test suite in J2MEUnit, you typically create a suite()

method in your test case class. This aligns closely with JUnit, although in

J2MEUnit, the suite method can be an instance method.

In regular JUnit, the suite method is a static method. The suite method

returns an instance of j2meunit.framework.TestSuite.

Advertisment

This object contains all the test methods for all the test cases that are

desired. Adding a test method for a test case to the suite can seem a little

convoluted. The best way to add a test method to the suite is to create and use

an instance of 2meunit.framework.

TestMethod.



TestMethod is actually an interface that stipulates a single run (TestCase)
method be provided by implementing classes. TestMethods are generally

implemented with an anonymous inner class. Here is how to add a TestMethod to

the suite for a simple add method as:

public Test suite() {



TestSuite suite=new TestSuite();


suite.addTest(new PCQAddTest("test pcq add",


new TestMethod(){


public void run(TestCase tc){


((PCQAddTest) tc).add();


}


}


));


return suite;


}








Advertisment

Running the tests



There are two ways to go about running the tests over here, as there is a
TestRunner class with two different implementations in two different



packages.

The first implementation is j2meunit.textui.TestRunner from version 1. Using

this class you can run the tests only from Java ME command line



environment.

The other one viz. j2meunit.midletui.TestRunner from version 1.1 is an

enhanced version that can be run using Sun's WTK (Wireless Toolkit) device

emulator or a mobile device. This class is implemented as a Midlet.

Advertisment

Using the newer TestRunner is simple; all you need to do is to tell the

TestRunner the test to be executed. You can override the startApp() method and

place call to the TestRunner's start() method passing string arguments

containing the names of the tests you have created.

protected void startApp(){

start(new String(“PCQAddTest”, “PCQSubTest”);



}

The alternate way to same is using the application archive's manifest file

( JAD file). The TestRunner looks for the Midlet property J2MEUnitTestClasses

when started as Midlet. So you can list the test method name values for this

property.

In conclusion



We looked into two different frameworks for unit testing Java Me applications.

Both of them have their own set of advantages for example JMUnit has more

assertions than J2MEUnit, whereas J2MEUnit follows a procedure very similar to

JUnit though without reflections.

The merger of both these open source projects might bring a best of both

worlds package for testing. So that developers have a one-stop solution



for their testing needs.

Advertisment