Cloud Native, Container First Java framework for building efficient Java applications with fast startup times and low memory usage
—
Quarkus provides comprehensive testing support with JUnit 5 integration, custom testing annotations, mocking capabilities, and test profiles for different testing scenarios including unit tests, integration tests, and native tests.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface QuarkusTest {
}Main annotation for Quarkus unit tests. Starts a Quarkus application in test mode with full CDI container and dependency injection support.
Usage Example:
@QuarkusTest
public class UserServiceTest {
@Inject
UserService userService;
@Test
public void testCreateUser() {
User user = new User("John", "john@example.com");
User created = userService.create(user);
assertNotNull(created.getId());
assertEquals("John", created.getName());
}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface QuarkusIntegrationTest {
}Annotation for integration tests that run against the packaged application (JAR or native executable).
Usage Example:
@QuarkusIntegrationTest
public class UserResourceIT {
@Test
public void testGetUser() {
given()
.when().get("/users/1")
.then()
.statusCode(200)
.body("name", equalTo("John Doe"));
}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface NativeImageTest {
}Runs tests against native executable compiled with GraalVM.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestProfile {
Class<? extends QuarkusTestProfile> value();
}Applies a custom test profile to override configuration for specific test classes.
public interface QuarkusTestProfile {
Map<String, String> getConfigOverrides();
Set<Class<?>> getEnabledAlternatives();
String getConfigProfile();
List<TestResourceEntry> testResources();
boolean disableGlobalTestResources();
Set<String> tags();
class TestResourceEntry {
public TestResourceEntry(Class<? extends QuarkusTestResourceLifecycleManager> clazz);
public TestResourceEntry(Class<? extends QuarkusTestResourceLifecycleManager> clazz,
Map<String, String> args);
}
}Interface for defining custom test profiles with configuration overrides and test resources.
Usage Example:
public class DatabaseTestProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"quarkus.datasource.db-kind", "h2",
"quarkus.datasource.jdbc.url", "jdbc:h2:mem:test",
"quarkus.hibernate-orm.database.generation", "drop-and-create"
);
}
@Override
public String getConfigProfile() {
return "test";
}
}
@QuarkusTest
@TestProfile(DatabaseTestProfile.class)
public class UserRepositoryTest {
// Tests with H2 in-memory database
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(QuarkusTestResource.List.class)
public @interface QuarkusTestResource {
Class<? extends QuarkusTestResourceLifecycleManager> value();
Map<String, String> initArgs() default {};
boolean restrictToAnnotatedClass() default false;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface List {
QuarkusTestResource[] value();
}
}Manages external test resources like databases, message brokers, or containers.
public interface QuarkusTestResourceLifecycleManager {
Map<String, String> start();
void stop();
void inject(TestInjector testInjector);
int order();
interface TestInjector {
void injectIntoFields(Object testInstance,
Map<Class<?>, Function<Class<?>, Object>> valueProducers);
}
}Interface for implementing custom test resource lifecycle management.
Usage Example:
public class PostgreSQLTestResource implements QuarkusTestResourceLifecycleManager {
private PostgreSQLContainer<?> container;
@Override
public Map<String, String> start() {
container = new PostgreSQLContainer<>("postgres:13")
.withDatabaseName("testdb")
.withUsername("test")
.withPassword("test");
container.start();
return Map.of(
"quarkus.datasource.jdbc.url", container.getJdbcUrl(),
"quarkus.datasource.username", container.getUsername(),
"quarkus.datasource.password", container.getPassword()
);
}
@Override
public void stop() {
if (container != null) {
container.stop();
}
}
}
@QuarkusTest
@QuarkusTestResource(PostgreSQLTestResource.class)
public class UserRepositoryIT {
// Tests with real PostgreSQL container
}public class QuarkusMock {
public static <T> void installMockForType(T mock, Class<T> type, Annotation... qualifiers);
public static <T> void installMockForInstance(T mock, T instance);
public static void removeMockForType(Class<?> type, Annotation... qualifiers);
public static void removeMockForInstance(Object instance);
}Utility class for installing and managing mocks in Quarkus tests.
Usage Example:
@QuarkusTest
public class OrderServiceTest {
@Mock
PaymentService paymentService;
@BeforeEach
void setup() {
QuarkusMock.installMockForType(paymentService, PaymentService.class);
}
@Test
public void testCreateOrder() {
when(paymentService.charge(any(), any())).thenReturn(true);
Order order = new Order("Product", 100.0);
Order result = orderService.create(order);
assertNotNull(result);
verify(paymentService).charge(eq(100.0), any());
}
}@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectMock {
}Automatically creates and injects mocks for CDI beans.
Usage Example:
@QuarkusTest
public class NotificationServiceTest {
@InjectMock
EmailService emailService;
@Inject
NotificationService notificationService;
@Test
public void testSendNotification() {
notificationService.sendWelcome("user@example.com");
verify(emailService).send(eq("user@example.com"), contains("Welcome"));
}
}@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DisabledOnIntegrationTest {
}Disables tests when running as integration tests.
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EnabledOnIntegrationTest {
}Enables tests only when running as integration tests.
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DisabledOnNativeImage {
}Disables tests when running against native image.
Usage Example:
@QuarkusTest
public class PerformanceTest {
@Test
@DisabledOnNativeImage // Skip slow test in native mode
public void testComplexOperation() {
// CPU-intensive test
}
@Test
@EnabledOnIntegrationTest // Only run in integration mode
public void testEndToEndWorkflow() {
// Full integration test
}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestHTTPEndpoint {
Class<?> value();
}Configures the base URI for REST Assured tests based on a JAX-RS resource class.
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestHTTPResource {
String value() default "";
}Injects the URL of HTTP resources for testing.
Usage Example:
@QuarkusTest
@TestHTTPEndpoint(UserResource.class)
public class UserResourceTest {
@TestHTTPResource
URL baseUrl;
@TestHTTPResource("/{id}")
URL userUrl;
@Test
public void testGetUser() {
given()
.pathParam("id", 1)
.when().get()
.then()
.statusCode(200)
.body("name", notNullValue());
}
}@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestSecurity {
String user() default "";
String[] roles() default {};
String authorizationHeader() default "";
}Configures test security context with specified user and roles.
Usage Example:
@QuarkusTest
public class SecureResourceTest {
@Test
@TestSecurity(user = "admin", roles = {"admin", "user"})
public void testAdminEndpoint() {
given()
.when().get("/admin/users")
.then()
.statusCode(200);
}
@Test
@TestSecurity(user = "user", roles = {"user"})
public void testUserEndpoint() {
given()
.when().get("/admin/users")
.then()
.statusCode(403); // Forbidden
}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TransactionalQuarkusTest {
}Wraps each test method in a transaction that is rolled back after the test.
Usage Example:
@TransactionalQuarkusTest
public class UserRepositoryTest {
@Inject
UserRepository repository;
@Test
public void testCreateUser() {
User user = new User("John", "john@example.com");
repository.persist(user);
// Transaction will be rolled back after test
assertNotNull(user.getId());
}
}@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ContinuousTest {
}Marks tests to run continuously during development mode.
# Enable continuous testing
quarkus.test.continuous-testing=enabled
# Include/exclude test tags
quarkus.test.include-tags=unit,integration
quarkus.test.exclude-tags=slow
# Disable broken tests
quarkus.test.disable-console-input=trueConfiguration options for continuous testing behavior.
Install with Tessl CLI
npx tessl i tessl/maven-io-quarkus--quarkus-bom