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
Create the
ExternalService
in thesrc/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"; } }
Create a class
UsesExternalService
that usesExternalService
in thesrc/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(); } }
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()); } }
Create the
MockExternalService
in thesrc/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 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:import javax.enterprise.context.ApplicationScoped; import io.quarkus.test.Mock; @Mock @ApplicationScoped public class MockExternalService extends ExternalService { @Override public String service() { return "mock"; } }
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()); } }