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/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. Note, that this approach does not work with native image testing because the native image does not include the test alternatives.
Procedure
Create the
ExternalServicein thesrc/main/javadirectory 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
UsesExternalServicethat usesExternalServicein thesrc/main/javadirectory: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/javadirectory 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
MockExternalServicein thesrc/test/javathat uses the@Alternativeannotation: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
MockExternalServiceis injected wherever theExternalServiceis being used. In this example,MockExternalServicewill be used inUsesExternalService.
NoteYou can use the
@Mockannotation instead of the@Alternative,@Priority(1)and@Dependentannotations.The following example shows how to create
MockExternalServiceclass that uses the@Mockannotation: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()); } }