系统 属性 -Dspring.profiles.active 在 spring 启动测试配置中不可用 gradle 构建

system property -Dspring.profiles.active not available in spring boot test config in gradle build

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

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

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

如果你想克隆回购:

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 运行.

中看到系统 属性

编辑

即使使用 ./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

两种情况下均失败

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

test {
    systemProperty 'key', 'value'
}

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

test {
    systemProperties = System.props
}