Getting FIT with Fitnesse: Getting Started

Useful links:
  1. Cory Foy does it! Fitnesse with Dotnet to suck em' into showing how to do it with Ruby as well.
  2. Ron Jeffries XP Mag article on Fitnesse with Ruby
  3. Gojko's PDF on getting Fitnesse up and running with DotNet
  4. Fitnesse Mailing List

On our way to Fitnesse
First we need to install Fitness, which will get FIT along with it. Go to http://fitnesse.org/ Click on Download. Get the full distribution zip. Next unzip it to a folder say c:\fitnesse.
Now just to be safe in case you have something running on the default port 80. Edit the run.bat batch file in the c:\fitnesse folder. Modify it as shown below to use a different port.

java -cp fitnesse.jar fitnesse.FitNesse %1 %2 %3 %4 %5 -p 2345
pause
That's it. Double click / Launch run.bat.

L:\fitnesse>java -cp fitnesse.jar fitnesse.FitNesse -p 2345
FitNesse (20070619) Started...
port: 2345
root page: fitnesse.wiki.FileSystemPage at ./FitNesseRoot
logger: none
authenticator: fitnesse.authentication.PromiscuousAuthenticator
html page factory: fitnesse.html.HtmlPageFactory
page version expiration set to 14 days.
PromiscuousAuthenticator... hmm wonder why its named like that.. but let us go on.

Fire up a browser to http://localhost:2345/
You should see the FrontPage with the Fitnesse Gauge Pointing to Green. Good work!

Does it work?

By default, Fitnesse is set to work with Java. So mustering the little bits of java that I know, lets forge ahead. Hmm.. lil reading up the wiki documentation.. Lets try the 2 Min Example
Next I spent a good 30 minutes, wondering how do I start typing that table. How do I create a blank page? I'll cut right to the chase. Just go to the address bar, think of a good name that is a WikiWord.. I chose 'GrandEntraceToMyAcceptanceTests'.. type in
http://localhost:2345/GrandEntraceToMyAcceptanceTests
and... (Right click and open the image in a new window/tab to make it readable)

Quick Batman.. click on the 'create this page' link. Its simple when you know how. And soon you should be on the 'Edit Page' screen, with a large textbox. Lets master page creation.. we don't want all our tests on one page. Type in ColumnFixtureSample and hit the Save button at the bottom of the page. You're back to the Entrance page but this time it has a set of buttons on the left and the content area shows ColumnFixtureSample?

Click on the ? link to edit the ColumnFixtureSample page.. welcome to the land of wikis.

Now lets masquerade as an excel-happy customer. He writes the following in the spreadsheet
Starting from an initial credit of 500, anytime the person spends money, the balance should be deducted by the same amount.


Copy this range and switch back to the wiki page in edit mode.
Enter the name of the java Fixture class that should be called as the first line. Press Enter. Paste the contents of the excel range too. You should see
com.gishu.trial.TestFixture
moneySpent balance?
100 400
100 300
Press the nifty 'Spreadsheet to Fitnesse' button at the bottom and ...
!|com.gishu.trial.TestFixture|
|moneySpent|balance?|

|100|400|

|100|300|
We have the fitnesse table ready. Hit Save.
Now to make this page testable, we need to set the 'Test' page property. Click on 'Properties' on the left panel. Check the 'Test' checkbox in the Actions group and click 'Save Properties'. You should now see that the page has a Test button at the top of the left panel.
Lets try it.. and we see a dirty yellow telling us we had an Exception - "Could not find fixture: com.gishu.trial.TestFixture."
But that was expected.
Let me code the java fixture up in L:\Gishu\Java\com\gishu\trial\TestFixture.java
package com.gishu.trial;

import fit.ColumnFixture;

public class TestFixture extends ColumnFixture
{
public int moneySpent;
public int balance()
{
UnderCoverDomainClass obDomainClass = new UnderCoverDomainClass();
obDomainClass.spend(moneySpent);
return obDomainClass.getBalance();
}
}

// class from the application source
class UnderCoverDomainClass
{
private int m_iBalance;
public void spend(int iMoneySpent)
{
m_iBalance -= iMoneySpent;
}
public int getBalance()
{
return m_iBalance;
}
}
Code is pretty simple. The test fixture has public data members for each column in the wiki fixture table, whose value is set to cell value. Every column name that ends in a ? signifies a method call, the value in the cell is the expected return value of that method. So the first row, would set moneySpent to 100 and then verify if the balance() method returns 400.
The fixture methods are supposed to call the application code to be verified. Here represented by the UnderCoverDomainClass.

Compile with...
L:\Gishu\Java\com\gishu\trial>javac -classpath l:\fitnesse\fitnesse.jar TestFixture.java
Now we need to tell fitnesse where to find our class files. Looks like this page has what we need.
So we edit our ColumnFixtureSample page to

!path L:\Gishu\Java

!|com.gishu.trial.TestFixture|
|moneySpent|balance?|
|100|400|
|100|300|
Save and click on Test again and we see that the tests have failed, with expected and actual values. The set classpath is being shown.

We seem to be getting a value of -100 as the balance always. Well.. we have not given the default credit of 500. Initialize the m_iBalance variable in UnderCoverDomainClass to 500. Save and Recompile. Lets run our tests again. Click Test again. This time, the first test passes. Second test returned 400 instead of 300.
Something is amiss.. Let me re-read the page on FixtureCode Aha! Test isolation. Read the section 'What that call to StaticGame is for'
So we need a singleton instance of UnderCoverDomainClass to be operated on by each row. So add another class

class SingletonDelivery
{
private static UnderCoverDomainClass m_obInstance = null;
public static UnderCoverDomainClass getInstance()
{
if (m_obInstance == null)
{
m_obInstance = new UnderCoverDomainClass();
}
return m_obInstance;
}
}
Update the balance method in the fixture to use SingletonDelivery.getInstance() instead of a new UnderCoverDomainClass(). Save and Recompile. Run Tests. Woo hoo!!!

And there we have it.
Next I'm gonna try writing some acceptance tests for a small Ruby on Rails project that I am gonna work on. But thats the next post...

No comments:

Post a Comment