Testing your Quarkus applications
Abstract
Preface
As an application developer, you can use Red Hat build of Quarkus to create microservices-based applications written in Java that run in serverless and OpenShift environments. These applications have small memory footprints and fast start-up 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
- 
					OpenJDK (JDK) 11 is installed and the JAVA_HOMEenvironment variable specifies the location of the Java SDK. Red Hat build of Open JDK is available from the Software Downloads page in the Red Hat Customer Portal (login required).
- Apache Maven 3.6.2 or higher is installed. Maven is available from the Apache Maven Project website.
- A completed Quarkus Getting Started project is available. - For instructions on building the Quarkus Getting Started project, see Getting started with Quarkus.
- 
							For a completed example of a Quarkus Maven project to use in this tutorial, download the Quarkus quickstart archive or clone the Quarkus QuickstartsGit repository. The example is in thegetting-starteddirectory.
 
Chapter 1. Verify test dependencies
			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-junit5dependency is required for testing because it provides the@QuarkusTestannotation that controls the testing framework.
- The - rest-assureddependency is not required but you can use it as a convenient way to test HTTP endpoints.Note- Quarkus provides integration that automatically sets the correct URL, so no configuration is required. 
Procedure
- 
					Open the Getting Started project pom.xmlfile.
- 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.xmlfile includes the- maven-surefire-plugin. Because this tutorial uses the JUnit 5 framework, the version of the- maven-surefire-pluginmust 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.managersystem property to use the correct log manager for test.
- Verify that the - GreetingResourceTest.javafile 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
			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-portproperty in the project- application.propertiesfile, 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 Note- Quarkus provides RestAssured integration that updates the default port used by RestAssured before the tests are run, so no additional configuration is required. 
Chapter 3. Injecting a URL into a test
			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.htmlfile with the following content:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Create the - StaticContentTest.javafile with the following content to test that- index.htmlis being served correctly:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - 1
- The@TestHTTPResourceannotation 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
			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.javafile with the following content:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - 1
- TheGreetingServicebean will be injected into the test.
 
Chapter 5. Applying interceptors to tests
			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-jtadependency to your- pom.xmlfile:- <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.javaincludes the following import statements:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Create the - @TransactionalQuarkusTestannotation:- 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 - @QuarkusTestand- @Transactionalannotations:- 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
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/javadirectory, and put the@Alternativeand@Priority(1)annotations on the bean.
- 
					Use the io.quarkus.test.Mockstereotype annotation. The@Mockannotation contains the@Alternative,@Priority(1)and@Dependentannotations.
			The following procedure shows how to mock an external service using the @Alternative annotation.
		
Procedure
- Create the - ExternalServicein the- src/main/javadirectory similar to the following example:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Create a class - UsesExternalServicethat uses- ExternalServicein the- src/main/javadirectory:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Create a test in the - src/test/javadirectory similar to the following example:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
- Create the - MockExternalServicein the- src/test/javathat uses the- @Alternativeannotation:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - 1
- TheMockExternalServiceis injected wherever theExternalServiceis being used. In this example,MockExternalServicewill be used inUsesExternalService.
 Note- You can use the - @Mockannotation instead of the- @Alternative,- @Priority(1)and- @Dependentannotations.- The following example shows how to create - MockExternalServiceclass that uses the- @Mockannotation:- 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
- For information about creating Quarkus applications with Maven, see Developing and compiling your Quarkus applications with Apache Maven.
- For information about deploying Quarkus Maven applications on Red Hat OpenShift Container Platform, see Deploying your Quarkus applications on Red Hat OpenShift Container Platform.
- For more information about the Maven Surefire plug-in, see the Apache Maven Project website.
- For information about the JUnit 5 testing framework, see the JUnit 5 website.
- For information about REST-assured, see the REST-assured website.
Revised on 2020-12-08 20:09:53 UTC