Chapter 1. Test your Red Hat build of Quarkus application with JUnit
After you compile your Quarkus Getting Started project, you can verify that it runs as expected by testing your application with the JUnit 6 framework.
Alternatively, you can enable continuous testing of your Quarkus application. For more information, see Enabling and running continuous testing.
The Quarkus project generates the following two test dependencies in the pom.xml file:
-
quarkus-junit: Required for testing because it provides the@QuarkusTestannotation that controls the JUnit testing framework. -
rest-assured: Therest-assureddependency is not required but, because it provides a convenient way to test HTTP endpoints, it is integrated. It automatically sets the correct URL, so no configuration is required.
Example pom.xml file:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
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. For more information, see Build your first Quarkus application.
Procedure
Open the generated
pom.xmlfile and review the contents.The Maven Surefire plugin,
maven-surefire-plugin, runs unit tests during thetestphase of the build lifecycle when Quarkus enables thetestprofile. The plugin generates text and XML test reports.<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <argLine>@{argLine}</argLine> <systemPropertyVariables> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> </configuration> </plugin>Note the values of the following properties:
-
The
java.util.logging.managersystem property is set to ensure that your application uses the correct log manager for the test. -
The
maven.homeproperty is automatically replaced by the location of the Maven home directory.
-
The
Edit the
src/test/java/org/acme/quickstart/GreetingResourceTest.javafile 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 from Quarkus REST")); } @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)); } }NoteBy using the
QuarkusTestrunner, you instruct JUnit to start the application before starting the tests.Run the tests in one of the following ways:
Using Apache Maven:
mvn testUsing Quarkus CLI:
quarkus testUsing the Maven wrapper:
./mvnw testBy default, tests run on port
8081so they do not conflict with the running application. In Quarkus, theRestAssureddependency is configured to use this port.NoteIf you want to use a different client, use the
@TestHTTPResourceannotation to directly inject the URL of the tested application into a field in theTestclass. This field can be of typeString,URL, orURI. You can also enter the test path in the@TestHTTPResourceannotation. For example, to test a resource exposed to/foo, add the following lines to your test:@TestHTTPResource("/foo") URL testUrl;
-
If necessary, specify the test port in the
quarkus.http.test-portconfiguration property.