Microprofile |配置 - 使用动态值进行测试

问题描述 投票:1回答:2

我在我的应用程序中使用Microprofile Config(@Inject,而不是ConfigProvider)。我有一个配置,为不同的值采用不同的分支。为了测试(Arquillian)我的代码中的所有路径,我需要能够在运行时更改此值。有人可以提供如何实现这一目标的提示吗?我的属性是使用系统属性设置的,但我对如何处理它的想法持开放态度。

java jboss-arquillian tomee microprofile tomee-8
2个回答
2
投票

您可以注册一个可以轻松配置的ConfigSource。您可以查看我为mp-config TCK本身编写的那个:https://github.com/eclipse/microprofile-config/blob/master/tck/src/main/java/org/eclipse/microprofile/config/tck/configsources/ConfigurableConfigSource.java

要将此ConfigSource添加到Arquillian @Deployment,请检查此测试:https://github.com/eclipse/microprofile-config/blob/1499b7bf734eb1710fe3b7fbdbbcb1ca0983e4cd/tck/src/main/java/org/eclipse/microprofile/config/tck/ConfigAccessorTest.java#L52

重要的是:

.addClass(ConfigurableConfigSource.class)
.addAsServiceProvider(ConfigSource.class, ConfigurableConfigSource.class)

然后调整值

ConfigurableConfigSource.configure(config, "my.config.entry", "some new value");

0
投票

关于提及如下的Microprofile Config: Spec: Configsource: -

系统属性(默认序号= 400)。

环境变量(默认序号= 300)。

在类路径中找到的每个属性文件META-INF / microprofile-config.properties的ConfigSource。 (默认序号= 100)。

这意味着system properties是这里的最高优先级。然后我们可以在META-INF/microprofile-config.properties设置默认值,并在system properties需要时覆盖它。

在集成测试期间,我们可以设置system properties并使用javax.inject.Provider使其动态检索,以便覆盖默认值,如下例所示: -

# META-INF/microprofile-config.properties
my.key=original
import javax.inject.Inject;
import javax.inject.Provider;

import org.eclipse.microprofile.config.inject.ConfigProperty;

public class SomeClass {
    @Inject
    @ConfigProperty(
        name = "my.key"
    )
    private Provider<String> key1;

    public String doSomethingWithConfig() {
        return key1.get();
    }
}
import javax.inject.Inject;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.junit.Test;
import org.junit.Assert;

@RunWith(Arquillian.class)
public class SomeClassTester {

    @Inject
    private SomeClass someclass;

    @Test
    @InSequence(1)
    public void whenTestDefaultConfig() {
        Assert.assertEquals("The value must be a defualt.",
                            "original", 
                            this.someclass.doSomethingWithConfig());
    }

    @Test
    @InSequence(2)
    public void whenTestOverrideMPConfig() {

        System.setProperty("my.key",
                           "new-value");
        Assert.assertEquals("The value must be overridden",
                            "new-value", 
                            this.someclass.doSomethingWithConfig());
    }

}

EDIT1

此外,如果我们想控制system properitesSystem Rules将使我们的生活更轻松。他们提供ClearSystemPropertiesProvideSystemPropertyRestoreSystemProperties作为他们的文件中的以下示例。

public class MyTest {
    @Rule
    public final RestoreSystemProperties restoreSystemProperties
     = new RestoreSystemProperties();

    @Test
    public void overrideProperty() {
        //after the test the original value of "MyProperty" will be restored.
        System.setProperty("MyProperty", "other value");
        ...
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.