简体   繁体   English

使用Spring Profiles运行gradle任务(集成测试)

[英]Run gradle task with Spring Profiles (Integration tests)

Need to run tests via gradle with spring profiles. 需要通过带有弹簧轮廓的gradle进行测试。

gradle clean build

I've added task: 我添加了任务:

task beforeTest() {
    doLast {
      System.setProperty("spring.profiles.active", "DEV")
    }
}

test.dependsOn beforeTest

And my test definition is: 我的测试定义是:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TestProfile")
public class SomeTest {

But this construction doesn't work for me. 但这种结构对我不起作用。

Gradle runs tests. Gradle运行测试。

I think you are wanting to set a system property in the runtime/test JVM but you are incorrectly setting a system property in the build-time JVM (ie the Gradle daemon). 我认为您想要在运行时/测试JVM中设置系统属性,但是您在构建时JVM(即Gradle守护程序)中错误地设置了系统属性。

See Test.systemProperty(String, Object) 请参见Test.systemProperty(String,Object)

Eg: 例如:

test {
    systemProperty 'spring.profiles.active', 'DEV'
}

... and another note on your attempt. ......以及关于你的尝试的另一个注释。 Please note that tasks have a doFirst and a doLast method so you wouldn't need a separate task for what you were attempting. 请注意,任务具有doFirstdoLast方法,因此您不需要为尝试的内容单独执行任务。

This works for me: 这对我有用:

test {
    doFirst {
        systemProperty 'spring.profiles.active', 'ci'
    }
}

Now when I do gradlew test it runs with the ci profile. 现在,当我进行gradlew test它会运行ci配置文件。

Wonder if this is a very late reply? 不知道这是否是一个非常晚的回复? But I have encountered the similar problem with situation. 但我遇到了与情况类似的问题。

  1. Using @Profiles in springboot src/test/* folder. 在springboot src / test / *文件夹中使用@Profiles。
  2. The @ActiveProfiles is actually activated (when viewing logs), but it doesn't use the @Profile when executed with gradle test. @ActiveProfiles实际上已激活(查看日志时),但在使用gradle测试执行时不使用@Profile。 However, the @Profile was working in Eclipse Editor when JUnit test was executed. 但是,在执行JUnit测试时,@ Editor正在Eclipse Editor中工作。

So in the end i found that @Profile needs to be in src/main/* folder. 所以最后我发现@Profile需要在src / main / *文件夹中。 Gradle test seems to have precedence over /src/main rather than /src/test. Gradle测试似乎优先于/ src / main而不是/ src / test。 With this my @Profile("test") was converted into @Profile("!test"). 有了这个,我的@Profile(“测试”)被转换为@Profile(“!test”)。 There was no added spring.profiles.active into the gradle built. 没有添加spring.profiles.active进入gradle构建。

Negating profile 否定个人资料

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM