在SpringBoot应用程序运行MVN测试时如何通过配置文件

时间:2020-09-15 14:50:43

标签: spring-boot maven

我正在尝试运行spring boot应用程序的测试用例。
为此,我尝试使用mvn test -Dspring.profiles.active=test
但这没用。
我是否需要在pom.xml

中添加个人资料

可以请你帮忙。

2 个答案:

答案 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>