Class QuarkusMock

java.lang.Object
io.quarkus.test.junit.QuarkusMock

public class QuarkusMock extends Object
Utility class that can be used to mock CDI normal scoped beans.

This includes beans that are ApplicationScoped and RequestScoped.

To use this, inject the bean into a test and invoke installMockForInstance(Object, Object, Options). Alternatively, invoke installMockForType(Object, TypeLiteral, Options, Annotation...).

Mocks installed in BeforeAll will be present for every test, while mocks installed within a test are cleared after the test has run. Note that you will likely need to use TestInstance.Lifecycle.PER_CLASS to have a non-static before all method that can access injected beans.

Note that as the bean is replaced globally, you cannot use parallel test execution, as this will result in race conditions where mocks from one test are active in another.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Allowed options for configuring mocked beans
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> void
    installMockForInstance(T mock, T instance)
    Installs the given mock for a CDI normal scoped bean identified by given instance.
    static <T> void
    installMockForInstance(T mock, T instance, QuarkusMock.Options options)
    Installs the given mock for a CDI normal scoped bean identified by given instance.
    static <T> void
    installMockForType(T mock, jakarta.enterprise.util.TypeLiteral<? super T> type, QuarkusMock.Options options, Annotation... qualifiers)
    Installs the given mock for a CDI normal scoped bean of given type and with given qualifiers.
    static <T> void
    installMockForType(T mock, jakarta.enterprise.util.TypeLiteral<? super T> type, Annotation... qualifiers)
    Installs the given mock for a CDI normal scoped bean of given type and with given qualifiers.
    static <T> void
    installMockForType(T mock, Class<? super T> type, Annotation... qualifiers)
    Installs the given mock for a CDI normal scoped bean of given type and with given qualifiers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • QuarkusMock

      public QuarkusMock()
  • Method Details

    • installMockForInstance

      public static <T> void installMockForInstance(T mock, T instance)
      Installs the given mock for a CDI normal scoped bean identified by given instance. Note that the given mock must be assignable to the bean class of the bean; it is not enough for it to implement only some of the bean types.
      Type Parameters:
      T - The bean type
      Parameters:
      mock - The mock object
      instance - The CDI normal scoped bean that was injected into your test
    • installMockForInstance

      public static <T> void installMockForInstance(T mock, T instance, QuarkusMock.Options options)
      Installs the given mock for a CDI normal scoped bean identified by given instance. Note that the given mock must be assignable to the bean class of the bean; it is not enough for it to implement only some of the bean types.
      Type Parameters:
      T - The bean type
      Parameters:
      mock - The mock object
      instance - The CDI normal scoped bean that was injected into your test
    • installMockForType

      public static <T> void installMockForType(T mock, Class<? super T> type, Annotation... qualifiers)
      Installs the given mock for a CDI normal scoped bean of given type and with given qualifiers. Note that the given mock must be assignable to the bean class of the looked up bean; it is not enough for it to implement only some of the bean types.
      Type Parameters:
      T - The bean type
      Parameters:
      mock - The mock object
      type - The type of the CDI normal scoped bean to replace
      qualifiers - The CDI qualifiers of the bean to mock
    • installMockForType

      public static <T> void installMockForType(T mock, jakarta.enterprise.util.TypeLiteral<? super T> type, Annotation... qualifiers)
      Installs the given mock for a CDI normal scoped bean of given type and with given qualifiers. Note that the given mock must be assignable to the bean class of the looked up bean; it is not enough for it to implement only some of the bean types.
      Type Parameters:
      T - The bean type
      Parameters:
      mock - The mock object
      type - The type of the CDI normal scoped bean to replace
      qualifiers - The CDI qualifiers of the bean to mock
    • installMockForType

      public static <T> void installMockForType(T mock, jakarta.enterprise.util.TypeLiteral<? super T> type, QuarkusMock.Options options, Annotation... qualifiers)
      Installs the given mock for a CDI normal scoped bean of given type and with given qualifiers. Note that the given mock must be assignable to the bean class of the looked up bean; it is not enough for it to implement only some of the bean types.
      Type Parameters:
      T - The bean type
      Parameters:
      mock - The mock object
      type - TypeLiteral representing the required type
      qualifiers - The CDI qualifiers of the bean to mock