Este contenido no está disponible en el idioma seleccionado.

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/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

  1. Create the ExternalService in the src/main/java directory similar to the following example:

    package org.acme.quickstart;
    
    import javax.enterprise.context.ApplicationScoped;
    
    @ApplicationScoped
    public class ExternalService {
    
        public String service() {
            return "external";
        }
    
    }
    Copy to Clipboard Toggle word wrap
  2. Create a class UsesExternalService that uses ExternalService in the src/main/java directory:

    package org.acme.quickstart;
    
    import javax.enterprise.context.ApplicationScoped;
    import javax.inject.Inject;
    
    @ApplicationScoped
    public class UsesExternalService {
    
        @Inject
        ExternalService externalService;
    
        public String doSomething() {
            return externalService.service();
        }
    }
    Copy to Clipboard Toggle word wrap
  3. Create a test in the src/test/java directory similar to the following example:

    package org.acme.quickstart;
    
    import javax.inject.Inject;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.Test;
    
    @QuarkusTest
    class UsesExternalServiceTest {
    
        @Inject
        UsesExternalService usesExternalService;
    
        @Test
        public void testDoSomething() {
            Assertions.assertEquals("external", usesExternalService.doSomething());
        }
    }
    Copy to Clipboard Toggle word wrap
  4. Create the MockExternalService in the src/test/java that uses the @Alternative annotation:

    package org.acme.quickstart;
    
    import javax.annotation.Priority;
    import javax.enterprise.context.ApplicationScoped;
    import javax.enterprise.inject.Alternative;
    
    @Alternative
    @Priority(1)
    @ApplicationScoped
    public class MockExternalService extends ExternalService { 
    1
    
    
        @Override
        public String service() {
            return "mock";
        }
    }
    Copy to Clipboard Toggle word wrap
    1
    The MockExternalService is injected wherever the ExternalService is being used. In this example, MockExternalService will be used in UsesExternalService.
    Note

    You 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:

    import javax.enterprise.context.ApplicationScoped;
    
    import io.quarkus.test.Mock;
    
    @Mock
    @ApplicationScoped
    public class MockExternalService extends ExternalService {
    
        @Override
        public String service() {
            return "mock";
        }
    }
    Copy to Clipboard Toggle word wrap
  5. Change the asserted string from "external" to "mock" in the test:

    package org.acme.quickstart;
    
    import javax.inject.Inject;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.Test;
    
    @QuarkusTest
    class UsesExternalServiceTest {
    
        @Inject
        UsesExternalService usesExternalService;
    
        @Test
        public void testDoSomething() {
            Assertions.assertEquals("mock", usesExternalService.doSomething());
        }
    }
    Copy to Clipboard Toggle word wrap
Volver arriba
Red Hat logoGithubredditYoutubeTwitter

Aprender

Pruebe, compre y venda

Comunidades

Acerca de la documentación de Red Hat

Ayudamos a los usuarios de Red Hat a innovar y alcanzar sus objetivos con nuestros productos y servicios con contenido en el que pueden confiar. Explore nuestras recientes actualizaciones.

Hacer que el código abierto sea más inclusivo

Red Hat se compromete a reemplazar el lenguaje problemático en nuestro código, documentación y propiedades web. Para más detalles, consulte el Blog de Red Hat.

Acerca de Red Hat

Ofrecemos soluciones reforzadas que facilitan a las empresas trabajar en plataformas y entornos, desde el centro de datos central hasta el perímetro de la red.

Theme

© 2025 Red Hat