Saturday, March 7, 2009

J2ME & Unit Testing

Hi Friends,

As you came to know from the blog title that today we are going to discuss about unit testing approach for J2ME. Before moving further, let's first understand unit testing. Most of the Mobile Application developer think that unit testing is same as the functionality testing which is not true. Functionality testing comes in picture when application installed on mobile and then we test the proper functionality of the application as per the product specification; where as unit testing comes in picture at your code level. In simple words, Unit Testing is an approach that provides confidence to the developer that given piece of code written by him/her are fit for use.

To support unit testing in JAVA we have JUnit Framework, we cannot use same framework for J2ME because JUnit rely on Java Reflection API whereas reflection API is not part of J2ME environment. So there is refine version of JUnit for mobile application unit testing. Actually there are two:
a) J2MEUnit
b) JMUnit
Both are Open Source and used for unit testing approach, but JMUnit has better approach for same. JMUnit has rich set of assert methods as compared to J2MEUnit. In future there are planning to merge these two frameworks into one & emerging framework will have the features of both.

My Experience with JMUnit:-
I used JMUnit in our project and trying to share my experience with all of you. You can use ANT task for running your unit tests. The basic approach for unit testing is that write test cases for those classes which internally call other class's methods, so that you need not to write test cases twice. For example, You can write test cases for one class that manage all the UI related functionality, second that manage RMS related functionality, third that manages data objects, fourth that manage HTTP/HTTPS connections, and the last one is your request and response objects. Suppose you have some abc() method in any class then you should write your testclass with method name testAbc() and test your code scenario as per the requirement. The basic architecture of test class is as follows:

First you have two import JMUnit APIs according to the CLDC
i) jmunit.framework.cldc10.*; //or
ii) jmunit.framework.cldc11.*;

Now basic skelton of a class--

public class Xyz extends Testcase {
public Xyz() {
super(int totalTests, String xyzTest);
public void setUp() {
// define any object or any initialization as per your requirement.
public tearDown() {
// release memory.
public void testAbc() {
// write your test case
public void test(int testNumber) throws Throwable {
switch(testNumber) {
case 0:
When you write all the test cases for your defined classes then you can combine them to in a TestSuite class so that each test will run using your ant script. The architecture for TestSuite class is as follows:

public class AbcTestSuite extends TestSuite {
public AbcTestSuite() {
super("Test Suite");
add(new Xyz);
add(new .......);
Now in the ant script you have to map the JMUnit jar files and you can also specify your test classes, source and reports directory path so that each will move to there respective folder and at the end html based reports will get generated.

1) I am not able to test the RMS value.
2) Also not able to test the UI Classes screen transition.
As per their documentation, I am able to run emulator from ant script but still not able to test both of them. If someone earlier also faced this problem and got any solution.... Please let me know.

I tried to share all the facts related to mobile application testing using JMUnit. If I forgot something please update me with your comments.