Creating a Simple Java Test Using Fitnesse


Video

Overview

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="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.skills421.java.examples.fitnesse</groupId>
    <artifactId>FitnesseExample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.fitnesse/fitnesse -->
        <dependency>
            <groupId>org.fitnesse</groupId>
            <artifactId>fitnesse</artifactId>
            <version>20190716</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>fitnesse</id>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <execution>
                                <id>start-fitnesse</id>
                                <phase>test</phase>
                                <configuration>
                                    <target>
                                        <echo taskname="fitnesse" message="Starting FitNesse…" />
                                        <java classname="fitnesseMain.FitNesseMain" classpathref="maven.runtime.classpath" fork="true">
                                            <arg line="-p 8000" />
                                            <arg line="-d ." />
                                        </java>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

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

package com.skills421.java.examples.fitnesse;

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;

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

        return calculate;
    }

    public static void main(String[] args)
    {
        Calculator cp = new Calculator();
        cp.setValue1(3.0);
        cp.setOperator("+");
        cp.setValue2(5.0);

        System.out.println(cp.calculate());
    }
}

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

>SimpleClassTest

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'' |


>SimpleClassTest
!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

| com.skills421.java.examples.fitnesse.Calculator |
| 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

| com.skills421.java.examples.fitnesse.Calculator |
| Value1 | Operator | Value2 | calculate? |
| 3.0 | + | 5.0 | 8.0 |
| 2.0 | * | 3.5 | 7.0 |

| import |
| com.skills421.java.examples.fitnesse |

!| 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

https://github.com/skills421/FitnesseExample

3 thoughts on “Creating a Simple Java Test Using Fitnesse

Add yours

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

Up ↑

%d bloggers like this: