이 콘텐츠는 선택한 언어로 제공되지 않습니다.
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
Open the generated
pom.xmlfile and review the contents:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note, that:
-
the
java.util.logging.managersystem property is set to ensure that you application uses the correct log manager for the test. -
the
maven.homeproperty points to the location of thesettings.xmlfile in which you can store custom Maven configuration that you want to apply to your project.
-
the
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.