我正在尝试运行spring boot应用程序的测试用例。
为此,我尝试使用mvn test -Dspring.profiles.active=test
。
但这没用。
我是否需要在pom.xml
可以请你帮忙。
答案 0 :(得分:1)
使用配置文件运行测试的最佳方法是使用ActiveProfiles
批注(假设JUnit 5):
@SpringBootTest
@ActiveProfiles("test")
class MyApplicationTest {
@Test
void test() {
// implement actual test here
}
}
这将在激活test
配置文件的情况下启动您的Spring应用程序。
答案 1 :(得分:1)
java参数位于maven生命周期参数之前:
mvn -Dspring.profiles.active=test test
您还可以向Maven surefire插件添加<argLine>
属性,以应用于所有测试。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<testEnvironment>true</testEnvironment>
</systemPropertyVariables>
<argLine>-Dspring.profiles.active=test</argLine>
</configuration>
</plugin>