【问题标题】:system property -Dspring.profiles.active not available in spring boot test config in gradle build系统属性 -Dspring.profiles.active 在 gradle build 的 spring boot 测试配置中不可用
【发布时间】:2016-12-17 11:21:35
【问题描述】:

我有一个 Spring Boot gradle 应用程序。当我运行 gradle 构建时,我想针对不同的环境运行。

例如:./gradlew clean build -Dapp.env=QA1。在测试代​​码中,我想检查这个属性并相应地收集测试数据。我观察到的是属性(app.env)不可用。

构建应该会失败,因为测试会检查系统属性。但构建成功。我也没有在控制台中看到 println 语句。

如果你想克隆 repo:

git 克隆https://SpringDevSeattle@bitbucket.org/SpringDevSeattle/gs-rest-service.git

这是我的测试代码:

src/test/java/hello/GreetingControllerTests.java

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ContextConfiguration(classes={hello.TestConfig.class,hello.AppConfig.class})
@WebAppConfiguration
public class GreetingControllerTests {


    @Autowired
    TestEnv testEnv;

    @Autowired
    Status status;

    private String env;

    @Before
    public void init(){
        System.out.println(status.getName());
        env=testEnv.getEnv();
    }

    @Test
    public void should_fail(){
        if (env.equalsIgnoreCase("DEV")){
            assertThat(false).isFalse();
        }
        if (env.equalsIgnoreCase("QA1")){
            System.out.println("failing the test");
            fail();
        }
    }

}

src/test/java/hello/TestConfig.java

@Configuration
public class TestConfig {


    @Bean
    public TestEnv testEnv(){
        Properties properties = System.getProperties();
        String env = properties.getProperty("app.env");
        System.out.println(env);
        if (env==null){
            env="dev";
        }
        return new TestEnv(env);
    }
}

src/test/java/hello/TestEnv.java

public class TestEnv {


        private String env;

        public TestEnv(String env){
            this.env=env;
        }

        public String getEnv() {
            return env;
        }

        public void setEnv(String env) {
            this.env = env;
        }
}

src/main/java/hello/AppConfig.java

@Configuration
public class AppConfig {

    @Value("${app.version}")
    private String version;
    @Value("${app.name}")
    private String name;


    @Bean
    public Status status(){
        return new Status(name,version);
    }

}

我的最终目标是为基于通过的-Dapp.env 的测试设置“spring.profiles.active”。但我目前在 gradle run 中没有看到系统属性。

编辑

即使使用./gradlew clean build -Dspring.profiles.active=QA1,我也看不到它有效。

测试相应地改变了。

@Autowired
Environment envi

@Before
    public void init(){
        System.out.println("*********-IN THE INIT METHOD **********"); //new line added
        System.out.println(status.getName());
        env=envi.getActiveProfiles()[0];
    }

@Test
    public void should_fail(){
        if (env.equalsIgnoreCase("DEV")){
            assertThat(false).isFalse();
        }
        if (env.equalsIgnoreCase("QA1")){
            System.out.println("failing the test");
            fail();
        }
    }

-Dspring.profiles.active=DEV-Dspring.profiles.active=QA1 两种情况下构建都会失败

【问题讨论】:

    标签: spring gradle spring-boot spring-test


    【解决方案1】:

    Gradle 在单独的 JVM 中执行测试。因此,在命令行中指定的系统属性在 build JVM 中使用,并且不会传播到测试 JVM。这样做是为了将测试过程与构建过程隔离开来。您可以像这样为测试过程明确指定系统属性。

    test {
        systemProperty 'key', 'value'
    }
    

    或者,您可以简单地将所有系统属性传递给测试进程,这将允许您通过-D 语法在命令行上指定它们。

    test {
        systemProperties = System.props
    }
    

    【讨论】:

    • 是的,在我的 build.gradle 中添加它之后,我看到了预期的行为。但我没有看到控制台上的 println。知道为什么吗? System.out.println("failing the test"); 方法中的 should_fail?谢谢
    • 你可以看到在 init() 方法中添加了一个打印语句System.out.println("*********-IN THE INIT METHOD **********")。那应该始终执行。但我没有在控制台中看到。这些行在 gradle build 中去哪里了?
    • 您应该能够在 build/reports/tests/test/index.html 中的测试报告中看到这一点。
    • 你能帮忙解决这个问题吗?它更基于 gradle。谢谢。 stackoverflow.com/questions/38752485/…
    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 2011-12-02
    • 2017-02-10
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多