However there are scenarios when it is unavoidable to encapsulate everything in helper classes and hence need of these advanced features in Generic Fixture. We are going to cover few of these scenarios in example section of these advanced scripts.
If you recall that Generic Fixture simply lets you execute all the public method of any Java class from FitNesse Wiki. So the obvious question that comes to anybody’s mind is how to do looping, branching etc from a method call. I took a cue from Apache JMeter software that lets you use a very useful utility called Bean Shell which is fully Java compatible scripting engine for evaluating data returned from JMeter test scripts. Let me just stress here that JMeter is an excellent tool for performance and load testing of Java applications and web portals. We use JMeter extensively in our projects for performance benchmarking.
Now coming back to Bean Shell (bsh), it’s becoming a top choice for Java scripting (not to be confused with JavaScript). It is being used by Apache, Sun, Bea in their products. All you need a bean shell interpreter and you can supply a block of Java code at run time without changing & compiling your existing Java code and your Java code in bsh script will be executed at run time. And how would it fit into Generic Fixture model? Well we can simply pass full blown Java code with loops and branches in a String to bsh’s eval method and bsh will just execute whole Java code and return us the results. All we need is a freely downloadable jar file from Bean Shell website called bsh.jar so just go ahead and download bsh-*.jar from this Bean Shell download page.
So how do you get an instance of bsh interpreter? You can simply call a static method inside Generic Fixture called bsh() using GenericFixture.bsh notation (see example below). And be assured that Generic Fixture will give you same bsh interpreter instance (singleton) every time you call GenericFixture.bsh. But of course if you want an instance of bsh in a dynamic variable just use static method getBeanShell() of GenerixFixture. For ex: bsh=GenericFixture.getBeanShell will give you instance of bsh interpreter in a dynamic variable bsh=
A note about your dynamically created variables: All the variables that you’ve created in your test story will be available in bsh as well. Generic Fixture will create and populate these variables inside bsh interpreter for your convenience. And remember that while writing your Java code which will be interpreted by bsh you must not use varName= notation of Generic Fixture, just use it like any other Java variable like varName (without = at the end).
Enough talking, now let’s look at some working examples:
!path genericfixture.jar !path bsh.jar !| Generic Fixture | java.lang.Math | | n=sqrt | 9 | | 3.0 | | GenericFixture.bsh | n >= 3 | | true | | GenericFixture.bsh | 4 <= n | | false |
Here in this example after Math.sqrt call value of dyamic variable n= is 3.0. Next 2 lines are executing Java code inside bsh. First condition n >= 3 is evaluated to true by bsh and second 4 <= n is compared to expected value false. Note that same variable n= became automatically available to bsh as n.
Now lets take a look at a looping example using bean shell.
!path genericfixture.jar !path bsh.jar !| Generic Fixture | list=java.util.ArrayList | | GenericFixture.bsh | for(int i=0;i<10;i++) list.add(new Integer(i+1)); | | toString | |
In this example we have created an instance of ArrayList in a variable list=. Then we are passing a simple Java code with loop that stores first 10 numbers in this ArrayList to bsh. And finally we are just printing this list using toString() method of ArrayList.
Looks simple, isn’t it? So far I have covered some very basic examples. But I chose them to give you an idea of how to execute your custom Java code at run time and how to evaluate conditions and loops. In my next Blog post (hopefully shouldn’t take long time) I will cover a more useful example of testing a Restful web service using these advanced features.
How can we create a control structure where in we shift to java and fitnesse code.