Using J2meunit to test J2ME programs

Source: Internet
Author: User

Very useful test tools, can not be wasted, hehe

JUnit is an excellent framework for unit testing, which is widely used in the J2SE,J2EE development process, which makes the quality of code better monitored and maintained. However, for the J2ME platform everything is not so simple, because the handheld device requires more consideration of performance issues, the J2ME platform does not provide a reflection (Reflectiong) API, so many of the features of JUnit can not be implemented on the J2ME, Programmers will not be able to use JUnit for unit testing on the J2ME platform. This article will introduce a tool for unit testing of J2ME applications: J2meunit. J2meunit is a unit testing framework for the J2ME platform based on JUnit implementations. For more information, please see: http://j2meunit.sourceforge.net/.

Operating Environment
The sample programs in this article are run and tested under eclipse3.1 and need to download the latest version of J2meunit at the following URL:
http://j2meunit.sourceforge.net/
After downloading, if for Rar/zip compression package, must first decompression.

HelloWorld instance
This article will use a HelloWorld example to introduce the basic methods of J2meunit. First, create a new J2ME Midlet Suite project in Eclipse. File->new->project->j2me Midlet Suite, named Hellomidlet.
The project includes two categories: Helloworld.java and Helloworldmidlet.java. Start with a new package name under Project Hello, then create a new class under the Hello Package: Helloworld.java, and a new J2ME Midlet:HelloWorldMidlet.java. The code is as follows:

Hello. Helloworld.java

The following is a reference fragment:
Package Hello;
public class HelloWorld {
Public HelloWorld () {
}

Public String SayHello () {
Return to "Hello World";
}
}

Hello. Helloworldmidlet.java

The following is a reference fragment:
Package Hello;
Import Javax.microedition.lcdui.Alert;
Import Javax.microedition.lcdui.AlertType;
Import Javax.microedition.lcdui.Display;
Import Javax.microedition.midlet.MIDlet;
Import javax.microedition.midlet.MIDletStateChangeException;
public class Helloworldmidlet extends MIDlet {
private display display;
Private alert alert;
Private HelloWorld Hello;

Public Helloworldmidlet () {
display = Display.getdisplay (this);
Hello = new HelloWorld ();
}
protected void startApp () throws Midletstatechangeexception {
String s = Hello.sayhello ();
Alert = new Alert ("Hello", s,null,alerttype.info);
Alert.settimeout (Alert.forever);
Display.setcurrent (alert);
}
protected void Pauseapp () {
}
protected void Destroyapp (Boolean arg0) throws Midletstatechangeexception {
}
}


The function of the program is to output the string of "HelloWorld" in an alert, the code is very simple, do not do too much discussion here. After compiling, you can run the program, and if you can see "HelloWorld" in alert after running, the program is working properly. Then start writing the test code.

Writing test Classes
When writing a test file, you need to use the J2meunit class library, first you need to add the class library to the project build path. Right-click the project name->properties->java build path->libraries, Add External JARs, select the file you just downloaded, and then OK.
You now need to test the SayHello () method in Helloworld.java. Create a new package name test under the engineering directory, and then create a new class named Testhelloworld.java, which tests primarily the SayHello () method in HelloWorld. Although J2meunit now does not support the reflection mechanism, it is still named after the JUnit specification for future code porting. The Testhelloworld code looks like this:

Testhelloworld.java

The following is a reference fragment:
Package test;

Import J2meunit.framework.Test;
Import J2meunit.framework.TestCase;
Import J2meunit.framework.TestMethod;
Import J2meunit.framework.TestSuite;
Import Hello. HelloWorld;
public class Testhelloworld extends TestCase {
Private HelloWorld Hello;

Public Testhelloworld () {}

/**
* Constructor
* @param the name of the Stestname test method
* Methods of @param rtestmethod test
*/
Public Testhelloworld (String Stestname,testmethod rtestmethod) {
Super (Stestname,rtestmethod);
}
protected void SetUp () throws Exception {
Super.setup ();
Hello = new HelloWorld ();
}

protected void teardown () throws Exception {
Super.teardown ();
}
Public Test Suite () {
TestSuite asuite = new TestSuite ();

Asuite.addtest (New Testhelloworld ("Testsayhello", new TestMethod () {
public void Run (TestCase tc) {
((Testhelloworld) TC). Testsayhello ();
}
}));

return asuite;
}

/**
* Test SayHello () method
*
*/
public void Testsayhello () {
String s = Hello.sayhello ();
Assertequals ("Hello world!", s);
}
}


First, the test class must inherit from J2meunit.framework.TestCase. Only the HelloWorld class is tested from, and therefore contains a HelloWorld object. Then, two constructors are overloaded. Similarly, two methods of TestCase are rewritten: setUp () and teardown (). Hello was initialized in Setup (). Since J2meunit does not support reflection, we must write our own suite (), create a Testsuite object, and then add the test methods to be performed. Finally, the method of writing Test SayHello (), we test the return value of SayHello () to see if it matches the expected result.
This is a test HelloWorld class, we also have to write a Testall class to run the test process. Create a new class under test package, name Testall, and also inherit from J2meunit.framework.TestCase, code as follows:

Testall.java

The following is a reference fragment:
Package test;
Import J2meunit.framework.Test;
Import J2meunit.framework.TestCase;
Import J2meunit.framework.TestSuite;
Import J2meunit.textui.TestRunner;
public class Testall extends TestCase {
Public Testall () {
Super ("null");
}

Public Testall (String name) {
Super (name);
}

/**
* @param args
*/
public static void Main (string[] args) {
string[] Runnerargs = new string[]{"Test. Testhelloworld "};
Testrunner.main (Runnerargs);
}
Public Test Suite () {
TestSuite asuite = new TestSuite ();

Asuite.addtest (New Testhelloworld (). Suite ());

return asuite;
}
}

The main () method is to run J2meunit's runner, note that the Runnerargs is an array of strings containing the names of each test class, and the class name must be with the full package name, otherwise the run will fail.

Run Tests
OK, now it's time to run our tests. Right-click Testall,run as. ->java application. At the end of the run, you can see the following output in the console window:

The following is a reference fragment:
Testrunner.main ()
. F
Time:0ms
Failures!!!
Test Results:
Run:1 failures:1 errors:0
There was 1 failure:
1) Testsayhello (test. Testhelloworld) "Expected:〈hello world!〉but Was:〈hello world〉"


Because when we test, we hope to get the result is "Hello world!", and the actual return result is "Hello World", so will get a failure. Now put "Hello world!" To "Hello World" and run again, the test will be passed. Try it.

Summarize
This is the simple process of using J2meunit for unit testing on the J2ME platform, and for more use and APIs, see the J2meunit documentation.

(Source: http://www.knowsky.com)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.