|
|
 |
首页 … 技术文档 Technical Articles |
|
| |
| |
|
| (时间:2007-8-3 15:04:29 共有
人次浏览) |
|
JUnit 单元测试学深入人心的同时,也发现它对用户交互测试无能为力:
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
} |
|
|
|
【打印该页】 【关闭窗口】 |
|
此技术资料来自网络,仅供参考。未经许可,不得转载。
若有侵权,请及时与我们取得联系! |
| |
|
|
|
|