Writing Advanced Generic Fixture Test Scripts (loops, conditions etc)

It’s been really long time since I wrote my last article on Generic Fixture and FitNesse. My projects kept me on my toes for most of the time but in between whatever time I got I was probably just too lethargic to shoot down a full post. However it doesn’t mean that I wasn’t developing and supporting Generic Fixture all this time. I made it a point to respond to each and every question that was asked in the comments section. I also gave ample hints on these features in few of my comments on my Blog.So as the title suggests we are going to cover advanced test scripting on this post which means loops, conditions, branches, expressions, exception handling etc. Let me make it clear at this point that I am not recommending using these features in each and every test story because it will only make test stories a bit harder to read and maintain. It is best to handle these things in a helper class and you should deal with helper class though Generic Fixture.

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.

One thought on “Writing Advanced Generic Fixture Test Scripts (loops, conditions etc)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s