Can I override quarkus application.properties value in my test class?

Viewed 3550

I have a value configured in my quarkus application.properties

skipvaluecheck=true

Now whenever I want to execute my tests, I want to have this value to be set to false instead of true. But I do not want to change in application.properties because it will affect the latest application deployment. I just want my tests to be executed with value false so that my test coverage goes green in sonar.

From java code, I fetch this value by doing below

ConfigProvider.getConfig().getValue("skipvaluecheck", Boolean.class);

Something similar already exists in Sprint boot and I am curious if such thing also exist in quarkus

Override default Spring-Boot application.properties settings in Junit Test

3 Answers

You need to define an implementation of io.quarkus.test.junit.QuarkusTestProfile and add it to the test via @TestProfile.

Something like:

@QuarkusTest
@TestProfile(MyTest.MyProfile.class)
public class MyTest {
    @Test
    public void testSomething() {
    }
    
    public static class BuildTimeValueChangeTestProfile implements QuarkusTestProfile {
    
        @Override
        public Map<String, String> getConfigOverrides() {
            return Map.of("skipvaluecheck", "true");
        }
    }
}

See more details can be found here

Quarkus provides the use of a QuarkusTestProfile for this, you can define a profile like so:

public class CustomTestProfile implements QuarkusTestProfile {

    Map<String, String> getConfigOverrides() {
        return Map.of("skipvaluecheck", "false");
    }

}

Then on your test class:

@QuarkusTest
@TestProfile(CustomTestProfile.class)
public class TestClass {
//...(etc)...

More info available here: https://quarkus.io/blog/quarkus-test-profiles/

Quarkus application properties have profiles. e.g.

quarkus.log.level=WARN
%test.quarkus.log.level=INFO

That way (with prefix %test.) you can set a different value for testing instead of the production value. You can also set %dev. for when you're running in local dev mode.

See https://quarkus.io/guides/config-reference#profiles for reference.

Related