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

yaz*_*ara 7 java spring profiling gradle spring-profiles

需要通过带有弹簧轮廓的gradle进行测试.

gradle clean build
Run Code Online (Sandbox Code Playgroud)

我添加了任务:

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

test.dependsOn beforeTest
Run Code Online (Sandbox Code Playgroud)

我的测试定义是:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TestProfile")
public class SomeTest {
Run Code Online (Sandbox Code Playgroud)

但这种结构对我不起作用.

Gradle运行测试.

lan*_*ava 7

我认为您想要在运行时/测试JVM中设置系统属性,但是您在构建时JVM(即Gradle守护程序)中错误地设置了系统属性.

请参见Test.systemProperty(String,Object)

例如:

test {
    systemProperty 'spring.profiles.active', 'DEV'
}
Run Code Online (Sandbox Code Playgroud)

......以及关于你的尝试的另一个注释.请注意,任务有一个doFirst和一个doLast方法,因此您不需要为您尝试的内容单独执行任务.