Testing your Quarkus applications
Abstract
Preface Copy linkLink copied to clipboard!
As an application developer, you can use Red Hat build of Quarkus to create microservices-based applications written in Java that run on OpenShift and serverless environments. Applications compiled to native executables have small memory footprints and fast startup times.
This guide shows you how to use Apache Maven to test the Quarkus Getting Started project in JVM mode and how to inject resources into your tests. You will expand the test that you created in Getting started with Quarkus.
Prerequisites
Have OpenJDK (JDK) 11 installed and the
JAVA_HOME
environment variable specify the location of the Java SDK.- Log in the Red Hat Customer Portal to download the Red Hat build of Open JDK from the Software Downloads page.
Have Apache Maven 3.6.3 or higher installed.
- Download Maven from the Apache Maven Project website.
A completed Quarkus Getting Started project.
- To learn how to build the Quarkus Getting Started project, see Getting started with Quarkus.
-
Alternatively, you can download the Quarkus quickstart archive or clone the
Quarkus Quickstarts
Git repository. The example is in thegetting-started
directory.
Making open source more inclusive Copy linkLink copied to clipboard!
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Chapter 1. Verify test dependencies Copy linkLink copied to clipboard!
For this tutorial, you must have a completed Quarkus Getting Started project and the project pom.xml
file must include the quarkus-junit5
and rest-assured
dependencies. These dependencies will be present if you completed the Quarkus Getting Started exercise or if you downloaded the completed example.
-
The
quarkus-junit5
dependency is required for testing because it provides the@QuarkusTest
annotation that controls the testing framework. The
rest-assured
dependency is not required but you can use it as a convenient way to test HTTP endpoints.NoteQuarkus provides integration that automatically sets the correct URL, so no configuration is required.
Procedure
-
Open the Getting Started project
pom.xml
file. Verify that the following dependencies are in the file and add them if necessary:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that your
pom.xml
file includes themaven-surefire-plugin
. Because this tutorial uses the JUnit 5 framework, the version of themaven-surefire-plugin
must be set because the default version does not support Junit 5:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Set the
java.util.logging.manager
system property to use the correct log manager for test. Verify that the
GreetingResourceTest.java
file contains the following content and add it if necessary:Copy to Clipboard Copied! Toggle word wrap Toggle overflow To run the test, enter the following command:
./mvnw clean verify
./mvnw clean verify
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can also run the test directly from your IDE.
This test uses HTTP to directly test the REST endpoint. When the test is triggered, the application will start before the test runs.
Chapter 2. Specifying the test port Copy linkLink copied to clipboard!
By default, Quarkus tests run on port 8081
to avoid conflict with the running application. This allows you to run tests while the application is running in parallel.
Procedure
To specify the port used when you are testing your project, configure the
quarkus.http.test-port
property in the projectapplication.properties
file, where<port>
is the port that you want to test on:quarkus.http.test-port=<port>
quarkus.http.test-port=<port>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can set the port number to
0
to let the your operating system assign a random port form the range of available ports on your system.NoteQuarkus provides REST Assured integration that updates the default port used by REST Assured before the tests are run, so no additional configuration is required.
Chapter 3. Injecting a URL into a test Copy linkLink copied to clipboard!
If you want to use a different client, use the Quarkus @TestHTTPResource
annotation to directly inject the URL of the application to be tested into a field on the test class. This field can be of the type String
, URL
, or URI
. You can also provide the test path in this annotation. In this exercise, you will write a simple test that loads static resources.
Procedure
Create the
src/main/resources/META-INF/resources/index.html
file with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
StaticContentTest.java
file with the following content to test thatindex.html
is being served correctly:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- The
@TestHTTPResource
annotation enables you to directly inject the URL of the Quarkus instance. The value of the annotation is the path component of the URL.
Chapter 4. Injection of CDI beans into tests Copy linkLink copied to clipboard!
You can perform unit testing and test CDI beans directly. Quarkus enables you to inject CDI beans into your tests through the @Inject
annotation. In fact, tests in Quarkus are full CDI beans so you can use the complete CDI functionality.
It is not possible to use injection with native tests.
Procedure
Create the
GreetingServiceTest.java
file with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- The
GreetingService
bean will be injected into the test.
Chapter 5. Applying interceptors to tests Copy linkLink copied to clipboard!
Quarkus tests are full CDI beans, so you can apply CDI interceptors as you would normally. For example, if you want a test method to run within the context of a transaction, you can apply the @Transactional
annotation to the method. You can also create your own test stereotypes.
Procedure
Add the
quarkus-narayana-jta
dependency to yourpom.xml
file:<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-narayana-jta</artifactId> </dependency>
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-narayana-jta</artifactId> </dependency>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Make sure the
TransactionalQuarkusTest.java
includes the following import statements:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
@TransactionalQuarkusTest
annotation:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Apply this annotation to a test class where it will behave as if you applied both the
@QuarkusTest
and@Transactional
annotations:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This is a simple test that evaluates the greeting service directly without using HTTP.
Chapter 6. Mocking CDI beans Copy linkLink copied to clipboard!
Quarkus allows you to mock certain CDI beans for specific tests.
You can mock an object using one of the following methods:
-
Override the bean you that you want to mock with a class in the
src/test/java
directory, and put the@Alternative
and@Priority(1)
annotations on the bean. -
Use the
io.quarkus.test.Mock
stereotype annotation. The@Mock
annotation contains the@Alternative
,@Priority(1)
and@Dependent
annotations.
The following procedure shows how to mock an external service using the @Alternative
annotation.
Procedure
Create the
ExternalService
in thesrc/main/java
directory similar to the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class
UsesExternalService
that usesExternalService
in thesrc/main/java
directory:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a test in the
src/test/java
directory similar to the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
MockExternalService
in thesrc/test/java
that uses the@Alternative
annotation:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- The
MockExternalService
is injected wherever theExternalService
is being used. In this example,MockExternalService
will be used inUsesExternalService
.
NoteYou can use the
@Mock
annotation instead of the@Alternative
,@Priority(1)
and@Dependent
annotations.The following example shows how to create
MockExternalService
class that uses the@Mock
annotation:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change the asserted string from
"external"
to"mock"
in the test:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 7. Additional resources Copy linkLink copied to clipboard!
Revised on 2021-04-19 12:59:23 UTC