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.
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:
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.
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.