JUnit 5 testing extension for Quarkus applications providing comprehensive test support including CDI injection, mocking, and integration testing capabilities
—
The core testing capabilities provide the primary annotations for different testing modes in Quarkus applications.
The @QuarkusTest annotation enables full CDI support in unit tests, running within the same JVM as the test with complete application context.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(QuarkusTestExtension.class)
@Tag("io.quarkus.test.junit.QuarkusTest")
public @interface QuarkusTest {
}import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@QuarkusTest
class ProductServiceTest {
@Inject
ProductService productService;
@Inject
EntityManager entityManager;
@Test
void testCreateProduct() {
Product product = productService.createProduct("Laptop", 999.99);
assertNotNull(product.getId());
assertEquals("Laptop", product.getName());
}
@Test
@Transactional
void testProductPersistence() {
Product product = new Product("Phone", 599.99);
entityManager.persist(product);
entityManager.flush();
Product found = entityManager.find(Product.class, product.getId());
assertEquals("Phone", found.getName());
}
}Key Features:
@Inject@Transactional@ConfigPropertyQuarkusMockThe @QuarkusIntegrationTest annotation runs tests against built application artifacts (JAR files, native images, or containers) in separate processes.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith({DisabledOnIntegrationTestCondition.class, QuarkusIntegrationTestExtension.class})
public @interface QuarkusIntegrationTest {
/**
* @deprecated Use DevServicesContext instead
*/
@Deprecated
interface Context extends DevServicesContext {
}
}import io.quarkus.test.junit.QuarkusIntegrationTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
@QuarkusIntegrationTest
class ProductEndpointIT {
@Test
void testGetProducts() {
given()
.when().get("/api/products")
.then()
.statusCode(200)
.body("size()", is(0));
}
@Test
void testCreateProduct() {
given()
.contentType("application/json")
.body("{\"name\":\"Tablet\",\"price\":299.99}")
.when().post("/api/products")
.then()
.statusCode(201);
}
@Test
void testHealthCheck() {
given()
.when().get("/q/health")
.then()
.statusCode(200)
.body("status", is("UP"));
}
}Key Features:
The framework automatically detects and launches the appropriate artifact type:
target/[app-name]-runner existsjava -jar target/quarkus-app/quarkus-run.jar@QuarkusTest: Tests share application instance (faster, but less isolation)@QuarkusIntegrationTest: Each test class gets fresh application instance (slower, but better isolation)Common exceptions and error scenarios:
// Configuration errors
@ConfigProperty(name = "nonexistent.property")
String value; // Throws DeploymentException if property not found
// CDI injection errors
@Inject
NonExistentBean bean; // Throws UnsatisfiedResolutionException
// Test resource errors
@QuarkusTestResource(DatabaseResource.class)
class MyTest {
// Throws TestResourceException if resource fails to start
}// Base test class with @QuarkusTest
@QuarkusTest
class UserServiceTest {
// Unit test methods
}
// Integration test extends base class
@QuarkusIntegrationTest
class UserServiceIT extends UserServiceTest {
// Same test methods run against built artifact
}@QuarkusTest
class DatabaseTest {
@TestTransaction // Automatic rollback
@Test
void testDataModification() {
// Changes are rolled back after test
}
@Test
void testReadOnlyOperation() {
// No transaction needed for read-only tests
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-quarkus--quarkus-junit5