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)
我不确定这是最好的。欣赏任何人更好的方法。
如果您有不同类型的测试,并且希望能够指定要运行的测试,则可以使用 @Conditionals 或 @Profile 来执行此操作。
例子:
@ConditionalOnProperty("test.run.integration")
该类仅在属性test.run.integration
被定义时才会被Spring加载,而不是被定义false
。
@Profile("integrationtest")
仅当配置文件处于活动状态时,Spring 才会加载该类integrationtest
。
尝试一下
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
归档时间: |
|
查看次数: |
30449 次 |
最近记录: |