需要通过 gradle 使用 spring 配置文件运行测试。
gradle clean build
我添加了任务:
task beforeTest() {
doLast {
System.setProperty("spring.profiles.active", "DEV")
}
}
test.dependsOn beforeTest
我的测试定义是:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("TestProfile")
public class SomeTest {
但这种结构对我来说不起作用。
Gradle 运行测试。
我认为您想要在运行时/测试 JVM 中设置系统属性,但在构建时 JVM(即 Gradle 守护进程)中错误地设置系统属性。
See Test.systemProperty(字符串,对象) https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html#systemProperty%28java.lang.String,%20java.lang.Object%29
Eg:
test {
systemProperty 'spring.profiles.active', 'DEV'
}
...以及关于您的尝试的另一个注释。请注意,任务有一个doFirst
and a doLast
方法,这样您就不需要为您正在尝试的事情单独执行任务。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)