While JUnit unit testing has been deeply rooted, it has also found itself powerless to test user interaction:
TestCase allows testers to make dynamic changes
A test parameter input function (UI or parameter profile) can be implemented in test case to solve the problem, but the cost of implementing these features and the amount of duplication is significant.
TestCase can be reused, combined, and saved easily
Not all test environments allow you to open a heavyweight Java IDE to write code that has strict specifications. That's why scripting languages are popular.
BeanShell can solve the above problems better.
1.BeanShell Basic:
BSh. Interpreter is the main interface of BeanShell.
Here's how to implement a simple Java Shell:
public class TestInt {
public static void main(String[] args) {
bsh.Interpreter.main(args);
}
}
Results:
BeanShell 2.0b4-by Pat Niemeyer (pat@pat.net)
BSh% System.out.println ("Hello BeanShell");
Hello BeanShell
BSH%
You can also use the following code to achieve the same function, the code can be more obvious to see its structure:
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 (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();
}
User's UI:
BeanShell 2.0b4-by Pat Niemeyer (pat@pat.net)
BSh% System.out.println (myobject.get (0));
Str1
BSH%
The shell's context is particularly useful in testing. Think about it, if the "Theobjectreadyforshelluser" above is replaced by a RMI local interface stub generated for the test user, the corresponding stub method is invoked by the test user. This can be applied to dynamic testing or to remote administration of the system.
1.2. Static Java code is used in combination with dynamic Java code.
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
}