Friday, May 29, 2009

J2ME Junit versus Sony Ericsson Mobile Junit

JUNIT for J2ME
The primary reason why we need to have a seperate junit framework for j2me is 
in conventional junit for j2se the test methods are either identifed using reflection (the test method should start with 'test')
or using annotations starting junit4.0 unfortunately both of these are not supported in CLDC.

The two flavours in which one can write junits for j2me applications

1. j2me unit from source forge -- http://j2meunit.sourceforge.net/doc.html

It is very  similar to junit framework.  Test methods are created in classes which extend from j2meunit.framework.TestCase 
instead of junit.framework.TestCase. Because reflection is not available developers always have to build the test suites 
that shall be run explicitly (as it is possible in JUnit too). This is done by implementing the method suite() 
(which is an instance method in J2MEUnit and not static as in JUnit!) and returning a new instance of the class TestSuite 
that contains all test methods from the test case that a developer wants to include in the test suite.

The best way to add test methods to a suite is with instances of j2meunit.framework.TestMethod. TestMethod is an interface 
with a single method run(TestCase) The argument to run() at test execeution time is the test case the TestMethod instance 
is associated with. So all that needs to be done in the implementation of the run method is to call the corresponding 
test method in the test case instance after casting it to the correct type. Here is an example from the method suite() 
in the class j2meunit.examples.TestOne that can be found in the source code distribution of J2MEUnit:

aSuite.addTest(new TestOne("testOne",
    new TestMethod()
    {
         public void run(TestCase tc)
        {
            ((TestOne) tc).testOne();
        }
    }));


Tests can be run through a class, called TestRunner as in JUnit. J2MEUnit comes with two implementations of TestRunner.
The first one is j2meunit.textui.TestRunner and is similar to the command line TestRunner from JUnit.
An enhanced TestRunner implemented as a MIDlet that can be run in the WTK emulator or on a real J2ME device 
like a mobile phone. Therefore it is recommended to use the implementation j2meunit.midletui.TestRunner to run tests.
Creating an instance of the test runner MIDlet is simple and straightforward, 
the only thing that needs to be done is to tell the test runner the tests to execute. 
This can be done by subclassing j2meunit.midletui.TestRunner, implementing the startApp() method 
(of the J2ME MIDlet base class), and calling the start() method of TestRunner with the test cases
and/or suites as parameters as in the ExampleTestMidlet class that is part of the J2MEUnit source distribution:

protected void startApp()
{
start(new String[] { "j2meunit.examples.TestAll" });
}


2. Sony erisson Mobile Junit.

Mobile JUnit can be downloaded from the Java Docs & Tools section of the Sony Ericsson Developer
World site. Mobile JUnit depends on the Sun Java Wireless Toolkit for CLDC (WTK) and can be used with
any development tool that incorporates or extends the WTK, such as the Sony Ericsson SDK for the Java
ME platform.

Here the test classes extend from com.sonyericsson.junit.framework.TestCase

To run the tests, open a console window and set the current directory to the Mobile JUnit folder. The runmobile-
junit batch file is used to compile and run any test cases it finds in the test folder of the specified
project. Use the following command to run the tests:
Developers guidelines | Unit testing with Sony Ericsson Mobile JUnit
16 September 2006
set projects=c:\SonyEricsson\JavaME_SDK_CLDC\PC_Emulation\WTK2\apps
run-mobile-junit --project-dir:%projects%\mobile-ju-sampleproject
--device:SonyEricsson_W800_Emu --compile-midlet:yes

This generates and compiles a MIDlet-based test runner, starts the SonyEricsson W800 emulator, and
runs the tests.Instead, Mobile JUnit uses generated helper classes to list and invoke individual
tests. A master list of test cases is also generated.
To run the tests, a test MIDlet is generated. The main class of the MIDlet is a Mobile JUnit test runner that
uses the list of test cases to run the tests.The test runner, the test cases, the helper classes, and the Mobile
JUnit framework itself are packaged together into a single JAR file. A JAD file is also generated. The
device emulator loads the generated MIDlet suite and starts the MIDlet to run the tests.

Mobile JUnit defines setUp and tearDown methods in its implementation of the TestCase class. These
methods can be used as expected to initialize and deinitialize test fixtures.
Tests that require a MIDlet main class can use the overloaded version of setUp to access the instance of
javax.microedition.midlet.MIDlet that is running the test case

No comments:

Post a Comment