Obyoxar
Obyoxar

Reputation: 113

How to override config property for one Unittest in Quarkus

In my Quarkus-Application an Observer of StartupEvent inserts default data into my database if a specific config-property is true. For one particular UnitTest I want my database to be empty.

I would think that there is some way to override configuration values for one unittest. Is that true, or is there a better way?

Upvotes: 9

Views: 7207

Answers (3)

Gwenneg
Gwenneg

Reputation: 11

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/

Upvotes: 1

gaetanc
gaetanc

Reputation: 116

I would suggest using test profiles

https://quarkus.io/blog/quarkus-test-profiles/

Upvotes: 2

Serkan
Serkan

Reputation: 1233

Have you tried out by using a test profile for that property in your application.properties ?

Something like this:

—default value is A

myProp=A

—this is the test profile, which overrides the default value

%tst.myProp=B

Upvotes: 2

Related Questions