站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题使用J2MEUnit测试J2ME程序

使用J2MEUnit测试J2ME程序

摘要: 使用J2MEUnit测试J2ME程序

作者:magic003 文章来源:http://www.j2medev.com/Article/ShowArticle.asp?ArticleID=903

JUnit是一个优秀的用于单元测试的框架,在j2se,j2ee的开发过程被广泛使用,它使得代码的质量得到更好的监控和维护。然而对于j2me平台一切就不是如此简单了,由于手持设备需要更多的考虑性能问题,j2me平台并没有提供反射(Reflectiong)的API,因此JUnit的很多功能无法在j2me上实现,程序员也就无法在j2me平台上使用JUnit进行单元测试。本文将介绍一个能对j2me应用程序进行单元测试的工具:J2MEUnit。j2meunit是一个基于JUnit实现的针对j2me平台的单元测试框架。详细介绍请看:http://j2meunit.sourceforge.net/。

运行环境
本文的示例程序的运行和测试都是在eclipse3.1下进行,并需要到如下网址下载J2MEUnit的最新版本:
http://j2meunit.sourceforge.net/
下载后如为rar/zip压缩包,须先解压。

HelloWorld 实例
本文将通过一个HelloWorld的例子来介绍J2MEUnit的基本使用方法。首先在eclipse中新建一个J2ME Midlet Suite工程。File->New->Project->J2ME Midlet Suite,取名为HelloMidlet。
此工程包括两个类:HelloWorld.java和HelloWorldMidlet.java。首先在工程底下新建包名hello,然后在hello包底下创建一个新类:HelloWorld.java,和一个新的J2ME Midlet:HelloWorldMidlet.java。代码如下:

hello.HelloWorld.java

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
package hello;
public class HelloWorld {
public HelloWorld(){
}

public String sayHello(){
return "Hello World";
}
}</td></tr></table>

hello.HelloWorldMidlet.java

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
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 {
}
}
</td></tr></table>

程序的功能就是在一个Alert中输出“HelloWorld”的字符串,代码十分简单,在此不做过多论述。编译后,可以运行程序,如果运行后能在Alert中看到“HelloWorld”,说明程序运行正常。接着开始写测试代码。

编写测试类
写测试文件时需要用到J2MEUnit的类库,首先需要将类库加入到工程的build path中。右击工程名->properties->Java Build Path->Libraries, Add External JARs,选择刚才下载的文件,然后确定。
现在需要对HelloWorld.java中的sayHello()方法进行测试。在工程目录底下新建一个包名test,然后新建一个名为TestHelloWorld.java的类,主要对HelloWorld中的sayHello()方法进行测试。虽然J2MEUnit现在不支持反射机制,但是仍然按照JUnit的规范命名,以便日后的代码移植。TestHelloWorld代码如下所示:

TestHelloWorld.java

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
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(){}

/**
* 构造函数
* @param sTestName 测试方法的名称
* @param rTestMethod 测试的方法
*/
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;
}

/**
* 测试sayHello()方法
*
*/
public void testSayHello(){
String s = hello.sayHello();
assertEquals("Hello World!",s);
}
}</td></tr></table>

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
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(){}

/**
* 构造函数
* @param sTestName 测试方法的名称
* @param rTestMethod 测试的方法
*/
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;
}

/**
* 测试sayHello()方法
*
*/
public void testSayHello(){
String s = hello.sayHello();
assertEquals("Hello World!",s);
}
}</td></tr></table>

首先测试类必须从j2meunit.framework.TestCase继承。只是对HelloWorld类从测试,因此包含一个HelloWorld的对象。接着,重载了两个构造函数。同样,重写了TestCase的两个方法:setUp()和tearDown()。在setUp()中对hello进行了初始化。由于J2MEUnit不支持反射机制,因此我们必须自己写suite(),创建一个TestSuite的对象,然后加入要进行的测试方法。最后就是写测试sayHello()的方法,我们对sayHello()的返回值进行测试,看与预期结果是否匹配。
这是一个测试HelloWorld的类,我们还要写一个TestAll的类要运行测试过程。在test包下创建一个新类,取名TestAll,同样,也是继承自j2meunit.framework.TestCase,代码如下:

TestAll.java

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
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;
}
}
</td></tr></table>

其中的main()方法是运行J2MEUnit的runner的,注意,其中的runnerArgs,是包含了各个测试类的名称的字符串数组,并且类名一定要带上完整的包名,否则运行会失败。

运行测试
好了,现在可以运行我们的测试了。右击TestAll,Run As..->Java Application。运行结束,可以在console窗口中看到如下输出:

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
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〉"</td></tr></table>

<table style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellspacing="0" cellpadding="6" width="95%" align="center" border="0"><tr><td style="WORD-WRAP: break-word" bgcolor="#f3f3f3">以下是引用片段:
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〉"</td></tr></table>

因为我们在测试时,希望得到的结果是“Hello World!”,而实际返回结果是“Hello World”,所以会得到一个Failure。现在把“Hello World!”改成“Hello World”,再次运行,则会输出测试通过。Try it!

总结
这就是在J2ME平台上使用J2MEUnit进行单元测试的简单过程,更多的使用和API请参见J2MEUnit的文档。



↑返回目录
前一篇: J2ME游戏 之 NORMAL-FRAME
后一篇: J2ME-定时器(TimerTask)使用及初探