Creating a Simple Java Test Using Fitnesse


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.

Download Fitnesse

Download fitnesse-standalone-jar  from www.fitnesse.org

Launch Fitnesse

The Fitnesse Wiki can be launched by double-clicking the jar file or by opening a command prompt and execute the jar file as follows

java -jar fitnesse-standalone.jar

By default, the wiki will launch on port 80.

View the Fitnesse Wiki

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

fitnesse001

Create the Java Code

Using Eclipse create a new Maven Project
Make it a  Simple Project as follows:
Group Id: com.skills421.examples.fitnesse
Artifact Id: SimpleExample

Create the following Calculator Class

package com.skills421.examples.fitnesse;
public class Calculator
{
	private double value1;
	private double value2;
	private String operator;
	//private double calculate;

	

	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());
	}
}

Build the Project

Now build the project using Maven Install from within Eclipse.
You will note that the following directory structure will be created:

E:\workspace\Skills421\examples\SimpleExample\target\classes

Note that in the example above E:\workspace\Skills421\examples is my project folder.

A jar file will also be created in the following subdirectory of .m2 in your home directory;

repository\com\skills421\examples\fitnesse\SimpleExample\0.0.1-SNAPSHOT
SimpleExample-0.0.1-SNAPSHOT.jar

Add Links to Our Test Pages

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

>SimpleClassTest
>SimpleJarTest

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.WikiWord]] to the page.

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

>SimpleClassTest
>SimpleJarTest

!note Release ${FITNESSE_VERSION}

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

fitnesse002

Create the SimpleClassTest

Click on the red ? 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 E:\workspace\Skills421\examples\SimpleExample\target\classes

| com.skills421.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:
test01.001

Click Test and you should see the following:

test01.002

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 E:\workspace\Skills421\examples\SimpleExample\target\classes

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

| import |
| com.skills421.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:

test01.003

And our executed tests look like this:

test01.004

Advertisements

2 thoughts on “Creating a Simple Java Test Using Fitnesse

  1. Pingback: Executing a Jar File from Fitnesse | Skills421

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