Newbie to fitnesse, I want to test Rest APIS using RestFixture, but did not know where to start.
Can some one give me step by step workflow and the sample decision table for GET, POST,and DELETE requests.
we use Json responses how can i input them in the decision tables when posting it?
Thanks
This answer provides a cookbook to install RestFixture and a section to present the Acceptance test framework.
Installation
One way to understand how to start with RestFixture is look at RestFixtureLiveDoc. The project provides the documentation for RestFixture itself and it is managed by Maven. Should you prefer going this way, please read below.
Assumptions
java -version
)mvn --version
)Cookbook
Create an empty Maven project (as explained here). In a nutshell:
mvn archetype:generate -DgroupId=smartrics.restfixture.sample -DartifactId=scratch -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
src/test/java/smartrics/restfixture/sample/*.java
src/main/java/smartrics/restfixture/sample/*.java
cd scratch
(or to whatever name you have chosen) and editpom.xml
mvn package
and build/copy dependencies (check them in thetarget/dependencies
directory)mvn clean package
start.bat
with this content, to create a start command for fitnesse (On Linux you should be able to create an equivalentstart.sh
):start.bat
(it will take some time for FitNesse to create all the relevant resources)http://localhost:9090
to check it worksCTRL-C
With FitNesse running, got to the home page and edit.
MyTestSamples
somewhere in the page, then save.MyTestSamples[?]
because the page doesn't exist.!define TEST_SYSTEM {slim} !path target/dependencies/*.jar
to add a reference to the RestFixture classes and dependencies and use the Slim Test system
MyTestSamples
page Add a new Test page from the Add menu.MySample1
, saveMySample1
, edit the page and type the following content. Then saveYou could achieve the same setup by constructing the project by hand by downloading and unzipping the RestFixture with dependencies (download from the link bin.zip here). But I find the Maven approach simpler in the longer run.
Logging
RestFixture uses slf4j but doesn't come with logger bridge. To complete the configuration of the logging system add the following dependencies:
Then create the configuration file at
src\main\resources\log4j.xml
with the following contentThe
httpclient.wire
logger can be set to DEBUG to observe HTTP traffic on the wire.For logging in the FitNesse server (outside the scope of this tutorial) check here
JUnit runner:
FitNesse tests can be run via JUnit as well, using the Maven Surefire plugin.
Create a test class runner in
src/test/java/smartrics/restfixture/sample/RunnerTest.java
Then install the surefire plugin in the pom file:
Save the pom.xml file and run
mvn clean package
to run the build with tests. The build will pass and the report will be available in/target/fitnesse-junit/MyTestSamples.MySample1.html
References
Acceptance tests
RestFixture has been designed to focus on the REST API exposed by the service under test. Mixing and matching Wiki content with structured fixtures does provide the best of both worlds for providing live documentation.
RestFixture can also be extended to provide additional functionality.
When writing acceptance tests focused on the API it's important highlight the parts of an HTTP request/response interaction: the VERB, the URI, the request/response BODY, the request/response HEADERs and the RETURN status code.
RestFixture does so by expressing each of these parts in a FitNesse table form:
The
<expected ...>
cells may be empty. If empty, no expectation will be checked.setHeaders
andsetBody
are only relevant for verbs requiring an entity (POST, PUT, ...)Moreover, by using
let
it's possible to extract data from parts of the HTTP response and share it with other fixtures and tests.(RestFixtureLiveDoc)1 has a comprehensive set of examples for the various features implemented by RestFixture, but here follows - for reference - an extract of the main ones.
RestFixtureConfig
(RestFixtureConfig)11 is a simple fixture to set RestFixture configuration parameters. Config parameters can be overridden in named config fixtures and passed on to RestFixture instances. For example
RestFixture operations
The supported HTTP operations are:
GET
,POST
,PUT
,DELETE
,HEAD
,OPTION
,TRACE
. For example:OR
Depending on the information that the author may want to convey expectation cells (3rd, 4th and 5th cell on each row) may be supplied for verification.
Simple fixtures like the examples above may be provided to test and document the service exposed REST api.
Sharing data
Data is shared using
let
to extract from the response.In here, the symbol
name
is set to the value of the json expressionresponse.jsonbody.name
.response
is a json object containing the last HTTP response,jsonbody
is an object in response containing the JSON object in the response body.The symbol can be further used in other fixtures:
or
Transactions
The system can be also test and documented for multiple operations providing an application transaction (in a lose meaning here).
The example above documents a way to use the API as an application transaction.
Extending RestFixture
RestFixture can be extended to provide additional functionality. Two methods are provided:
Simple Javascript code can be embedded in RestFixture via configuration.
The config above - when used - will load the Javascript in the file provided as path and make it available in the context of the expectations handler: