Use of variables, arrays and complex types in Generic Fixture

Please visit Introducing FitNesse’s Generic Fixture for introductory details about Generic Fixture.Here we shall see how we can use variables in Generic Fixture.You can use a variable myVar in test tables like this to store return value of method1:
| myVar=method1 | arg1 | arg2 | | returnValue |
….
….
or You can use a variable “params” in a test table like this to store the SUT (System Under Test, which is a HashMap here) itself:
!| Generic Fixture | params=java.util.HashMap |
| put | curr_id | 128 |
| size | | 1 |
| toString | |

and later to use this variable myVar in an argument list use this syntax:
| method2 | myVar= | arg2 | | returnValue |
….
or to use this variable myVar in return value use this syntax:
| method3 | arg1 | | myVar= |
….
or You can even use this variable myVar to instantiate a new class, use this syntax:
!| Generic Fixture | myClass | myVar= |
….

Or you can even use this variable myVar in a new table as SUT WITHOUT a new instantiation. In this case GenericFixture starts a new test table with the class type derived from the type of the variable itself. After that you are free to call all the available methods in the class type of myVar. Use this syntax:
!| Generic Fixture | myVar= |
….

myVar=method call will store method’s return value in variable named “myVar”. And subsequent myVar= will populate the stored value.There is a special variable named “this”, that will mean assigning a new value to the System Under Test (SUT) object itself.Variables in Generic Fixture can store any Java object or primitive type, that included arrays also. To re-use the varialbe “params” we used above for storing HashMap in a new table just write this:
!| Generic Fixture | params= |
| size | | 1 |
| toString | |

Now lets take a look at another example:

!| Generic Fixture | Address | 123 | Nutley Street | New York | 21019 | NY |
| getStreetNo | | 123 |
| getCity | | New York |
| state=getState | | NY |
| getFullAddress | | 123 Nutley Street, New York, NY - 21019 |
| setStreetNo | 111 |
| setPostcode | 20133 |
| getPostcode | | 20133 |
| getStreetNo | | 111 |
| getState | | state= |
| setName | John Smith |
| name=getName | | John Smith |

!| Generic Fixture | Address$Name | name= |
| getSurname | | Smith |
| getFirstname | | John |
| toString | | John Smith |

!| Generic Fixture | name= |
| getSurname | | Smith |
| getFirstname | | John |
| toString | | John Smith |

Here first I am storing return value of getState() (String type) in a variable “state” and later using state to compare return value of getState in the test.

We are also using another variable called “name” to store return value of getName() which is of Address$Name type. We are using variable “name” in the constructor of a new new table that operates on Address$Name class. And in the 3rd table we are once again using variable “name” without instantiating a new class by marking variable “name” as SUT.

These examples show how variables can be used to store all standard Java as well as user defined objects.

Code for class Address and inner Address$Name is attached here for reference:

Source Code Address.java

public class Address {
 public static class Name {
    private String surname = null;
    private String firstname = null;       

    public Name() {
    }       

    public Name(Name n) {
       this.surname = n.surname;
       this.firstname = n.firstname;
    }       

    public static Object parse(String s) {
       Name n = new Name();
       String[] arr = s.split(" ");
       if (arr.length == 2) {
          n.setFirstname(arr[0]);
          n.setSurname(arr[1]);
       }
       return n;
    }       

    @Override
    public String toString() {
       return this.firstname + " " + this.surname;
    }       

    public String getSurname() {
       return surname;
    }       

    public void setSurname(String surname) {
       this.surname = surname;
    }       

    public String getFirstname() {
       return firstname;
    }       

    public void setFirstname(String firstname) {
       this.firstname = firstname;
    }
 }       

 private int streetNo = 0;
 private String street = null;
 private String city = null;
 private int postcode = 0;
 private String state = null;
 private Name name = null;       

 public Address() {
    super();
 }       

 public Address(Integer streetNo, String street, String city, int postcode,
       String state) {
    super();
    setAddress(streetNo, street, city, postcode, state);
 }       

 public void setAddress(Integer streetNo, String street, String city,
       int postcode, String state) {
    this.streetNo = streetNo.intValue();
    this.street = street;
    this.city = city;
    this.postcode = postcode;
    this.state = state;
 }       

 public void setStreetNo(Integer streetNo) {
    this.streetNo = streetNo;
 }       

 public Integer getStreetNo() {
    return this.streetNo;
 }       

 public String getStreet() {
    return this.street;
 }       

