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:
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.
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
Set the version of the Surefire Maven plug-in:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The default Surefire Maven plugin version does not support JUnit 5.
-
Set the
java.util.loggingsystem property to make sure tests use the correct log manager. Edit the
src/test/java/org/acme/quickstart/GreetingResourceTest.javafile to match the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteBy using the
QuarkusTestrunner, you instruct JUnit to start the application before starting the tests.To run these tests from Maven, enter the following command:
./mvnw test
./mvnw testCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou 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
8081so they do not conflict with the running application. In Quarkus, theRestAssureddependency is configured to use this port. If you want to use a different client, use the@TestHTTPResourceannotation to directly inject the URL of the tested application into a field on theTestclass. This field can be of the typestring,URLorURI. 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;@TestHTTPResource("/myservlet") URL testUrl;Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
If necessary, specify the test port in the
quarkus.http.test-portconfiguration property.
Quarkus also creates a system property called test.url that is set to the base test URL for situations where you cannot use injection.