How to override config property for one Unittest in Quarkus
Asked Answered
P

3

9

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?

Patriarchate answered 15/11, 2019 at 20:33 Comment(0)
S
2

I would suggest using test profiles

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

Stenographer answered 3/6, 2021 at 8:9 Comment(1)
Thanks a lot. This indeed seems to be the way to go about this nowadays... I will mark this as accepted even though I cannot verify if this one actually solves the problem as that particular project is long done. If someone else has anything to add that would be very welcome.Patriarchate
I
2

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

Inhibitory answered 17/11, 2019 at 14:29 Comment(1)
This is the way I am handling it right now. But the problem is that this property would then be set for all of my tests and not just for a single one.Patriarchate
S
2

I would suggest using test profiles

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

Stenographer answered 3/6, 2021 at 8:9 Comment(1)
Thanks a lot. This indeed seems to be the way to go about this nowadays... I will mark this as accepted even though I cannot verify if this one actually solves the problem as that particular project is long done. If someone else has anything to add that would be very welcome.Patriarchate
M
0

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/

Madly answered 4/7 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.