你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / Linux开发
BeanShell在人工测试与管理之中的应用
 
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
}

(编辑:aniston)

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089