Chapter 3. Creating the main class and test class for GreetingController


When you create your project on the command line, the Quarkus Maven plugin automatically generates the GreetingController class file with Spring Web annotations that defines the REST endpoint and a class file that contains the unit test for GreetingController.

Procedure

  1. Create the src/main/java/org/acme/spring/web/GreetingController.java file that contains the following code.

    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. Create the src/test/java/org/acme/spring/web/GreetingControllerTest.java file that contains the following code.

    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"));
        }
    
    }

Red Hat logoGithubredditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2026 Red Hat
Back to top