Spring boot 如何在为springboot应用程序运行mvn测试时通过配置文件

Spring boot 如何在为springboot应用程序运行mvn测试时通过配置文件,spring-boot,maven,Spring Boot,Maven,我正在尝试运行spring boot应用程序的测试用例。 为此,我尝试使用mvn test-Dspring.profiles.active=test 但它不起作用。 我需要在pom.xml 请提供帮助。使用配置文件运行测试的最佳方法是使用ActiveProfiles注释(假设JUnit 5): 这将在测试概要文件处于活动状态时启动Spring应用程序。java参数位于maven生命周期参数之前: mvn -Dspring.profiles.active=test test 您还可以将属性添加到

我正在尝试运行spring boot应用程序的测试用例。
为此,我尝试使用
mvn test-Dspring.profiles.active=test

但它不起作用。
我需要在
pom.xml


请提供帮助。

使用配置文件运行测试的最佳方法是使用
ActiveProfiles
注释(假设JUnit 5):


这将在
测试
概要文件处于活动状态时启动Spring应用程序。

java参数位于maven生命周期参数之前:

mvn -Dspring.profiles.active=test test
您还可以将
属性添加到maven surefire插件以应用于所有测试

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

org.apache.maven.plugins
maven surefire插件
真的
-Dspring.profiles.active=测试

这里没有专门针对JUnit 5的内容。它也适用于其他测试库。如果使用JUnit4,则需要在类级别添加
@RunWith(SpringRunner.class)
。而且这个类和方法应该是公开的。我的意思是特别使用
@ActiveProfiles
注释。它适用于任何测试框架。
<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>