이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 6. Testing your Quarkus application with JUnit


After you compile your Quarkus Getting Started project, test your application with the JUnit 5 framework to ensure that it runs as expected. There are two test dependencies in the Quarkus project generated pom.xml file:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-junit5</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <scope>test</scope>
</dependency>
Copy to Clipboard Toggle word wrap

The quarkus-junit5 dependency is required for testing because it provides the @QuarkusTest annotation that controls the JUnit 5 testing framework. The rest-assured dependency is not required but because it provides a convenient way to test HTTP endpoints, it is integrated as well. It automatically sets the correct URL so no configuration is required.

Note

These tests use the REST-assured framework, but you can use a different library if you prefer.

Prerequisites

  • You have compiled the Quarkus Getting Started project.

Procedure

  1. Open the generated pom.xml file and review the contents:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${surefire-plugin.version}</version>
        <configuration>
           <systemPropertyVariables>
             <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
             <maven.home>${maven.home}</maven.home>
           </systemPropertyVariable>
        </configuration>
    </plugin>
    Copy to Clipboard Toggle word wrap

    Note, that:

    • the java.util.logging.manager system property is set to ensure that you application uses the correct log manager for the test.
    • the maven.home property points to the location of the settings.xml file in which you can store custom Maven configuration that you want to apply to your project.
  2. Edit the src/test/java/org/acme/quickstart/GreetingResourceTest.java file to match the following content:

    package org.acme.quickstart;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Test;
    
    import java.util.UUID;
    
    import static io.restassured.RestAssured.given;
    import static org.hamcrest.CoreMatchers.is;
    
    @QuarkusTest
    public class GreetingResourceTest {
    
        @Test
        public void testHelloEndpoint() {
            given()
              .when().get("/hello")
              .then()
                 .statusCode(200)
                 .body(is("hello"));
        }
    
        @Test
        public void testGreetingEndpoint() {
            String uuid = UUID.randomUUID().toString();
            given()
              .pathParam("name", uuid)
              .when().get("/hello/greeting/{name}")
              .then()
                .statusCode(200)
                .body(is("hello " + uuid));
        }
    
    }
    Copy to Clipboard Toggle word wrap
    Note

    By using the QuarkusTest runner, you instruct JUnit to start the application before starting the tests.

  3. To run these tests from Maven, enter the following command:

    ./mvnw test
    Copy to Clipboard Toggle word wrap
    Note

    You can also run the test from your IDE. If you do this, make sure to stop the application first.

    By default, tests run on port 8081 so they do not conflict with the running application. In Quarkus, the RestAssured dependency is configured to use this port. If you want to use a different client, use the @TestHTTPResource annotation to directly inject the URL of the tested application into a field on the Test class. This field can be of the type String, URL or URI. You can also provide the test path in this annotation. For example, to test a servlet mapped to /myservlet, add the following lines to your test:

    @TestHTTPResource("/myservlet")
    URL testUrl;
    Copy to Clipboard Toggle word wrap
  4. If necessary, specify the test port in the quarkus.http.test-port configuration property.
Note

Quarkus also creates a system property called test.url that is set to the base test URL for situations where you cannot use injection.

Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat
맨 위로 이동