You can set up automated tests for your sapi-nt API by using the sapi-nt-test module.
This module contains a library for generating test cases and a base class for JUnit tests (ApiTest).
A standard test class (SapiNtApiTest) is included in the sapi-nt Maven archetype.
The API test class is disabled by default.
To enable it, set the sapint.api.test.enabled property to true in the src/test/resources/application.properties file of your project.
You must define the test suite for your API in a YAML file located at src/test/resources/api/test.yaml
(this is created automatically by the sapi-nt archetype).
The test suite specifies a set of tests to perform against the API,
as well as actions to perform before and after the test run.
The test suite definition is the root of the test.yaml YAML structure.
It has the following properties:
| Property | Description |
|---|---|
setup |
Test setup configuration (optional). |
tests |
Array of test cases. |
The test setup definition contains predefined options for setting up the test environment.
If you want to run an embedded Fuseki server as part of your test,
you should provide a Fuseki setup definition for the fuseki property.
Otherwise, the sapi-nt application will attempt to connect to the configured data source as usual.
| Property | Description |
|---|---|
fuseki |
Embedded Fuseki setup (optional). |
The Fuseki setup definition contains the options for the embedded Fuseki server, if your test uses one.
| Property | Description | Default |
|---|---|---|
file |
The location of an RDF file containing the test data set. | |
dataset |
The name of the dataset in which to load the RDF data in the given file. |
ds |
port |
The port on which to run the embedded server. | 3030 |
The file location must be inside the src/test/resources directory of your project,
and should be given as a path relative to that directory.
Each test case definition specifies a single request that can be sent to the API, and the expected response.
| Property | Description |
|---|---|
path |
The URL path to request. |
params |
A map of request parameters (key-value pairs). |
format |
The response format to request (JSON by default). |
expect |
The test expectation. |
name |
A user friendly name for the test (optional). |
The path value should only include the part of the path after the context path of the application.
The format value should be the short name of the format, eg. json, csv.
If you do not specify a name for a test case, a suitable name will be generated automatically.
The expectation definition specifies the response that is expected from the API for a given request.
By default, all test cases expect the response to have a 200 OK status code.
| Property | Description |
|---|---|
content |
The expected body of the API response. |
You can provide the content value as a raw string.
Alternatively, you can specify it as a nested object with a file property that gives the location of a file containing the expected response body.
In that case, the file location must be inside the src/test/resources directory of your project,
and should be given as a path relative to that directory.
setup:
fuseki:
file: api/data/underground.ttl
tests:
- path: "/station"
params:
_limit: 10
format: ttl
expect:
content:
file: api/result/station.ttl
If you set up your application from the sapi-nt archetype,
you can run the API tests by setting the sapint.api.test.enabled property to true and running the SapiNtApiTest from your IDE.
You can also run the test with Maven using mvn test.
Alternatively, you can create your own test class by extending the ApiTest class in the sapi-nt-test module.
package com.epimorphics.underground.test
import com.epimorphics.sapint.test.ApiTest
class UndergroundApiTest: ApiTest()