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.
The tests described here are based on a the getting started example repo.
The example leverages:
<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-spring-boot-starter</artifactId>
<version>${elide.version}</version>
</dependency>
<dependency>
<groupId>com.yahoo.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>
Using elide with Spring Boot, you can setup a test service for integration tests by having your 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.
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class IntegrationTest {
@LocalServerPort
int port;
@BeforeAll
public void setUp() {
RestAssured.port = port;
}
}
Using Rest Assured and the JSON-API DSL, you can issue JSON-API requests and verify responses against your 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.
Using Rest Assured and the GraphQL DSL, you can issue GraphQL requests and verify responses against your 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.yahoo.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.yahoo.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.