 public void setCity(String city) {
    this.city = city;
 }       

 public String getCity() {
    return this.city;
 }       

 public void setPostcode(int postcode) {
    this.postcode = postcode;
 }       

 public int getPostcode() {
    return this.postcode;
 }       

 public String getFullAddress() {
    return this.streetNo + " " + this.street + ", " + this.city + ", "
          + this.state + " - " + this.postcode;
 }       

 public void setStreet(String street) {
    this.street = street;
 }       

 public String getState() {
    return state;
 }       

 public void setState(String state) {
    this.state = state;
 }       

 public Name getName() {
    return name;
 }       

 public void setName(Name name) {
    this.name = name;
 }
}

How to input Array parameter: Simply start the value with array: suffix and use delimiter comma ‘,’ to separate out items in array. For example array:G,e,n,e,r,i,c,F,i,x,t,u,r,e means an array of characters or array:1,4,9,16,25 means an array of integers.

Let’s take a look at another example where simple type, this type and array type variables are being used.

!| Generic Fixture | java.lang.String | It's a cool fixture |
| toString | |
| i=indexOf | cool | | 7 |
| this=concat | ; indeed | | It's a cool fixture; indeed |
| equals | It's a cool fixture; indeed | | true |
| equalsIgnoreCase | blank | | false |
| str=concat | blank | | It's a cool fixture; indeed |
| equals | str= | | true |
| matches | ^.*cool fix.*$ | | true |
| indexOf | cool | | i= |
| arr=toCharArray | |
| concat | !!! | | It's a cool fixture; indeed !!! |        

!| Generic Fixture | java.lang.String | arr= |
| toString | | str= |
| toCharArray | | arr= |        

!| Generic Fixture | java.lang.String | str= |
| toString | | str= |
| toCharArray | | arr= |       

!| Generic Fixture | java.lang.String | array:G,e,n,e,r,i,c,F,i,x,t,u,r,e |
| toString | | GenericFixture |
| toCharArray | | G,e,n,e,r,i,c,F,i,x,t,u,r,e |       

!| Generic Fixture | java.lang.Math |
| abs | i= | | 7 |

You can see how I am using this to overwrite the value of the object itself on which all the methods are being executed. Execution of above test will produce below result. Note how actual value of a variable is appended in the result. Also note the use of array type variable “arr” in various operations.

UPDATE:
Added support of in-table variable.method() invocation. eg: if you already have a return value stored in a variable called “myvar” then calling a method foo() on myvar is now allowed in the same test table. Just use following syntax:
| myvar=.foo | arg1 | arg2 | | ret |
Take a look at this example:

!| Generic Fixture | java.lang.String | It's a cool fixture |
| toString | |
| i=indexOf | cool | | 7 |
| this=concat | ; indeed | | It's a cool fixture; indeed |
| equals | It's a cool fixture; indeed | | true |
| equalsIgnoreCase | blank | | false |
| str=concat | blank | | It's a cool fixture; indeed |
| equals | str= | | true |
| str=.substring | 21 | | indeed |
| matches | ^.*cool fix.*$ | | true |
| indexOf | cool | | i= |
| arr=toCharArray | |
| concat | !- !!!-! | | It's a cool fixture; indeed !!! |
| substring | 3 | |
| str=.toCharArray | | arr= |

Note how this test table is using variable “str” to store original string and then calling methods directly on variable “str” using str=.substring and str=.toCharArray calls. You can even do substr=str=.substring to create another variable “substr” to store return value of str=.substring call.

Advertisements

44 thoughts on “Use of variables, arrays and complex types in Generic Fixture

  1. Hi Ajay,

    Sorry for the late reply. I can answer question about Generic Fixture but you seem to be having problems with slim test system. I haven’t tried using slim.

    cheers.

  2. Ajay says:

    Hi Anubhava

    I am trying to run Fitnesse Tests , for some reason I am not able to run it , reason being classpath issue.I get this error :

    Could not invoke constructor for :

    This is my code :

    !define TEST_SYSTEM {slim}
    !path c:\fitnesse\fitnesse.jar

    |import |
    |com.jkk.example.ui.fitnesse|

    ”’Home”’
    !|Home |
    |User Role|HomeTest?|
    |CNB |true |
    |REPT |false |

  3. Hi Tom,
    Let me download sahi myself and try to recreate your problem then I will be in a better position to answer your problem. Just bear with me for a day more.

    Thanks.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s