第3章 GreetingController のメインクラスおよびテストクラスの作成

コマンドラインでプロジェクトを作成すると、Quarkus Maven プラグインは、REST エンドポイントを定義する Spring Web アノテーションを使用した GreetingController クラスファイルおよび GreetingController のユニットテストが含まれるクラスファイルを自動的に生成します。

手順

  1. 以下のコードを含む src/main/java/org/acme/spring/web/GreetingController.java ファイルを作成します。

    src/main/java/org/acme/spring/web/GreetingController.java

    package org.acme.spring.web;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/greeting")
    public class GreetingController {
    
        @GetMapping
        public String hello() {
            return "Hello Spring";
        }
    }

  2. 以下のコードを含む src/test/java/org/acme/spring/web/GreetingControllerTest.java ファイルを作成します。

    src/test/java/org/acme/spring/web/GreetingControllerTest.java

    package org.acme.spring.web;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Test;
    
    import static io.restassured.RestAssured.given;
    import static org.hamcrest.CoreMatchers.is;
    
    @QuarkusTest
    public class GreetingControllerTest {
    
        @Test
        public void testHelloEndpoint() {
            given()
              .when().get("/greeting")
              .then()
                 .statusCode(200)
                 .body(is("Hello Spring"));
        }
    
    }