Testing Spring-style Applications in Quarkus
Spring developers migrating to Quarkus can keep using @SpringBootTest as they do in Spring Boot — no import change required.
Under the hood, Quarkus treats @SpringBootTest as an alias for @QuarkusTest: it starts the Quarkus runtime and CDI container, not a Spring Application Context.
|
|
Prerequisites
完成这个指南,你需要:
-
大概15分钟
-
编辑器
-
JDK 17+ installed with
JAVA_HOMEconfigured appropriately -
Apache Maven 3.9.16
-
如果你愿意的话,还可以选择使用Quarkus CLI
-
如果你想构建原生可执行程序,可以选择安装Mandrel或者GraalVM,并正确配置(或者使用Docker在容器中进行构建)
Adding the dependency
Add quarkus-test-spring to your project as a test-scoped dependency:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-spring</artifactId>
<scope>test</scope>
</dependency>
testImplementation("io.quarkus:quarkus-test-spring")
Writing a test
Use @SpringBootTest as you would in a Spring Boot project — the import stays the same:
import org.springframework.boot.test.context.SpringBootTest; (1)
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest (2)
public class GreetingServiceTest {
@Autowired (3)
GreetingService greetingService;
@Test
public void testGreeting() {
assertEquals("Hello, Quarkus!", greetingService.greet("Quarkus"));
}
}
| 1 | Keep the original Spring Boot import — no change needed. |
| 2 | Quarkus starts the application and makes CDI beans available for injection. |
| 3 | Use @Autowired if you have quarkus-spring-di on the classpath (or standard CDI @Inject otherwise). |
HTTP endpoint testing
For testing REST endpoints, use RestAssured exactly as you would with @QuarkusTest:
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.is;
@SpringBootTest
public class GreetingControllerTest {
@Test
public void testHelloEndpoint() {
given()
.when().get("/greeting")
.then()
.statusCode(200)
.body(is("Hello, Quarkus!"));
}
}
Native executable testing
To run the same tests against the packaged application (JVM or native mode), create a companion class annotated with @QuarkusIntegrationTest that extends your test:
import io.quarkus.test.junit.QuarkusIntegrationTest;
@QuarkusIntegrationTest
public class GreetingControllerIT extends GreetingControllerTest {
}
Differences from Spring Boot’s @SpringBootTest
@SpringBootTest in Quarkus is intentionally simpler than in Spring Boot. The table below summarises the main differences:
| Feature | Spring Boot | Quarkus |
|---|---|---|
Application context |
Spring Application Context |
Quarkus CDI container (ArC) |
Annotation attributes ( |
Supported |
Not supported — configure via Quarkus configuration and test profiles |
Dependency injection in tests |
|
|
Dev Services (databases, messaging, etc.) |
Testcontainers via Spring Boot autoconfiguration |
Quarkus Dev Services — zero configuration required |
MockBean |
|
|
Test profiles |
Spring profiles via |