Test
The elide-test-helpers package provides a JSON-API and GraphQL type safe DSL that simplifies adding integration tests to your service. The DSLs are designed to work with Rest Assured.
Dependencies
The tests described here are based on a the getting started example repo.
The example leverages:
- Elide Spring Boot Starter for running the test service and setting up Elide.
- JUnit 5 for adding tests.
- elide-test-helpers for the JSON-API and GraphQL DSLs.
- Rest Assured for issuing HTTP requests against the test service.
- Spring Boot Test Starter for adding test data for each test.
- H2 In Memory Database for an in memory test database.
Maven
<dependency>
<groupId>com.paiondata.elide</groupId>
<artifactId>elide-spring-boot-starter</artifactId>
<version>${elide.version}</version>
</dependency>
<dependency>
<groupId>com.paiondata.elide</groupId>
<artifactId>elide-test-helpers</artifactId>
<version>${elide.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
Setup
Using elide with Spring Boot, we can set up a test service for integration tests by having our test classes extend a common test base class like this one:
/**
* Base class for running a set of functional Elide tests. This class
* sets up an Elide instance with an in-memory H2 database.
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
@LocalServerPort
int port;
@BeforeAll
public void setUp() {
RestAssured.port = port;
}
}
JSON-API DSL
Using Rest Assured and the JSON-API DSL, we can issue JSON-API requests and verify responses against our test service. This example uses Spring Boot to initialize the H2 database with a clean set of test records.
@Test
@Sql(statements = {
"DELETE FROM ArtifactVersion; DELETE FROM ArtifactProduct; DELETE FROM ArtifactGroup;",
"INSERT INTO ArtifactGroup (name, commonName, description) VALUES\n" +
"\t\t('com.example.repository','Example Repository','The code for this project');"
})
void jsonApiGetTest() {
when()
.get("/api/v1/group")
.then()
.log().all()
.body(equalTo(
data(
resource(
type( "group"),
id("com.example.repository"),
attributes(
attr("commonName", "Example Repository"),
attr("description", "The code for this project")
),
relationships(
relation("products")
)
)
).toJSON())
)
.log().all()
.statusCode(HttpStatus.SC_OK);
}
The complete set of static DSL operators for JSON-API can be found here.
GraphQL DSL
Using Rest Assured and the GraphQL DSL, we can issue GraphQL requests and verify responses against our test service like this:
@Test
@Sql(statements = {
"DELETE FROM ArtifactVersion; DELETE FROM ArtifactProduct; DELETE FROM ArtifactGroup;",
"INSERT INTO ArtifactGroup (name, commonName, description) VALUES\n" +
"\t\t('com.example.repository','Example Repository','The code for this project');",
"INSERT INTO ArtifactGroup (name, commonName, description) VALUES\n" +
"\t\t('com.paiondata.elide','Elide','The magical library powering this project');"
})
void graphqlTest() {
given()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body("{ \"query\" : \"" + GraphQLDSL.document(
query(
selection(
field("group",
selections(
field("name"),
field("commonName"),
field("description")
)
)
)
)
).toQuery() + "\" }"
)
.when()
.post("/graphql/api/v1")
.then()
.body(equalTo(GraphQLDSL.document(
selection(
field(
"group",
selections(
field("name", "com.example.repository"),
field( "commonName", "Example Repository"),
field("description", "The code for this project")
),
selections(
field("name", "com.paiondata.elide"),
field( "commonName", "Elide"),
field("description", "The magical library powering this project")
)
)
)
).toResponse()))
.statusCode(HttpStatus.SC_OK);
}
The complete set of static DSL operators for GraphQL can be found here.