This is a project which explores the modelling and code generation functionality of Xtext, resulting in Eclipse DSL plugin which generates JUnit tests, as HTTP calls for testing REST API's.
The DSL makes it possible to write this and get this, to e.g. test this.
- Eclipse IDE for Java and DSL Developers (download link).
- Clone the repo
- Open Eclipse and select the folder as workspace and use
Import projects...
in the Package Explorer, then selectGeneral/Existing Projects into Workspace
to import the modules. - Run this file
./xtext.http.rest.testsuite/src/xtext/http/rest/TestSuite.xtext
withGenerate Xtext Artifacts
(Proceed with errors), this will build the project an generate missing files. - Select the root project
xtext.http.rest.testsuite
and use the run -> run as -> Eclipse application, this will open a new instance of Eclipse with the DSL plugin installed. - Use the new instance and import
./xtext.http.rest.testsuite.example
- Open
./src/CompleteExample.testsuite
this is an example of the features implemented in the DSL - When you edit and save this file, a java file will be generated
./src-gen/CompleteExample.java
containing JUnit tests
If there are missing packages, follow these steps: - Add JUnit 5 library to the project with: Project -> Properties -> Java Build Path -> Libraries -> Add Library
- Add all external jars from
./externalJars
with: Project -> Properties -> Java Build Path -> Libraries -> Add External JARS - Run
.src-get/CompleteExample.java
asJUnit 5 test
, this will run the generated JUnit tests
For the JUnit tests to succeed, there have to be a REST API which responds to the request. For the CompleteExample
a mock API can be found here.
- xtext.http.rest.testsuite: Implementation of the DSL
- xtext.http.rest.testsuite.example: Example usage of the DSL
- xtext.http.rest.testsuite.ide: IDE related code generated by Eclipse
- xtext.http.rest.testsuite.test: JUnit tests for the DSL implementation
- JUnit tests here
- xtext.http.rest.testsuite.ui: UI related code
- Quick fixes here
- xtext.http.rest.testsuite.ui.tests: UI test related code generated by Eclipse
- Global Resources: Define a resource once, use it in multiple tests
- Global Headers: Define a header once, use it in multiple tests
- Headers included in every test: Globally defined headers which will be present in every request
- Define multiple tests, where each test consist of:
- Environment: Define variables, objects or headers to be used in the test
- GET/POST/PUT/DELETE requests
- Add json body to the request
- Inject variables into the resource url
- Save values from the response body to a variable
- Assertions of test response
- Status code
- Body, with possibility to inject an variable into the body
- Text
- Headers
For more details of what's possible see the example.
An overview of the DSL's data model, generated with PlantUML: