Creating a Simple Java Test Using Fitnesse



Finesse, is a Wiki Engine that can be integrated with Java Code to run test suites from the wiki page. In brief, you create tables of inputs and expected outputs and a simple click on the Test button on that page can run a suite of tests, invoking Java classes or jars where necessary.

In this example we will create a simple Test Wiki Page that calls a calculator class with two operands and an operator and tests the output when calling the calculate method.

Create a Maven Project with the following pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""



        <!-- -->

                                        <echo taskname="fitnesse" message="Starting FitNesse…" />
                                        <java classname="fitnesseMain.FitNesseMain" classpathref="maven.runtime.classpath" fork="true">
                                            <arg line="-p 8000" />
                                            <arg line="-d ." />

Create a Maven Run Configuration

Create a Maven Run Configuration called Fitnesse. Edit the command line for the run configuration as follows:

-Pfitnesse test

Launch Fitnesse

Run the new run configuration. This will have the effect of executing

mvn -Pfitnesse test

View the Fitnesse Wiki

Open a browser window to the page http://localhost:8000/

Create the Java Code


public class Calculator
    private double value1;
    private double value2;
    private String operator;

    public double getValue1()
        return value1;

    public void setValue1(double value1)
        this.value1 = value1;

    public double getValue2()
        return value2;

    public void setValue2(double value2)
        this.value2 = value2;

    public String getOperator()
        return operator;

    public void setOperator(String operator)
        this.operator = operator;

    public double calculate()
        double calculate;

            calculate = value1 + value2;
        else if(operator.equals("-"))
            calculate = value1 - value2;
        else if(operator.equals("*"))
            calculate = value1 * value2;
            calculate = value1 / value2;

        return calculate;

    public static void main(String[] args)
        Calculator cp = new Calculator();


Restart Fitnesse

Stop and re-run the Maven Fitnesse Run Configuration that you created earlier

Add Links to Our Test Page

From the main wiki page – http://localhost:8000 click on Edit and add the following lines above the !note Release line as follows


to give something like:

!1 Welcome to [[FitNesse][FitNesse.FitNesse]]!
!3 ''The fully integrated stand-alone acceptance testing framework and wiki.''
# Here is a good place to add your first page (WikiWord). For example, MyTopLevelApplicationPage

To add your first "page", click the [[Edit][.FrontPage?edit]] button and add a [[!-WikiWord-!][.FitNesse.UserGuide.FitNesseWiki.WikiWord]] to the page.

|'''To Learn More...'''                                                                                    |
|[[User Guide][.FitNesse.UserGuide]]                           |''Answer the rest of your questions here.''|
|[[A Two-Minute Example][.FitNesse.UserGuide.TwoMinuteExample]]|''A brief example. Read this one next.''   |
|[[Acceptance Tests][.FitNesse.SuiteAcceptanceTests]]          |''FitNesse's suite of Acceptance Tests''   |
|[[Release Notes][.FitNesse.ReleaseNotes]]                     |''Find out about FitNesse's new features'' |

!note Release ${FITNESSE_VERSION}

Now click on Save and you will see something like this:

Create the SimpleClassTest

Click on the ? next to SimpleClassTest and a new page will be created and opened in Edit view.

Replace all the code on that page with the following:

!define TEST_SYSTEM {slim}

!path /Skills421/Examples/Java/FitnesseExample/target/classes

| |
| Value1 | Operator | Value2 | calculate? |
| 3.0 | + | 5.0 | 8.0 |
| 2.0 | * | 3.5 | 7.0 |

Notice how we provide a path to the classes directory.  Our Calculator class is within a package in that directory.

We have provided the full package and classname for our Calculator class in the line following.

For setting our input values we use the last part of the setter method  in the headers – so setValue1 become Value1, setOperator becomes Operator and setValue2 becomes Value2.

Finally, the method we want to invoke in the test is followed by a ? – so calculate() becomes calculate?

The last two lines show our inputs and expected outputs.

Click Save and our page will look like this:

Click Test and you should see the following:

The output shows that all our tests have completed successfully.

Using Import in Fitnesse

This works fine but now let’s add to our test wiki page to give the following:

!define TEST_SYSTEM {slim}

!path /Skills421/Examples/Java/FitnesseExample/target/classes

| |
| Value1 | Operator | Value2 | calculate? |
| 3.0 | + | 5.0 | 8.0 |
| 2.0 | * | 3.5 | 7.0 |

| import |
| |

!| Calculator |
| Value1 | Operator | Value2 | calculate? |
| 3.0 | + | 5.0 | 8.0 |
| 2.0 | * | 3.5 | 7.0 |
| 6.0 | / | 3.0 | 2.0 |
| 5.0 | / | 2.0 | 2.0 |

Now our page looks like this:

And our executed tests look like this:

Source Code



Leave a Reply

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

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

Facebook photo

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

Connecting to %s