If you don't need to run the tests in native mode, mocking the configuration with Mockito is a good option.
Let's assume your Quarkus config looks like this:
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
@ConfigMapping(prefix = "features")
interface FeaturesConfig {
@WithDefault("false")
boolean awesomeFeatureEnabled();
}
and it is injected in a class like that one:
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
@Path("/features")
public class FeaturesResource {
@Inject
FeaturesConfig featuresConfig;
@GET
@Path("/awesome")
public boolean isAwesomeFeatureEnabled() {
return featuresConfig.awesomeFeatureEnabled();
}
}
then, you can override the config values from your unit test this way:
import io.quarkus.test.InjectMock;
import io.quarkus.test.Mock;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.smallrye.config.SmallRyeConfig;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@QuarkusTest
public class FeaturesResourceTest {
@Inject
SmallRyeConfig smallRyeConfig;
/*
* This is required to make the FeaturesConfig interface implementation proxyable.
* Without that, it wouldn’t be possible to mock it with @InjectMock.
*/
@ApplicationScoped
@Mock
FeaturesConfig featuresConfig() {
return smallRyeConfig.getConfigMapping(FeaturesConfig.class);
}
/*
* The config class is mocked with the help of the quarkus-junit5-mockito extension.
* Injections are not supported in tests in native mode, so this only works when the test is run in JVM mode.
*/
@InjectMock
FeaturesConfig featuresConfig;
@Test
void test() {
// Here's a config value override.
Mockito.when(featuresConfig.awesomeFeatureEnabled()).thenReturn(true);
RestAssured.given()
.when().get("/features/awesome")
.then().body(CoreMatchers.is("true"));
}
}
This approach also works if your project relies on @ConfigProperty
instead of @ConfigMapping
.
More details about overriding the configuration from a Quarkus test are available in this blog post I wrote: https://quarkus.io/blog/overriding-configuration-from-test-code/