이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 7. 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. Note, that this approach does not work with native image testing because the native image does not include the test alternatives.

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";
        }
    
    }
  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();
        }
    }
  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());
        }
    }
  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";
        }
    }
    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";
        }
    }
  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());
        }
    }
Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.