Maven / Spring boot 项目 - 如何跳过@SpringBootTest

use*_*343 8 java junit maven spring-boot

在我的项目中,有许多标有 @SpringBootTest 的测试,我不将其视为单元测试,而是将其视为集成测试。所以我想在执行时仅运行单元测试:

mvn 干净安装

实际上我想运行这个命令作为预提交 git hook 的一部分,但是 @SpringBootTest 使得完成执行的时间更长。

有没有办法排除标有@SpringBootTest的测试?可能有一种模式我们可以传递给 Maven 来排除/某些测试。或者可以编写一个包含 Spring Boot 测试的测试套件。

我通过谷歌搜索来实现上述目标,但运气不佳。

还有更好的方法吗?

@Update:约束是maven pom文件不能修改。

@Update2:我有一个看起来很有希望的解决方案:

1. Use @Category("IntegrationTests") for @SpringBootTests tests.
2. Create TestSuite with excludeCategory:
@RunWith(CategoryRunner.class)
@ExcludeCategory("IntegrationTests")
public class TestSuite {
}
3. From mvn command line, run only TestSuite.
Run Code Online (Sandbox Code Playgroud)

我不确定这是最好的。欣赏任何人更好的方法。

And*_*eas 7

如果您有不同类型的测试,并且希望能够指定要运行的测试,则可以使用 @Conditionals 或 @Profile 来执行此操作。

例子:


Ram*_*ash 1

尝试一下

mvn clean install -DskipTests 
Run Code Online (Sandbox Code Playgroud)

或者

mvn clean install -Dmaven.test.skip=true
Run Code Online (Sandbox Code Playgroud)

有关更多选项,请参阅以下链接

https://mkyong.com/maven/how-to-skip-maven-unit-test/

https://www.baeldung.com/maven-skipping-tests