选择显示字体大小

使用j2meunit测试j2me程序

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

以下是引用片段:
package hello;
public class helloworld {
public helloworld(){
}

public string sayhello(){
return "hello world";
}
}

hello.helloworldmidlet.java

以下是引用片段:
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 {
}
}

程序的功能就是在一个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

以下是引用片段:
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);
}
}

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

testall.java

以下是引用片段:
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;
}
}

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

运行测试
好了,现在可以运行我们的测试了。右击testall,run as..->java application。运行结束,可以在console窗口中看到如下输出:

以下是引用片段:
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〉"

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

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


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons