use*_*426 6 junit4 maven maven-failsafe-plugin spring-boot
我试图通过maven命令分离单元测试和集成测试。
pom.xml
....
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Fast*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
这是我的整合测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StudentApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class StudentControllerIT {
...
Run Code Online (Sandbox Code Playgroud)
这是单元测试
@RunWith(SpringRunner.class)
@WebMvcTest(value = StudentController.class, secure = false)
public class StudentFastControllerTest {
....
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试运行命令时,mvn test
仅 StudentFastControllerTest
执行测试,但是当我运行命令mvn integration-test
或mvn verify
同时执行两个测试类而不是仅执行时StudentControllerIT
。
编辑:有两种方法对我有用:
1)使用maven-failsafe配置(我从这个答案得到了帮助):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>it-tests</id>
<phase>none</phase>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
请注意,此执行 i) 由单词it-tests标识,并且 ii) 将其阶段设置为none。后者将这些测试从默认生命周期中分离出来,而前者使我们能够使用以下命令按需运行它们:
mvn failsafe:integration-test@it-tests
Run Code Online (Sandbox Code Playgroud)
同样重要的是,将集成测试添加为后缀IT
,就像我们将它们包含在本节中一样,我们对其他插件(例如 Surefire)的部分也<includes>
执行相同的操作。<exclude>
2) 使用配置文件
在配置文件部分中,您可以为集成测试添加配置文件:
<profile>
<id>it-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
Run Code Online (Sandbox Code Playgroud)
此外,如果您在单独的测试源目录中进行集成测试,则将以下内容添加到配置文件配置部分,在本例中test-integration
:
<testSourceDirectory>test/test-integration/java</testSourceDirectory>
Run Code Online (Sandbox Code Playgroud)
现在返回到插件部分并在您的 上,通过将以下内容添加到其配置部分maven-surefire-plugin
来排除集成测试:mvn test
<excludes>
<exclude>**/*IT.java</exclude>
</excludes>
Run Code Online (Sandbox Code Playgroud)
最后,将以下执行添加到您的 maven-failsafe-plugin 中:
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
Run Code Online (Sandbox Code Playgroud)
因此(至少在我的情况下),集成测试不会在mvn test
我运行期间执行:
mvn failsafe:integration-test -Pit-tests
Run Code Online (Sandbox Code Playgroud)
只有我的集成测试被执行。
我希望这也适合你。
归档时间: |
|
查看次数: |
2717 次 |
最近记录: |