Java Fitnesse quick start tutorial

Posted: February 16, 2014 in Uncategorized

This is a short and quick tutorial to get you started on Fitnesse and I will list down the steps below.
The tutorials on Fitnesse.org should do the trick,

Prerequisites: Why Fitnesse is used(if you are here, I am guessing, you already know this, and you want to get your hands dirty ūüôā )
Code repository: https://github.com/anirbanchowdhury/anirbans_playspace.git
1. Fitness is available at http://fitnesse.org/FitNesseDownload/ . Download and setup

Upon successful setup, you should see the customary http://localhost/FitNesse.UserGuide.TwoMinuteExample (Your port might differ, I am using the default 80)

2. Once, you are at the 2 minute example, an easy way to test your setup would be to edit and add a sample test case, for eg. denominator is 1. The below pic is a before pic, and if you notice, there is no denominator with 1 and no Edit button.

Image

Lets edit this wiki page, and add numerator = 10, denominator = 1, and quotient ? = 10

http://localhost/FitNesse.UserGuide.TwoMinuteExample?edit
Search for the table below and add the last row.

| eg.Division |
 | numerator | denominator | quotient? |
 | 10 | 2 | 5.0 |
 | 12.6 | 3 | 4.2 |
 | 22 | 7 | ~=3.14 |
 | 9 | 3 | <5 |
 | 11 | 2 | 4<_<6 |
 | 100 | 4 | 33 |
 | 10 | 1 | 10 |

Save and Test. The last assertion should fail, which is expected..

Image

3. Now, lets customize.  Lets put up

  • New test page
  • Add Fixture which sits between System Under Test(SUT)
  • Create a very simple biz logic class which mimics the SUT)

4.  Test Page

Edit  http://localhost/FitNesse.UserGuide.TwoMinuteExample?edit
and add next line at the end and save the page.

>SampleExample

Once you save the page, click on the [?] which should open up a new test page. Edit it. Replace everything with below:

!define TEST_SYSTEM {slim}
!path D:\workspace\FitnesseBusinessApplication\bin
 |import|
 |com.fitnesse.fixture|
 !define COLLAPSE_SETUP {true}
 !define COLLAPSE_TEARDOWN {true}

!|Division Program |
 |Numerator|Denominator|quotient?|
 | 10 | 2 | 5.0 |
 | 12.6 | 3 | 4.2 |
 | 22 | 7 | ~=3.14 |
 | 9 | 3 | <5 |
 | 11 | 2 | <6 |
 | 10 | 1 | 10.0 |

From here onwards, it is a Java implementation, and the below steps should be customized for your end system (.Net etc)

Everything below is coded on Eclipse and finally the class files are generated under :

D:\workspace\FitnesseBusinessApplication\bin

5. SUT (Business Logic)

package com.biz.calculator;

public class Calculator {
private float numerator;
private float denominator;
private float quotient;

public Calculator(float numerator, float denominator) {
 super();
 this.numerator = numerator;
 this.denominator = denominator;
 }
 public float getNumerator() {
 return numerator;
 }
 public void setNumerator(float numerator) {
 this.numerator = numerator;
 }
 public float getDenominator() {
 return denominator;
 }
 public void setDenominator(float denominator) {
 this.denominator = denominator;
 }
 public float getQuotient() {
 return numerator/denominator;
 }
}

6. Fixture class which communicates between the SUT and the Fitnesse HTML.
(Actually, this is compiled to D:\workspace\FitnesseBusinessApplication\bin which is present under !path on the Fitnesse page)

package com.fitnesse.fixture;
import com.biz.calculator.Calculator;

public class DivisionProgram {

private float numerator;
 private float denominator;
 private float quotient;

public void setNumerator(float numerator) {
 this.numerator = numerator;
 }

public void setDenominator(float denominator) {
 this.denominator = denominator;
 }

public float quotient() {
 Calculator calculator = new Calculator(numerator, denominator);
 this.quotient = calculator.getQuotient();
 return this.quotient;
 }
 }

Few common problems:

a. Could not invoke constructor for DivisionProgram[0]

FitnesseIssues1

This means, Fitnesse is not able to resolve the Fixtures class, check the below settings on your Fitnesse page

Division Program should point to DivisionProgram class in fixture.

!path D:\workspace\FitnesseBusinessApplication\bin
|import|
|com.fitnesse.fixture|
!|Division Program              |

b.  Method Quotient[0] not found in com.fitnesse.fixture.DivisionProgram.
FitnesseIssues2

This will not come if you follow the instructions properly, but if you customize, then this might happen. Basically, Quotient? searches for a getter named Quotient() on the Fixture, but if you check again, you have a getter method named quotient() [Case sensitive]

Advertisements
Comments
  1. vyask1971 says:

    Very well explained. Nice work, thx

  2. LeFrog says:

    Nice work !

    Be careful: If you just copy and paste the code for the Fitnesse Test Page (Step 4) there will be blank spaces at the beginning of some lines. In my case Fitnesse was not able to compile the code in a right way and I could not execute any test (always got “Could not invoke constructor for DivisionProgram[0]”). Removing the blank spaces at the beginning of any line solved the problem.

  3. coueyamin says:

    Reblogged this on togetherandcompany and commented:
    ŤĹ¨šłÄŚŹĎ~~~

  4. Jonathan says:

    For those who copy and paste the code on this page: when you get errors, look for spaces in front of the lines (they will corrupt the code)

  5. Fit Lerner says:

    Hey, I’m quite new to FitNesse and was trying to understand the concepts for last few days.Nice article. Thanks.

    Quick question. Do we actually test the ‘Fixture’? How do we link the SUT with teh Fixture?

    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