Chinaunix首页 | 论坛 | 博客
  • 博客访问: 442256
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 17:21:49

JUnit 单元测试学深入人心的同时,也发现它对用户交互测试无能为力:

TestCase 允许测试人员作动态的修改
    可以在Test Case 中实现一个测试参数输入功能(UI 或参数配置文件)来解决这个问题,但实现这些功能的代价与重复工作量会很大。

TestCase 可以方便地重复使用、组合、保存
    不是所有所有测试环境下,都容许打开一个重量级的 IDE 编写有严格规范的 代码。这就是脚本语言受欢迎的原因。

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

}

【责编:Peng】

--------------------next---------------------

阅读(129) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~