junit 单元测试学深入人心的同时,也发现它对用户交互测试无能为力:
testcase 允许测试人员作动态的修改
可以在test case 中实现一个测试参数输入功能(ui 或参数配置文件)来解决这个问题,但实现这些功能的代价与重复工作量会很大。
testcase 可以方便地重复使用、组合、保存
不是所有所有测试环境下,都容许打开一个重量级的 java ide 编写有严格规范的 java 代码。这就是脚本语言受欢迎的原因。
beanshell 可以较好解决以上问题。
1.beanshell基本:
bsh.interpreter 是beanshell 的主要接口。
以下可以实现一个简单的java shell:
public class testint {
public static void main(string[] args) {
bsh.interpreter.main(args);
}
}
|
结果:
beanshell 2.0b4 - by pat niemeyer (pat@pat.net)
bsh % system.out.println("hello beanshell");
hello beanshell
bsh %
你也可以用以下代码实现同样的功能,代码中可以比较明显地看出其结构:
public static void main(string[] args) {
reader in = new inputstreamreader( system.in );
printstream out = system.out;
printstream err = system.err;
boolean interactive = true;;
bsh.interpreter i = new interpreter( in, out, err, interactive );
i.run();//线程在这里阻塞读system.in
}
|
1.1.beanshell上下文(context/namespace):
public static void main(string[] args) throws throwable {
reader in = new inputstreamreader( system.in );
printstream out = system.out;
printstream err = system.err;
boolean interactive = true;;
bsh.interpreter i = new interpreter( in, out, err, interactive );
collection theobjectreadyforshelluser = new arraylist();
theobjectreadyforshelluser.add("str1");
i.set("myobject", theobjectreadyforshelluser);
i.run();
}
|
用户的ui:
beanshell 2.0b4 - by pat niemeyer (pat@pat.net)
bsh % system.out.println( myobject.get(0) );
str1
bsh %
shell的上下文在测试中特别有用。想一下,如果将上面的“theobjectreadyforshelluser”换成一个预先为测试用户生成的rmi本地接口存根,由测试用户调用相应的存根方法。这可应用于动态测试,也可以应用于系统的远程管理。
1.2.静态java代码与动态java代码的组合使用。
public static void main(string[] args) throws throwable {
reader in = new inputstreamreader( system.in );
printstream out = system.out;
printstream err = system.err;
boolean interactive = true;;
bsh.interpreter i = new interpreter( in, out, err, interactive );
//show a dialog for user to input command.
string command = joptionpane.showinputdialog( "input command(s)" );
i.eval( command );//run the command
}
|