Common test utilities and framework for Quarkus applications, providing core testing infrastructure including application launchers, test isolation, configuration management, and integration with REST Assured for HTTP testing
—
Specialized support for HTTP endpoint testing with automatic URL injection and endpoint configuration. Integrates seamlessly with REST Assured and other HTTP testing libraries, providing convenient access to test server URLs and endpoints.
Injects HTTP resources configured for test URLs, supporting various protocols and management interfaces.
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestHTTPResource {
String value() default "";
@Deprecated(since = "3.10", forRemoval = true)
boolean ssl() default false;
boolean tls() default false;
boolean management() default false;
}Basic Usage:
public class UserEndpointTest {
@TestHTTPResource
URL baseUrl;
@TestHTTPResource("/api/users")
URL usersEndpoint;
@TestHTTPResource(value = "/admin", management = true)
URL adminEndpoint;
@TestHTTPResource(value = "/secure", tls = true)
URL secureEndpoint;
@Test
public void testUserCreation() {
given()
.baseUri(usersEndpoint.toString())
.contentType(ContentType.JSON)
.body(new User("John", "john@example.com"))
.when()
.post()
.then()
.statusCode(201);
}
}Specifies the endpoint being tested for automatic URL configuration and path resolution.
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestHTTPEndpoint {
Class<?> value();
}Usage with JAX-RS Resources:
@Path("/api/users")
public class UserResource {
@GET
public List<User> getUsers() { /* implementation */ }
@POST
public Response createUser(User user) { /* implementation */ }
}
@TestHTTPEndpoint(UserResource.class)
public class UserResourceTest {
@TestHTTPResource
URL userResourceUrl; // Automatically configured to /api/users
@TestHTTPResource("/search")
URL searchUrl; // Relative to UserResource path: /api/users/search
@Test
public void testGetUsers() {
given()
.baseUri(userResourceUrl.toString())
.when()
.get()
.then()
.statusCode(200)
.contentType(ContentType.JSON);
}
}SPI for providing custom HTTP resource types beyond the built-in URL, URI, and String providers.
public interface TestHTTPResourceProvider<T> {
Class<T> getProvidedType();
T provide(String testUri, Field field);
}Custom Provider Implementation:
public class RestAssuredRequestSpecProvider implements TestHTTPResourceProvider<RequestSpecification> {
@Override
public Class<RequestSpecification> getProvidedType() {
return RequestSpecification.class;
}
@Override
public RequestSpecification provide(String testUri, Field field) {
return new RequestSpecBuilder()
.setBaseUri(testUri)
.setContentType(ContentType.JSON)
.build();
}
}
// Usage in test:
public class APITest {
@TestHTTPResource
RequestSpecification requestSpec; // Automatically configured
@Test
public void testWithCustomSpec() {
requestSpec
.body(testData)
.when()
.post("/api/endpoint")
.then()
.statusCode(200);
}
}The framework includes several built-in providers for common HTTP resource types:
public class StringTestHTTPResourceProvider implements TestHTTPResourceProvider<String> {
@Override
public Class<String> getProvidedType() { return String.class; }
@Override
public String provide(String testUri, Field field) { return testUri; }
}public class URLTestHTTPResourceProvider implements TestHTTPResourceProvider<URL> {
@Override
public Class<URL> getProvidedType() { return URL.class; }
@Override
public URL provide(String testUri, Field field) {
try {
return new URL(testUri);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
}public class URITestHTTPResourceProvider implements TestHTTPResourceProvider<URI> {
@Override
public Class<URI> getProvidedType() { return URI.class; }
@Override
public URI provide(String testUri, Field field) {
return URI.create(testUri);
}
}Configure resources for different protocols and security settings:
public class SecurityTest {
@TestHTTPResource(value = "/public", tls = false)
URL publicEndpoint;
@TestHTTPResource(value = "/secure", tls = true)
URL secureEndpoint;
@TestHTTPResource(value = "/health", management = true)
URL managementEndpoint;
@Test
public void testSecureAccess() {
// Test HTTPS endpoint
given()
.baseUri(secureEndpoint.toString())
.relaxedHTTPSValidation()
.when()
.get()
.then()
.statusCode(200);
}
}Utility class for managing RestAssured configuration in Quarkus tests, providing automatic URL setup, protocol management, and timeout configuration.
public class RestAssuredURLManager {
public static void setURL(boolean useSecureConnection) {}
public static void setURL(boolean useSecureConnection, String additionalPath) {}
public static void setURL(boolean useSecureConnection, Integer port) {}
public static void setURL(boolean useSecureConnection, Integer port, String additionalPath) {}
public static void clearURL() {}
}Usage Examples:
public class RestAssuredConfigTest {
@BeforeEach
public void setup() {
// Configure RestAssured for HTTP testing
RestAssuredURLManager.setURL(false);
}
@AfterEach
public void cleanup() {
// Clear RestAssured configuration
RestAssuredURLManager.clearURL();
}
@Test
public void testWithCustomPath() {
// Configure with additional path
RestAssuredURLManager.setURL(false, "/api/v1");
given()
.when()
.get("/users")
.then()
.statusCode(200);
}
@Test
public void testWithSSL() {
// Configure for HTTPS testing
RestAssuredURLManager.setURL(true);
given()
.relaxedHTTPSValidation()
.when()
.get("/secure-endpoint")
.then()
.statusCode(200);
}
@Test
public void testWithCustomPort() {
// Configure with specific port and path
RestAssuredURLManager.setURL(false, 9090, "/admin");
given()
.when()
.get("/status")
.then()
.statusCode(200);
}
}Test HTTP configuration is provided through specialized configuration classes that automatically setup test URLs and properties.
public class TestHTTPConfigSourceProvider implements ConfigSourceProvider {
public static final String TEST_URL_KEY = "test.url";
public static final String TEST_URL_SSL_KEY = "test.url.ssl";
public static final String TEST_MANAGEMENT_URL_KEY = "test.management.url";
public static final String TEST_MANAGEMENT_URL_SSL_KEY = "test.management.url.ssl";
public static final String HTTP_ROOT_PATH_KEY = "quarkus.http.root-path";
public static final String MANAGEMENT_ROOT_PATH_KEY = "quarkus.http.management-path";
public Iterable<ConfigSource> getConfigSources(ClassLoader forClassLoader) {}
}
public class TestHTTPConfigSourceInterceptor extends ExpressionConfigSourceInterceptor {
public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) {}
}Usage with Configuration:
public class ConfigAwareTest {
@ConfigProperty(name = "test.url")
String testUrl;
@ConfigProperty(name = "test.url.ssl")
Optional<String> testSslUrl;
@ConfigProperty(name = "test.management.url")
Optional<String> managementUrl;
@Test
public void testConfigProperties() {
// The test URLs are automatically configured
assertNotNull(testUrl);
assertTrue(testUrl.startsWith("http://"));
// Management URL is available if management interface is enabled
managementUrl.ifPresent(url -> {
assertTrue(url.contains("management"));
});
}
@Test
public void testSSLConfiguration() {
// SSL URLs are provided when TLS is enabled
testSslUrl.ifPresent(url -> {
assertTrue(url.startsWith("https://"));
});
}
}Common patterns for REST Assured integration:
public class RestAssuredTest {
@TestHTTPResource
URL baseUrl;
@BeforeEach
public void setup() {
RestAssured.baseURI = baseUrl.toString();
RestAssured.defaultParser = Parser.JSON;
}
@Test
public void testCRUDOperations() {
// Create
String userId = given()
.contentType(ContentType.JSON)
.body(new User("Alice", "alice@example.com"))
.when()
.post("/api/users")
.then()
.statusCode(201)
.extract()
.path("id");
// Read
given()
.when()
.get("/api/users/{id}", userId)
.then()
.statusCode(200)
.body("name", equalTo("Alice"));
// Update
given()
.contentType(ContentType.JSON)
.body(Map.of("name", "Alice Updated"))
.when()
.put("/api/users/{id}", userId)
.then()
.statusCode(200);
// Delete
given()
.when()
.delete("/api/users/{id}", userId)
.then()
.statusCode(204);
}
}Testing WebSocket and Server-Sent Events endpoints:
public class WebSocketTest {
@TestHTTPResource("/websocket")
URI websocketUri;
@TestHTTPResource("/events")
URL eventsUrl;
@Test
public void testWebSocketConnection() {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
Session session = container.connectToServer(
new MyWebSocketClient(),
websocketUri
);
// Test WebSocket communication
session.getBasicRemote().sendText("Hello");
// Verify response...
}
@Test
public void testServerSentEvents() {
given()
.accept("text/event-stream")
.when()
.get(eventsUrl.toString())
.then()
.statusCode(200)
.contentType("text/event-stream");
}
}Combining HTTP testing with external services:
@QuarkusTestResource(DatabaseTestResource.class)
public class IntegrationTest {
@TestHTTPResource("/api/users")
URL usersApi;
@Test
public void testUserPersistence() {
// Create user via HTTP API
String userId = given()
.baseUri(usersApi.toString())
.contentType(ContentType.JSON)
.body(new User("Bob", "bob@example.com"))
.when()
.post()
.then()
.statusCode(201)
.extract()
.path("id");
// Verify user exists via API
given()
.baseUri(usersApi.toString())
.when()
.get("/{id}", userId)
.then()
.statusCode(200)
.body("email", equalTo("bob@example.com"));
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-quarkus--quarkus-test-common