background image

Quarkus Test Bootstrap Guide with Maven and RestAssured

by pp
Updated:

To bootstrap a Quarkus test, you can follow these steps:

We are going to write junit test for a class:

package com.example;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello from RESTEasy Reactive";
    }
}

Dependencies:

Ensure that the following dependencies are included in your project’s 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>

If these dependencies are not present, please add them. Refer to pom.xml in attached project’s source files in case you don't know how the result should look like.

Create a Test Class:

Create a test class with the @QuarkusTest annotation. The @QuarkusTest annotation is used to signify that the test is a Quarkus test and controls the lifecycle of the Quarkus test environment. This environment includes starting and stopping the Quarkus application, setting up any necessary resources, and providing dependency injection. Here’s an example of a simple test GreetingResourceTest:

package com.example;

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
class GreetingResourceTest {
    @Test
    void testHelloEndpoint() {
        given()
          .when().get("/hello")
          .then()
             .statusCode(200)
             .body(is("Hello from RESTEasy Reactive"));
    }
}

Here's a breakdown of the code:

  1. @QuarkusTest: This annotation is from the Quarkus testing framework and indicates that this class is a test class that needs Quarkus to set up the testing environment.

  2. @Test: This annotation is from JUnit and indicates that the testHelloEndpoint method is a test case.

  3. given(): This method is part of RestAssured, a library for testing RESTful services. It is used to start building an HTTP request.

  4. .when().get("/hello"): This chain of methods specifies that an HTTP GET request should be made to the "/hello" endpoint.

  5. .then(): This part of the chain is used to define assertions on the response received.

  6. .statusCode(200): Asserts that the HTTP status code of the response is 200 (OK).

  7. .body(is("Hello from RESTEasy Reactive")): Asserts that the body of the response is equal to the specified string, in this case, "Hello from RESTEasy Reactive".

In summary, this test case ensures that when an HTTP GET request is made to the "/hello" endpoint in your Quarkus application, the response has a status code of 200, and the body contains the expected string "Hello from RESTEasy Reactive". This is a basic example of testing a RESTful endpoint using Quarkus and RestAssured.

Run the Test:

You can now run the test using your IDE or from command line. For maven, that would be:

mvn test -Dtest=com.example.GreetingResourceTest

that produces output:

[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 2 source files with javac [debug release 17] to target/test-classes
[INFO] Annotation processing is enabled because one or more processors were found
  on the class path. A future release of javac may disable annotation processing
  unless at least one processor is specified by name (-processor), or a search
  path is specified (--processor-path, --processor-module-path), or annotation
  processing is enabled explicitly (-proc:only, -proc:full).
  Use -Xlint:-options to suppress this message.
  Use -proc:none to disable annotation processing.
[INFO] 
[INFO] --- maven-surefire-plugin:3.2.5:test (default-test) @ hello-world ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.GreetingResourceTest
2024-02-20 21:54:39,402 INFO  [io.quarkus] (main) hello-world 1.0.0-SNAPSHOT on JVM (powered by Quarkus 3.7.2) started in 2.460s. Listening on: http://localhost:8081
2024-02-20 21:54:39,405 INFO  [io.quarkus] (main) Profile test activated. 
2024-02-20 21:54:39,405 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy-reactive, smallrye-context-propagation, vertx]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.556 s -- in com.example.GreetingResourceTest
2024-02-20 21:54:40,755 INFO  [io.quarkus] (main) hello-world stopped in 0.037s
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.339 s
[INFO] Finished at: 2024-02-20T21:54:40+01:00
[INFO] ------------------------------------------------------------------------
petr@zbox-nano:~/QuarkusProjects/hello-world$ 

Wrap Up Tutorial:

Congratulations! You have successfully created and executed a Quarkus test. This tutorial covers the basics of setting up a test environment, creating a simple test class, and running the test using Maven. As you continue developing with Quarkus, you can expand on this foundation to create more comprehensive tests for your applications. Happy coding!